Wikia

Vim Tips Wiki

Script for choosing vim background color

Talk0
1,612pages on
this wiki
Revision as of 17:46, October 9, 2012 by Alain b (Talk | contribs)

While choosing colors in gvim is very easy, done by a single ":colorscheme some-name" command, in contrast it's much harder for vim users to do the same. Here is a small script which may help choosing a background color for vim users.

script: vim_bg_color_test.vim

" script name : vim_bg_color_test.vim
"
" dedicated to choose a background color in vim (not gvim), for ":hi Normal".
" Needed here is an integer ranging from  0 to 255. (vs rgb values for gvim)
" (Does color value effect depends on operating used ?)
" (Here foreground color is set to black but script may help on choosing 
" foreground color too.)
"
" Execute this script from within vim by ":source %" if you edit it currently.
" Beginning and ending values are asked for in order to do a loop on colors.
" The status line at the bottom of screen shows script effect.
" Type a carriage-return after seeing color showed (and associated number).
"
" In principle the lot of highlight (:hi) options are well chosen by vim
" when ":hi Normal" options  are set, while possibly adding in .vimrc (linux) :
" :set background=dark or light
" :set t_Co=256
" 08/08/2012 - alain_b
"----------------------------------------------
let n=input("First number (0 to 255)")
let n2=input("Last number (0 to 255)")
while n <= n2 
  exe "hi Normal ctermfg=black ctermbg=".n
  exe input("Value ".n )
  let n=n+1
endwhile
" Initial display restored
source ~/.vimrc

Your vimrc should contain something like this:

if has("gui_running")
  colorscheme peachpuff
else
  set t_Co=256
  "set background=dark  " dark/light if useful
  " The ctermbg option has been chosen using vim_bg_color_test.vim script 
  highlight Normal ctermfg=black ctermbg=222
endif

References

Vim documentation gives only one example, for vim users:

:highlight Normal ctermfg=grey ctermbg=darkblue

and vim gives access to "colortest.vim" script which seems to me too difficult to tweak.

Comments

I don't understand what problem you're solving here. What's wrong with colorschemes in a color terminal? --Fritzophrenic (talk) 17:45, August 8, 2012 (UTC)

The problem is not with gvim (colorscheme is easy to use), as said, but with vim.

Coming back to the script. It is working well inside vim 7.3 on ubuntu. I am not sure it's ok everywhere, since selected background color may sometimes not be visible, on the bottom line of the screen. I don't know why.

Around Wikia's network

Random Wiki