12

For some reason Vim thinks all of my comments should have all indentation removed. If I add # to the beginning of a line, suddenly all leading whitespace is removed. How can I stop this?

I have Janus and NERDtree installed, in case it's relevent, along with copious other personal configurations in my .vimrc, but none that (as far as I know) would cause such behavior.

3

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
  • Wow! OP changed their selected answer. I'm honored. – Bruno Bronosky Sep 26 at 13:49
6

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 was able to skip the last two lines and still solve the problem – iconoclast Dec 25 '13 at 3:50
  • 1
    @iconoclast note though that you may still have issues with some types of files. The last line is because some syntax files override indentation settings. – Patrick Dec 25 '13 at 10:07
  • 1
    OMG!!! set indentkeys-=0#!! FINALLY MY SUFFERING IS OVER!!!!!!!! – Kasapo Nov 26 '18 at 16:19
2

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

2

ref this answer:

http://vim.wikia.com/wiki/Restoring_indent_after_typing_hash

modify to:

set nosmartindent
set cindent
set cinkeys-=0#
set indentkeys-=0#

or:

:inoremap # X<BS>#
-1

I have solved the problem by commenting set smartindent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.