Stop vim from messing up my indentation on comments

This is a battle I fought as well, and think I finally won. The problem is that there are a dozen different ways the behavior can be overridden (by plugins/syntaxes).
Here's all the settings I had to use to win the battle:

set nosmartindent
set cindent
filetype plugin indent on
set cinkeys-=0#
set indentkeys-=0#
autocmd FileType * set cindent "some file types override it

With the autocmd, the first set cindent shouldn't be necessary, but this is one of those things where I kept adding lines until the behavior went away.


I'm never satisfied with the "set all these things just in case" answers. I insist on knowing which of those things were set wrong and how they got set. Here is a command that will show you the values of all the relevant settings mentioned here, as well as where they were last set. If no Last set from line follows, it is a default value. No point in re-setting correct default values.

:verbose set autoindent? smartindent? cindent? cinkeys? indentexpr?

noautoindent
nosmartindent
  cindent
        Last set from ~/.vim/vimrc
  cinkeys=0{,0},0),:,!^F,o,O,e
        Last set from ~/.vim/vimrc
  indentexpr=
Press ENTER or type command to continue

See: http://vimdoc.sourceforge.net/htmldoc/options.html#:set-verbose

All I had to do to get that working was remove set smartindent from a plugin and add to my ~/.vim/vimrc:

set cindent cinkeys-=0#
set expandtab shiftwidth=4 tabstop=4 softtabstop=4

You might want to check out the Vim Wiki Indenting source code page and checkout how 'autoindent' works.

Tags:

Vi

Vim