How to configure vim to not put comments at the beginning of lines while editing python files How to configure vim to not put comments at the beginning of lines while editing python files python python

How to configure vim to not put comments at the beginning of lines while editing python files


I found an answer here http://vim.wikia.com/wiki/Restoring_indent_after_typing_hash

It seems that the vim smartindent option is the cause of the problem.The referenced page above describes work-a-rounds but after reading the help in smartindent in vim itself (:help smartindent), I decided to try cindent instead of smartindent.

I replaced

set smartindent

with

set cindent

in my .vimrc file

and so far it is working perfectly.

This changed also fixed the behavior of '<<' and '>>' for indenting visual blocks that include python comments.

There are more configuration options for and information on indentation in the vim help for smartindent and cindent (:help smartindent and :help cindent).


@PolyThinker Though I see that response a lot to this question, in my opinion it's not a good solution. The editor still thinks it should be indented all the way to left - check this by pushing == on a line that starts with a hash, or pushing = while a block of code with comments in it is highlighted to reindent.

I would strongly recommend filetype indent on, and remove the set smartindent and set autoindent (or set cindent) lines from your vimrc. Someone else (appparently David Bustos) was kind enough to write a full indentation parser for us; it's located at $VIMDIRECTORY/indent/python.vim.

(Paul's cindent solution probably works for python, but filetype indent on is much more generally useful.)


I have the following lines in my .vimrc, seems to be installed by default with my Ubuntu 8.10

set smartindentinoremap # X^H#set autoindent

And I don't observe the problem. Maybe you can try this. (Note that ^H should be entered by Ctrl-V Ctrl-H)