I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove filetype indent on
from your vimrc!
This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.
File Specific Indent Settings
So if you're like me, you probably had filetype indent on
in your vimrc and had no idea what it was doing.
All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
The first place holds the default indent rules that come with vim. If you were to set filetype indent on
on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file called index.html
in would get the rules from $VIMRUNTIME/indent/html.vim
.
In my experience, these default rules are pretty darn good, but they can get messed up by other settings.
The second place (the after
directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.
Flavors of Indentation
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
autoindent
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
I use filetype indent on
and set autoindent
in my vimrc, since they work well together. I don't have the others set.
smartindent & cindent
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.
Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.
indentexpr
Runs filetype indent scripts found in (vimfolder)\indent\\(indentscripts)
. It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):
Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.
Troubleshooting
There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily verbose
will tell you which file was the last to change the option in question.
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
You may get a result such as
indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim
If that happens, you can move that file, close and open vim, and see if it fixes your problem.
Turning Off Indent Settings for TeX
Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the after
directory.
I don't know much about Tex or LaTex, but when I created a file with the .tex
extension and ran :filetype
it had the filetype as plaintex
. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim
. In that file:
set autoindent&
set cindent&
set smartindent&
set indentexpr&
This will set all these values to their defaults whenever you open a .tex
file.
Accolade
is a somewhat obscure term for curly braces ({
,}
); it's probably more common in Europe than in the US. – Suavitynoautoindent
,nocindent
andsmartindent
. I'm not quite sure why this works the best, but trial and error shows that this is the most satisfactory. – Sipeindentexpr
, which if set, overridessmartindent
andcindent
. – Wheat