Search code examples
vimcolor-scheme

How to change the color of operators and methods in vim?


Recently I've been trying to create my own color scheme for vim. I've been mostly just writing in-file, referencing Vivify for hi syntax calls and another site for RGB color codes. After changing most of the color scheme, and creating something that I'm happy with, I noticed that you can't change the color of operators (i.e. +, -, =, || etc.) or the color of methods (e.g. object.method()), as both of these fall under the keyword Normal. Being a java programmer, and having used only the NetBeans IDE up to this point, it's kind of tough not to have any differentiation between objects and their methods. As such I was hoping someone would know of a work-around, or a way to color such forms of syntax not normally changeable via pre-reserved keywords.

Here's the relevant color scheme:

image


Solution

  • Create a file $HOME/.vim/after/syntax/java.vim with:

    " highlight operators
    syntax match _Operator "[-+&|<>=!\/~.,;:*%&^?()\[\]{}]"
    
    " highlight methods
    syntax match _Paren "?=(" contains=cParen,cCppParen
    syntax match _memberFunc "\.\s*\w\+\s*(\@=" contains=_Operator,_Paren
    
    " colors
    hi _memberFunc guifg=#00FF00 guibg=NONE gui=none
    hi _Operator guifg=#FF0000 guibg=NONE gui=none
    
    " to resolve conflict with comment markers
    syntax region _Comment start="\/\*" end="\*\/"
    syntax match _Comment "\/\/.*$"
    hi link _Comment Comment
    

    enter image description here