如何在 Vim 中为不同的语言设置不同的选项卡设置?

在我的 。 vimrc中,我有一个两个空格的通用制表符设置,我想在每种语言的基础上覆盖它(也就是说,对 Python 来说是四个,等等,否则使用默认设置) ,但是我很难找到这方面的任何好例子。

24921 次浏览

Typically what you do is set up a special vimrc-type file with the settings for a particular language, and then use autocommands in your main .vimrc to execute the special vimrc when necessary. Here's my configuration for Haskell (.hs, etc.) files:

autocmd! BufNewFile,BufReadPre,FileReadPre  *.hs    so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.hsc   so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.lhs   so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.cabal so ~/.vim/haskell.vim

My ~/.vim/haskell.vim does stuff like "set expandtab" to use spaces instead of tabs, and all sorts of other magic for formatting and things like this. You can often download good versions of these for various languages from http://vim.org and other sites.

Note that you can do a lot more than just change vim settings. For example, you can run the file through a filter before and after editing:

" Edit gpg-encrypted ascii-armoured files
autocmd! BufReadPre,FileReadPre      *.asc  set bin
autocmd  BufReadPost,FileReadPost    *.asc  '[,']!gpg -q -d
autocmd  BufReadPost,FileReadPost    *.asc  set nobin
autocmd! BufWritePre,FileWritePre    *.asc  set bin
autocmd  BufWritePre,FileWritePre    *.asc  '[,']!gpg -e
autocmd  BufWritePost,FileWritePost  *.asc  undo
autocmd  BufWritePost,FileWritePost  *.asc  set nobin

Just put the settings into the filetype plugin file ~/.vim/ftplugin/LANGUAGE.vim . My ~/.vim/ftplugin/perl.vim contains the lines:

"
" ---------- tabulator / shiftwidth --------------------
"  Set tabulator and shift width to 4 (Perl Style Guide)
"
setlocal  tabstop=4
setlocal  shiftwidth=4
"

These settings will automatically be in effect for each file with file type 'perl' (new or existing).

My answer is based on this tip on the VIM Wiki. This answer uses the "after" directory so you won't have to muck with the supplied plugin files for different filetypes.

For example, to specify custom settings for Python files, create a file called python.vim to hold your Python settings:

setlocal expandtab
setlocal shiftwidth=4
setlocal softtabstop=4

Place this file in either

  • ~/.vim/after/ftplugin (Linux)
  • $HOME/vimfiles/after/ftplugin (Windows)

And finally, you must have this in your .vimrc (Linux) or _vimrc (Windows):

filetype plugin indent on

These other answers seem way too complex. Instead of messing around with yet more directories and files in your ~/.vim tree, just add the following to your ~/.vimrc.

autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4

(you can be l33t and abbreviate parameters to et ts=4 sw=4 sts=4). I found this in Setting Vim whitespace preferences by filetype

I use the editor config plugin and add a .editorconfig file to all my projects - it will let you define different indentation settings for different projects using the same programming language which can be useful as often different projects in the same language have different coding standards.

You can see an example of the types of configuration you can set here: http://editorconfig.org/