Comments (#) go to start of line in the insert mode in Vim

Jonatan Littke picture Jonatan Littke · Jan 14, 2010 · Viewed 9.4k times · Source

Whenever I want to add a comment to an indented line in vim, I hit Shift-o (open a new row above the current, switch to insert mode) and start typing a Python comment (using #). That hash is then magically moved to the start of the line (no indentation) and I have to click tab a few times.

Anyone know how to work around it?

Answer

Maxim Kim picture Maxim Kim · Jan 14, 2010

I suppose you have set smartindent in your .vimrc

See :h smartindent

When typing '#' as the first character in a new line, the indent for
that line is removed, the '#' is put in the first column.  The indent
is restored for the next line.  If you don't want this, use this
mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
When using the ">>" command, lines starting with '#' are not shifted
right.

I believe you don't need smartindenting while coding python. So just remove it from your settings or add the following to your .vimrc:

au! FileType python setl nosmartindent