tags:

views:

283

answers:

3

Hey.

Whenever I want to add a comment to an indented line in vim, I click 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?

A: 
Pierre-Antoine LaFayette
+2  A: 

try putting that in your .vimrc:

autocmd BufRead *.py inoremap # X<c-h>#

This will make that the insertion of the hash (pound) sign is always indented in Python source files.

Haes
+3  A: 

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
Maxim Kim
That answer is obviously right on the money, thanks a lot. I had missed this completely.However, I wouldn't agree on the fact that I don't need the smartindenting since python is all about it. I can just add the exception, instead.I wonder, though, which is the best, the manual's inoremap or the one provided by Haes? Or rather, what's the difference?
Jonatan Littke
I think that for python `smartindent` is useless. Python programmer don't need an indent to be auto-inserted 1. after a line ending in '{'; 2. before a line starting with '}'. And keywords from `cinwords` are correctly processed by python filetype indentation.
Maxim Kim
Basicly inoremaps are the same. The only drawback this inoremap has -- you can't shift comments to the right with `>>`
Maxim Kim