tags:

views:

425

answers:

2

VIM: Is it possible to change the color of these symbols: ~!%^&*()-+=[]{},.<>?:/; like Visual Studio does?

A: 

Yes, you need to edit the C color theme in vimfiles/colors/c.vim I don't know all the theme options one can use, but I'm sure they are documented on http://vim.org/

ewanm89
While I'm all for everyone reading the documentation, simply pointing to it isn't much of an answer... and you got the path wrong. This is syntax highlighting, not a color scheme, so it's in syntax/, not colors/. Also, vimfiles is an add-on/override directory used for all versions of Vim; the built-in scripts are all in the vimXX directory.
Jefromi
The colour scheme is loaded by the syntax defines what is what, the colour scheme defines what colour to use, that way most the time on matches colour highlighting nomatter which syntax type is loaded.
ewanm89
+1  A: 

The C/C++ syntaxes are defined in syntax/c.vim and syntax/cpp.vim. If you're using Linux, the main syntax directory is in /usr/share/vimXX/, where XX is the version (e.g. mine are in vim72). I don't know about installation directories on other OSes, but I'm sure you can find it. I'd suggest making a copy of these and placing them in your user vim directory (for example, in Linux, $HOME/.vim/syntax/c.vim and so on). You can then add whatever you like.

The C++ syntax sources the C syntax, so any symbols you want highlighted in both should go in c.vim, and anything for C++ only should be in cpp.vim.

To get syntax highlighting for specific symbols, you'll need to use a syntax match statement, something like:

syn match cUserSpecialCharacter display "[~!%^&*()-+=[\]{},.<>?:;]"
syn match cUserSpecialCharacter display "/[^*/]"me=e-1
syn match cUserSpecialCharacter display "/$"

I called it cUserSpecialCharacter since cCharacter and cSpecialCharacter are already used. The second and third matches are a bit of a kludge to highlight '/' without it matching comment prefixes, which would then override the comment highlighting and break everything. The "display" option tells Vim that it doesn't need to look for this match if it's not going to be displayed - see :help syn-display for an explanation if you like!

Once you've defined a syntax match, you can link it to a highlight group, for example:

hi def link cUserSpecialCharacter cCharacter

This will put it in with the already defined cCharacter group, so it'll get whatever highlighting that gets - in this case, Character. You can see a nice list of highlight groups at the bottom of c.vim for examples. If you really want, you can also hardcode a highlight by doing something like:

hi cUserSpecialCharacter term=reverse ctermfg=15 ctermbg=1 guifg=#ffffff guibg=#800000

(Arbitrary example - my current highlighting for the Error group.) See :help hi for more information on this, or simply :hi to see the list of defined highlighting - plenty of examples. I'd recommend against doing this, though, since it won't change with color schemes.

Jefromi
Thanks Jefromi for your thorough response. I have made the test and it works fine. There is just a minor issue; The "-" symbol refuses to highlight.
Tassos
My best guess is that it's part of another syntax definition, which takes priority. The C syntax definitions are pretty complex, so unfortunately I don't know enough to point out where it might be. You can do a search like "/syn *\(match\|region\)[^"]*"[^"]*-" to look for things containing the '-' and see if you find anything?
Jefromi