created January 27, 2004 · complexity basic · author stsi · version 6.0
In 'smartindent' mode, '#' removes the indent if it is the first char on the line. That can be very annoying.
From Vim's help:
- 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.
This helps, but it fails to work when placed in vimrc.
The following mapping works in any case:
:inoremap # a#^Oh^Ox^OA, where ^O is entered with CTRL-V CTRL-O.
Comments
Too bad that map doesn't work when there are characters on the line after where you are trying to insert the #, then you wind up with an "a" instead. Instead try
:inoremap # a#<Left><BS><Right>
Just do as the help on smartindent states and add the following line to your .vimrc file:
:inoremap # X<C-H>#
Nice, but does not help for indenting with '>>' or '<<'. The lines are still ignored.
You can avoid all this nastiness by just not enabling 'smartindent'. It's not very smart and really only makes sense for C-like languages.