Changing Vim indentation behavior by file type
vim autocmd filetype
vim change indent based on file type
vim filetype indent on
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.
setlocal shiftwidth=2 softtabstop=2 expandtab
And don't forget to turn them on in
filetype plugin indent on
:h ftplugin for more information)
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
I would also suggest learning the difference between
softtabstop. A lot of people don't know about
~/.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:
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: <LI> 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.
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…
- 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.viminstead. But as others have pointed out below, it’s much nicer to just add
autocmd FileType html setlocal shiftwidth=2 tabstop=2to your
- 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.
- You need to add
filetype plugin onto 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.
swis short for
stsis short for
etis short for
setlis short for
auis short for
autocmd. You can use the long forms instead of the short forms.
- I believe
- 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.