I want to set different settings for different languages or
filetypes.
Every language has own style guides (e.g., different tab size, spaces instead tabs, etc.) so I can't add settings below in my .vimrc since I use vim with several languages. The settings should be separate for each language
Python files settings for 4 spaces indent style:
set tabstop=4
set shiftwidth=4
JavaScript files settings for 2 spaces indent style:
set tabstop=2
set shiftwidth=2
autocmd FileType python call Python_settings()
function! Python_settings()
setlocal tabstop=4
setlocal shiftwidth=4
setlocal expandtab
endfunction
Vim comes with built-in filetype detection that, among other things, makes it possible to do things differently for different filetypes.
For that mechanism to work, you need either of the following lines in your vimrc:
filetype on
filetype indent on
filetype plugin on
filetype indent plugin on " the order of 'indent' and 'plugin' is irrelevant
The first line only enables filetype detection.
The second line is like the first, plus filetype-specific indenting.
The third line is like the first, plus filetype-specific settings.
The fourth line enables everything.
Assuming you have either of the following lines:
filetype plugin on
filetype indent plugin on
you can create $HOME/vim/after/ftplugin/javascript.vim with the following content:
setlocal tabstop=2
setlocal shiftwidth=2
:setlocal is used instead of :set to make those settings buffer-local and thus prevent them to leak into other buffers.
the after directory is used to make sure your settings are sourced last.
Still assuming you have ftplugins enabled, there is nothing to do for Python as the default ftplugin already sets those options the way you want.
Related
At my work, I am required to follow the house style for indentation, which goes as follows:
2 spaces when coding html and ruby
tabs when coding javascript, with tabwidth=4 recommended
What is the best way to specify different whitespace preferences per filetype?
there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4
Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab
Also note:
You can make vim show tab characters by using :set list.
Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.
You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.
There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open.
It's very handy if you work with many files with different coding style.
I use an autocommand in my .vimrc:
:autocmd BufReadPost * :DetectIndent
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab
Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:
:set tabstop=2
ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces
My vim configuration uses the setting expandtab to replace tabs with spaces.
For some configuration files, for example the rsnapshot configuration file, I must use tabs rather than spaces.
Also please note that fields are separated by tabs, not spaces. The reason for this is so it's easier to specify file paths with spaces in them.
rsnapshot documentation
On my Ubuntu 16.04 and Debian 9.0 Server, the rsnapshot configuration file is stored on /etc/rsnapshot.conf.
How I can set up .vimrc to use tabs for only specific filename? For example rsnapshot.conf?
If you are asking how to configure whether you use hard tabs (tab characters) vs. spaces for indentation, based on filetype, you can do that using autocmd hooks.
For example, here is my base indent configuration for most files. It sets a tabstop of 8 (for hard tab display), and most tabs / indentation shifts are 4, and they use spaces, not hard tabs.
set tabstop=8 softtabstop=4 shiftwidth=4 expandtab
However, in some syntaxes that is wrong. For instance, I want 2 spaces in Javascript and YAML. And in Makefiles, space indent is a syntax error - there, I want hard tabs. You can set these things using autocmd and the filetype in question.
autocmd Filetype make setlocal noexpandtab
autocmd Filetype yaml setlocal shiftwidth=2 softtabstop=2
autocmd Filetype javascript setlocal shiftwidth=2 softtabstop=2
If you don't want to specify by file type you can also specify the filename (or a glob to match filenames).
autocmd BufNewFile,BufRead rsnapshot.conf setlocal noexpandtab
I'm trying to learn to use Vim. I like indenting by 4 spaces but use 2 spaces for certain languages, such as Nim and MoonScript. I tried adding this to my .vimrc:
autocmd BufNewFile,BufRead,BufEnter *.nim :setlocal tabstop=2 shiftwidth=2 softtabstop=2
Problem? It doesn't do anything! All that happens is that the tab does nothing when I press it! What am I doing wrong?
autocmd BufNewFile,BufRead *.nim setlocal tabstop=2 shiftwidth=2 softtabstop=2
This should work (after restarting Vim / re-editing an existing file via :e!) fine, but it mixes filetype detection with filetype settings. Vim has an intermediate abstraction called filetype, which you should use. With it, you map file globs like *.nim to a filetype nim, and then define settings either via an :autocmd FileType, or a filetype plugin in ~/.vim/ftplugin/nim.vim (for the latter, you need :filetype plugin on in your ~/.vimrc).
Steps
So, create a filetype detection in ~/.vim/ftdetect/nim.vim:
autocmd BufRead,BufNewFile *.nim setfiletype nim
Then, create a filetype plugin in ~/.vim/ftplugin/nim.vim:
setlocal tabstop=2 shiftwidth=2 softtabstop=2
You can check that these are loaded correctly via the :scriptnames output. After a restart of Vim, this should work, and you can add additional settings to the latter file. If your filetype is derived from another, you can also add :runtime! ftplugin/javascript.vim (for example) in there to get those settings, too.
Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.
You can add .vim files to be executed whenever vim switches to a particular filetype.
For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:
setlocal shiftwidth=2
setlocal tabstop=2
Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).
This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.
Use ftplugins or autocommands to set options.
ftplugin
In ~/.vim/ftplugin/python.vim:
setlocal shiftwidth=2 softtabstop=2 expandtab
And don't forget to turn them on in ~/.vimrc:
filetype plugin indent on
(:h ftplugin for more information)
autocommand
In ~/.vimrc:
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
I would also suggest learning the difference between tabstop and softtabstop. A lot of people don't know about softtabstop.
edit your ~/.vimrc, and add different file types for different indents,e.g. I want html/rb indent for 2 spaces, and js/coffee files indent for 4 spaces:
" by default, the indent is 2 spaces.
set shiftwidth=2
set softtabstop=2
set tabstop=2
" for html/rb files, 2 spaces
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
" for js/coffee/jade files, 4 spaces
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab
refer to: Setting Vim whitespace preferences by filetype
Put autocmd commands based on the file suffix in your ~/.vimrc
autocmd BufRead,BufNewFile *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile *.pl syntax on
The commands you're looking for are probably ts= and sw=
I usually work with expandtab set, but that's bad for makefiles. I recently added:
:autocmd FileType make set noexpandtab
to the end of my .vimrc file and it recognizes Makefile, makefile, and *.mk as makefiles and does not expand tabs. Presumably, you can extend this.
Personally, I use these settings in .vimrc:
autocmd FileType python set tabstop=8|set shiftwidth=2|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
This might be known by most of us, but anyway (I was puzzled my first time):
Doing :set et (:set expandtabs) does not change the tabs already existing in the file, one has to do :retab.
For example:
:set et
:retab
and the tabs in the file are replaced by enough spaces. To have tabs back simply do:
:set noet
:retab
For those using autocmd, it is a best practice to group those together. If a grouping is related to file-type detection, you might have something like this:
augroup filetype_c
autocmd!
:autocmd FileType c setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
:autocmd FileType c nnoremap <buffer> <localleader>c I/*<space><esc><s-a><space>*/<esc>
augroup end
Groupings help keep the .vimrc organized especially once a filetype has multiple rules associated with it. In the above example, a comment shortcut specific to .c files is defined.
The initial call to autocmd! tells vim to delete any previously defined autocommands in said grouping. This will prevent duplicate definition if .vimrc is sourced again. See the :help augroup for more info.
Today, you could try editorconfig, there is also a vim plugin for it. With this, you are able not only change indentation size in vim, but in many other editors, keep consistent coding styles.
Below is a simple editorconfig, as you can see, the python files will have 4 spaces for indentation, and pug template files will only have 2.
# 4 space indentation for python files
[*.py]
indent_style = space
indent_size = 4
# 2 space indentation for pug templates
[*.pug]
indent_size = 2
While you can configure Vim's indentation just fine using the indent plugin or manually using the settings, I recommend using a python script called Vindect that automatically sets the relevant settings for you when you open a python file. Use this tip to make using Vindect even more effective. When I first started editing python files created by others with various indentation styles (tab vs space and number of spaces), it was incredibly frustrating. But Vindect along with this indent file
Also recommend:
pythonhelper
python_match
python_ifold
In Lua (for Neovim users) you can use RUNTIMEPATH/ftplugin/*yourfiletype*.lua with options like:
vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2
Just be sure to use string values in quotes. For example:
vim.opt_local.foldmethod = 'marker'
I use a utility that I wrote in C called autotab. It analyzes the first few thousand lines of a file which you load and determines values for the Vim parameters shiftwidth, tabstop and expandtab.
This is compiled using, for instance, gcc -O autotab.c -o autotab. Instructions for integrating with Vim are in the comment header at the top.
Autotab is fairly clever, but can get confused from time to time, in particular by that have been inconsistently maintained using different indentation styles.
If a file evidently uses tabs, or a combination of tabs and spaces, for indentation, Autotab will figure out what tab size is being used by considering factors like alignment of internal elements across successive lines, such as comments.
It works for a variety of programming languages, and is forgiving for "out of band" elements which do not obey indentation increments, such as C preprocessing directives, C statement labels, not to mention the obvious blank lines.
At my work, I am required to follow the house style for indentation, which goes as follows:
2 spaces when coding html and ruby
tabs when coding javascript, with tabwidth=4 recommended
What is the best way to specify different whitespace preferences per filetype?
there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4
Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab
Also note:
You can make vim show tab characters by using :set list.
Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.
You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.
There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open.
It's very handy if you work with many files with different coding style.
I use an autocommand in my .vimrc:
:autocmd BufReadPost * :DetectIndent
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab
Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:
:set tabstop=2
ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces