Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change vim settings distinctively which each language?

I use vim with many different languages (C, C++, Java, shell, etc.). I know vim already has it preset settings for each language, but I want to change the settings for each individual language to my personal perference. I already have a .vimrc file with settings,but I want a few more files to declare more specific settings according to the language I'm using. What are the files suppose to be called? c.vim? java.vim?

Example: In C, I want my comments to be green, while in Java, I want them to be light purple.

ALSO, I hate vim's preset colorschemes. What's a way I can create my own colorscheme?

I hope that is understandable and complete and makes sense.

Thanks in advance!!

like image 372
Rob Avery IV Avatar asked Nov 28 '22 14:11

Rob Avery IV


2 Answers

You can use either autocmds:

autocmd FileType java colorscheme desert

or put the commands in ~/.vim/ftplugin/java_mycolors.vim. (This is for new settings; if you want to override stuff from the default, system-wide ftplugins, you'd put them in ~/.vim/after/ftplugin/java.vim.)

As you can see, the first approach is quick and dirty, while the latter allows for modularity and lots of customization. Your call.


As for changing the colorscheme, this is a global setting; you cannot mix them at the same time; but you will only notice that when you split windows or use tab pages, though, so it may be fine.

You can however change the individual syntax colors. By default, comments in all languages are linked to the Comment highlight group. Read the syntax file (e.g. $VIMRUNTIME/syntax/java.vim) or use the SyntaxAttr.vim plugin to determine the group name. Then you can redefine it in your .vimrc:

:highlight javaLineComment guifg=Purple
:highlight javaComment guifg=Purple

This is tedious (depending on how much you want to customize), but more precise, and works in parallel. I'd recommend this unless you really want totally different coloring for each filetype.

like image 152
Ingo Karkat Avatar answered Dec 14 '22 22:12

Ingo Karkat


You should put all your language-specific settings in the ftplugin directory:

~/.vim/ftplugin/c.vim
like image 26
romainl Avatar answered Dec 14 '22 23:12

romainl