Changing Vim indentation behavior by file type

vim filetype
vim autocmd filetype
vim change indent based on file type
vim auto-indent
vim filetype indent on
vim shiftwidth
vim ftplugin
vim settings per file type

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.

[SOLVED] vim, I tried to override this behavior by disabling plugins, adding "filetype off" to my /etc​/vimrc, removing /etc/vimrc completely and setting tabstop,  possible duplicate of Changing Vim indentation behavior by file type – acgtyrant Jul 12 '15 at 12:54 All these answers just made me more confused. The problem is that the options are up to preference.

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

You can replace any of the long commands or settings with their short versions: autocmd: au setlocal: setl shiftwidth: sw tabstop: ts softtabstop: sts expandtab: et

I would also suggest learning the difference between tabstop and softtabstop. A lot of people don't know about softtabstop.

how to make vim indentation file type specific?, Putting these in your .vimrc should do the trick: augroup FileTypeSpecificAutocommands autocmd FileType javascript setlocal tabstop=2 softtabstop=2  +1 to Nello. The tab character has a long established tradition of meaning a jump to the next position at a multiple of 8 chars. It is only because people wanted to use tab for "the next indent that looks good in my language" and because some text editors didn't bother making the difference between "adding a tab character" and "adding spaces for indenting", and people tweaked their editor to

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

How can I change the indent size?, Several settings controls the behavior: First set expandtab allows to replace the tabs by white spaces characters :h You could also be interested by by :h '​tabstop' which defines the number of spaces that a tab character in the file counts for. readable and since your don't need to type them that often in your config file  Avec cela, vous pouvez non seulement changer la taille de l'indentation dans vim, mais dans beaucoup d'autres éditeurs, garder des styles de codage cohérents. ci-dessous est un simple editorconfig, comme vous pouvez le voir, les fichiers python auront 4 espaces pour l'indentation, et les fichiers de template pug n'en auront que 2.

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=

VIM: Setting custom tab behaviour for some filetypes, To change the VIM tab settings for some file types, you can put something like this into ~/.vimrc (or _vimrc in your VIM directory if you are on  The indent features of Vim are very helpful for indenting source code. This tip discusses settings that affect indentation. These settings mostly affect the automatic indentation which Vim inserts as you type, but also can be triggered manually with the= operator, so that you can easily Fix indentation in your buffer. For indentation without tabs, the principle is to set'expandtab', and set

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.

Vim indent 4 spaces, Make vim indent 2 spaces for ruby and scala files only: filetype plugin indent on: To change this value, just add these lines to your vimrc file (usually located in a <Tab> (thus taking you to Making Indentation Behave Sanely (Electric Indent)​  By default vim aligns lines inside LI tags on the same position as the position of LI tag, but I want the contents of LI to have deeper indentation. Current behaviour: &lt;LI&gt; first line secon

Tabs, stop! The truth about tab and spaces in Vim by Federico "Lox , We'll walk through each setting connected to indentation in detail to This setting tells Vim how many columns a tab should be made up of in the editor This option results in different behaviours depending on its own value and the one set autocmd FileType ruby setlocal expandtab tabstop=2 shiftwidth=2 softtabstop=2. vim gedoens. Contribute to schultyy/vim-shortcuts development by creating an account on GitHub.

Vim, With Vigor, We can verify this by asking vim what the current filetype is: Vim provides a number of options for setting tabs and indentation. Then Vim will use a mix of tabs and spaces, but typing <Tab> and <BS> will behave like a tab appears every 4  Vim - Set indentation based on file type. Feb 5, 2013 | Hits. Let say now you want your .js (JavaScript) Reference: Changing Vim indentation behavior by file type.

Creating ~/.vimrc disables syntax highlighting, Your system default vimrc no longer gets loaded when you create your own (and that's as it should be). You also won't get filetype based  Changing Vim indentation behavior by file type . 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…

Comments
  • BTW - PEP8 convention for Python says the tabstop should be 4 spaces and tabs should be 4 spaces. ref: stackoverflow.com/questions/120926/…
  • You should put that in ~/.vim/after/ftplugin/html.vim instead. But as others have pointed out below, it’s much nicer to just add autocmd FileType html setlocal shiftwidth=2 tabstop=2 to your .vimrc.
  • Whoops, actually, that /is/ where I have that file. I'll fix the answer. I disagree though, I think separating out commands for different filetypes into separate files makes everything much easier, especially if you have requirements for many filetypes, or lots of options for some filetypes.
  • Actually, there's not much reason to use the after directory for ftplugins. Vim will load all of them it finds in your runtimepath, not just the first like for syntax files.
  • FYI: don't use js for javascript filetype. Use javascript instead. (autocmd FileType javascript setlocal shiftwidth=2 tabstop=2)
  • You need to add filetype plugin on to your vimrc too.
  • Thanks! Also thanks for that bit about 'ts' and 'sts'. Are there any particular pages that you'd recommend that discuss this difference and how to use it?
  • @jvriesem There's not much to it: 'ts' is how tab characters are displayed; 'sts' is how many "spaces" to insert when the tab key is pressed ; 'sw' is how many "spaces" to use per indent level; 'et' is whether to use spaces or tabs; 'sta' lets you insert 'sw' "spaces" when pressing tab at the beginning of a line.
  • I wonder if the full forms would be better to use, for clarity, rather than the curt sentence at the end.
  • sw is short for softwidth, sts is short for softtabstop, et is short for expandtab, setl is short for setlocal, and au is short for autocmd. You can use the long forms instead of the short forms.
  • I believe sw expands to shiftwidth rather than softwidth.
  • Comment for vimrc is single " :)
  • @sdkks I don't think so . double quote is comment, single quote ' gives error on all my Linux's vim . (7.3+, 8.0...)
  • Yeah. My comment says use a single ", meaning don't close it with another ". Not sure why I commented that though.