26

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.

5 Answers 5

10

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
2
  • Wow! OP changed their selected answer. I'm honored. Sep 26, 2019 at 13:49
  • 2
    2 years later an I find my own answer via DDG search. StackExchange is my blog. Feb 22, 2021 at 18:30
10

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.

3
  • I was able to skip the last two lines and still solve the problem
    – iconoclast
    Dec 25, 2013 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.
    – phemmer
    Dec 25, 2013 at 10:07
  • 4
    OMG!!! set indentkeys-=0#!! FINALLY MY SUFFERING IS OVER!!!!!!!!
    – Kasapo
    Nov 26, 2018 at 16:19
3

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>#
2

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

1
  • This doesn't "answer" the question, when there could be clearly given answers like above, should've been a comment to the OP instead. Would be much helpful there on top as a comment. Aug 28, 2021 at 18:08
-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.