Take the 2-minute tour ×

When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.

For example, when writing this in vim

for i in range(10):

the # does not stay there where I entered it.

It is moved like so, by vim.

for i in range(10):

Does anyone know of a configuration item in vim that would change this?

If it helps, I am using Ubuntu 8.10.

share|improve this question
See also: stackoverflow.com/questions/191201/… –  Dan May 13 '10 at 23:39
add comment

8 Answers

up vote 17 down vote accepted

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


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).

share|improve this answer
Ah, I have "filetype plugin indent on" in my .vimrc, perhaps that's responsible for it working correctly for me. vimdoc.sourceforge.net/htmldoc/filetype.html –  m0j0 Dec 9 '08 at 22:38
Saved my day. +1 ! :) –  Ory Band May 7 at 19:55
add comment

PolyThinker: Though I see that response a lot to this question, 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.

The real solution is 'filetype indent on', and remove the smartindent and autoindent (or cindent) lines in 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)

share|improve this answer
Thanks - this helped me out. I had one hitch, and that's I was trying "filetype indent on" within gvim (MacVim) and saw no behavior change. Once I started testing it by adding it to .vimrc, then restarting, I saw behavior change. –  finity Oct 31 '12 at 21:29
add comment

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

set smartindent
inoremap # 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)

share|improve this answer
add comment

It's caused by the 'smartindent' feature. If you have :set smartindent in your .vimrc you need to remove it.

share|improve this answer
add comment

My Vim configuration doesn't do that. You might try the python.vim script available from this link: http://www.vim.org/scripts/script.php?script_id=790

share|improve this answer
Unfortunately, that did not work in my case, the problem persists while using it. I used version 2.6.3 because I have not yet upgraded to python 3.0. Perhaps the problem is in the /usr/share/vim/vim71/indent/python.vim file installed from the Ubuntu vim-runtime package? –  Paul D. Eden Dec 9 '08 at 21:50
add comment

@nevinera: I was having the same problem as the others (Python comments appearing at the first column when entered as the first character of a new line), but only your method fixed the problem in my case (I'm running version 7.2 on OS X 10.4).

share|improve this answer
add comment

My solution to the unindenting of #:

If you use cindent, recognize that it is designed for C and C++ coding. Here, a # means you are creating a #DEFINE or #MACRO(), so the behavior is correct. But for other languages where # is a comment, it is irritating.

The following worked for me:

" cindent       enable specific indenting for C code
" set cin       nocin
set cin

" cinkeys       The default cinkeys causes leading # to unindent to column 0.
"               To prevent this, remove the 0# from the definition.
" set cinkeys=0{,0},0),:,0#,!^F,o,O,e - default
set cinkeys=0{,0},0),:,!^F,o,O,e
share|improve this answer
add comment

I removed set smartindent from ~/.vimrc but it still didn't disable smartindent. When I opened a .py file and ran :set smartindent? it displayed smartindent.

Turns out that further down in the ~/.vimrc was this line:


Once I deleted "smartindent" from that line, then smartindent was finally disabled and my comments were indented properly again.

share|improve this answer
add comment

Your Answer


