How to change vim settings distinctively which each language?

跟風遠走 提交于 2020-01-01 12:12:50

问题


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


回答1:


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.




回答2:


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

~/.vim/ftplugin/c.vim



回答3:


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



回答4:


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:

  • https://stackoverflow.com/a/1414845/15934
  • https://stackoverflow.com/a/8826323/15934
  • gVim and multiple programming languages


来源:https://stackoverflow.com/questions/12055105/how-to-change-vim-settings-distinctively-which-each-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!