Wikia

Vim Tips Wiki

Better colors for syntax highlighting

Talk0
1,613pages on
this wiki

Redirected from VimTip53

Tip 53 Printable Monobook Previous Next

created 2001 · complexity basic · version 5.7


For syntax highlighting there are two sets of default color maps: One for a light and another one for a dark background.

If you have a black background, use the following command to get a better color map for syntax highlighting:

:set background=dark

For a detailed description of syntax highlighting refer to :help syntax.txt

See also the Vim syntax support file: $VIMRUNTIME/syntax/synload.vim

CommentsEdit

I just checked this in 6.0 and you no longer have to turn syntax off and then back on.


This function reverses background setting so you can quickly toggle between light and dark . I use it because I'm often changing my desktop background and use transparent terminals.

:function! ReverseBackground()
: let Mysyn=&syntax
: if &bg=="light"
: se bg=dark
: else
: se bg=light
: endif
: syn on
: exe "set syntax=" . Mysyn
": echo "now syntax is "&syntax
:endfunction
:command! Invbg call ReverseBackground()
:noremap <F11> :Invbg<CR>

You may also need to change the highlighting to match the new background. Here's the same macro as before, but toggling the background colour of the window as well:

:function! ReverseBackground()
: let Mysyn=&syntax
: if &bg=="light"
: se bg=dark
: highlight Normal guibg=black guifg=white
: else
: se bg=light
: highlight Normal guibg=white guifg=black
: endif
: syn on
: exe "set syntax=" . Mysyn
": echo "now syntax is "&syntax
:endfunction
:command! Invbg call ReverseBackground()
:noremap <F11> :Invbg<CR>

script#105


Just to say I use this line in my vimrc:

map <F11> :let &background = ( &background == "dark"? "light" : "dark" )<CR>

That let's me switch backgrounds pretty quickly


Tweaking the colours isn't too hard.

as root head over to /usr/share/vim/vim63/colors/ or where ever you're storing all the .vim definitions for the colorschemes. Then open the color scheme file you use most of the time and look for the line starting with 'hi Comment'.

If it's not in there (it wasn't in the evening.vim for me) just add it. then set ctermfg=<something> and guifg=<something> if you're using gvim.

eg: (my evening.vim file has the added line

hi Comment ctermfg=DarkGrey guifg=DarkGrey

you can then do the same things for Constant and Identifier types. You'll notice what you're looking for in the syntax definition files. /usr/share/vim/vim63/syntax for me.


To change the search highlighting I use:

hi Search guibg=LightBlue

(also try LightGreen)

For my error message highlighting I use:

highlight ErrorMsg guibg=White guifg=Red

"hi Search guibg=LightBlue" is only for the GUI version of Vim. If you're using Vim in-console, try "hi Search ctermbg=LightBlue"


Around Wikia's network

Random Wiki