How to configure different tab settings for different languages ​​in Vim?

In my .vimrc I have a general tab setting of two spaces, and I would like to override this to (i.e. four for Python, etc., otherwise use the default value), but it's hard for me to find a good example of this.

+57
vim
May 21 '09 at 7:48 a.m.
source share
5 answers

Just put the settings in the filetype ~ / .vim / ftplugin / LANGUAGE.vim plugin file. 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 apply to each file with the 'perl' file type (new or existing).

+46
May 21 '09 at 8:03 a.m.
source share

These other answers seem too complicated. Instead of messing around with even more directories and files in the ~ / .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 reduce the parameters to et ts=4 sw=4 sts=4 ). I found this in Configuring Vim Space Settings by File Type

+56
Aug 26 2018-12-12T00:
source share

My answer is based on this tip on the VIM Wiki. This answer uses the "after" directory, so you don’t have to guess with the attached plugin files for different file types.

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

 setlocal expandtab setlocal shiftwidth=4 setlocal softtabstop=4 

Put this file in

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

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

 filetype plugin indent on 
+19
Nov 16 '09 at 16:26
source share

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

 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 things like "set expandtab" to use spaces instead of tabs and all kinds of magic for formatting and the like. You can often download good versions of these files for different languages ​​from http://vim.org and other sites.

Please note that you can do much more than just change the vim settings. For example, you can run a 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 
+8
May 21 '09 at 7:54 a.m.
source share

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

You can see an example of settings that you can set here: http://editorconfig.org/

+2
Feb 15 '15 at 11:01
source share



All Articles