18

I've found Vim to have fantastic syntax highlighting with my preferred color schemes for Ruby and Python, but whenever I need to write Java code, it's like I haven't even turned on highlighting in the first place.

As you can see, keywords are highlighted, but there's no highlighting for parentheses or methods. In Sublime Text (a far less preferred editor by me), I see far better highlighting. There is italicization for class names and highlighting for mathematical operators. Vim looks sparse in comparison.

Is there any way, any way at all to get that luscious Sublime Text-style highlighting on Vim?

  • Have you tried using Vintage mode in Sublime Text? It changes all the key bindings to those vi(m) uses, yet still allows for powerful features of ST2 like better syntax highlighting and multiple selection editing. – MattDMo Aug 3 '13 at 23:36
  • 2
    I do have Vintage Mode enabled in ST2, but I still prefer Vim because I can use it in the terminal, with several tmux panes open so that I can ssh into my remote machine simultaneously without leaving my editor for more than a second. – kunaicode Aug 3 '13 at 23:46
  • yes, there is that minor setback of using a GUI editor... – MattDMo Aug 3 '13 at 23:50
1

Have a look at the cSyntaxAfter plugin. It highlights operators et cetera.

Another option is to edit the syntax/java.vim script and add highlighting for the Operator group. Have a look at syntax/pascal.vim as an example.

It you want to use italics for class names, that should be possible if the java syntax file recognizes them as a group, and I think it does. It seems that class names are in the JavaTypedef group.

You would then have to define a new highlight for that group. That would mean removing the line

JavaHiLink javaTypedef                Typedef

from the syntax file, and adding a new one. Below I'm re-using the hightlight declaration for Type, which is what Typedef is linked to. I changed the term from underline to italic.

You should put the following in yout java.vim syntax file.

hi javaTypedef term=italic cterm=NONE ctermfg=LightGreen ctermbg=NONE gui=bold guifg=#60ff60 guibg=NONE
| improve this answer | |
  • Thanks for the help, but is/are there any plugin or plugins to capitalize class names? – kunaicode Aug 19 '13 at 0:43
  • @kunaicode: Not that I know of. Syntax highlighting doesn't alter the meaning of the code. Capitalization does. Personally I'd immediately remove a plugin that would automatically change code that I have written. – Roland Smith Aug 19 '13 at 17:11
  • I apologize for the confusion-- I meant, italicize class names. Not capitalize. Sorry. – kunaicode Aug 21 '13 at 23:46
  • @kunaicode: See updated answer. – Roland Smith Aug 24 '13 at 10:27
3

It seems there simply isn't a rich syntax file out there.

But we can squeeze a little bit more out of the bundled syntax file we have:

let java_highlight_functions = 1
let java_highlight_all = 1
" If you are trying this at runtime, you need to reload the syntax file
set filetype=java

" Some more highlights, in addition to those suggested by cmcginty
highlight link javaScopeDecl Statement
highlight link javaType Type
highlight link javaDocTags PreProc

The first trick came from here.

If someone ever makes a richer Java syntax file, we should add it to vim-polyglot!

| improve this answer | |
1

I'm using base16-default color scheme in Vim and for some reason it does a bad job defining colors for Java. Adding the following lines will help get part of the way there:

" Java: 'new', 'instanceof'
highlight Operator ctermfg=5  guifg=#d175bc
" Java: 'this', 'super'
highlight Typedef ctermfg=5  guifg=#d175bc
" Java: 'void', 'int', 'double'
highlight Type ctermfg=4  guifg=#69b7d3
" literal numbers
highlight Number term=bold ctermfg=16 gui=bold guifg=#d2d22d
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.