Rob Avery IV
Rob Avery IV

Reputation: 3730

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!!

Upvotes: 4

Views: 5586

Answers (4)

Luc Hermitte
Luc Hermitte

Reputation: 32946

Romainl gave you the right answer, ftplugins are the way to go, but it is incomplete.

As your question is a recurring one, here are a few more complete other answers that I've found:

Upvotes: 3

Ingo Karkat
Ingo Karkat

Reputation: 172590

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.

Upvotes: 13

romainl
romainl

Reputation: 196566

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

~/.vim/ftplugin/c.vim

Upvotes: 9

Michael Berkowski
Michael Berkowski

Reputation: 270637

You want autocmd

:help autocmd

autocmd FileType c,java map something somethingelse
" Colorscheme just for PHP
autocmd FileType php colorscheme desert

By testing the FileType, you can specify additional specific settings. Or, inside your ~/.vim/ftplugin/, create files for individual types:

~/.vim/ftplugin/java.vim
~/.vim/ftplugin/c.vim

Upvotes: 5

Related Questions