Vim automatically removes indentation on Python comments

PythonVimIndentation

Python Problem Overview


I'm using Vim and editing Python scripts.

Autoindent works pretty well in general, but when I start a new line and type '#' to type a comment, Vim unindents that line for me.

For example, if have

def foo():

and I press enter, Vim will indent properly

def foo():
    pass

but, if instead of typing pass, I type #, it unindents automatically

def foo():
# comment

class Thing():
    def __init__(self):
         pass
# comment line gets unindented all the way

my .vimrc file follows. anyone know why this is happening?

set tabstop=4
set smartindent
set shiftwidth=4
set expandtab
set backspace=indent,eol,start
set scrolloff=3
set statusline=%f%m%r%h%w\ [%Y\ %{&ff}]\ [%l/%L\ (%p%%)]
set laststatus=2

Python Solutions


Solution 1 - Python

Setting smartindent on makes Vim behave like you describe for me, whereas with nosmartindent (which is what I tend to use) it behaves like you'd prefer it to.

Update: From the docs on 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.

That seems to be it.


Update: Probably no need to bother with the following... I'll leave it here for the added informational value. ;-)

If setting nosmartindent doesn't help, perhaps you could use the :set command -- with no parameters -- to obtain the list of all settings in effect in your Vim session, then paste it somewhere (on Pastie perhaps). There's a few other options which affect automatic indentation, as far as I remember.

Solution 2 - Python

While Michał's post explains what smartindent does, you can do a lot better than just turning it off. You could configure it more to your liking, or better yet, let Vim pick better indentation for you. With the following in your vimrc instead of other indent settings:

filetype indent on

Vim will automatically use the proper indent plugin for python. This is way better than just not de-indenting a # line - pretty much everything should be properly indented.

Solution 3 - Python

You can try an option only for python files:

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

Solution 4 - Python

For some unkown reason the above behavior was caused when i had cindent on. Turning it off fixed it for me. None of the other fixes above helped.

Solution 5 - Python

If you install this script, you will get proper python (pep8) indenting:

http://www.vim.org/scripts/script.php?script_id=974

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandyleiView Question on Stackoverflow
Solution 1 - PythonMichał MarczykView Answer on Stackoverflow
Solution 2 - PythonCascabelView Answer on Stackoverflow
Solution 3 - PythonzzartView Answer on Stackoverflow
Solution 4 - PythonMichael HärtlView Answer on Stackoverflow
Solution 5 - PythonChronialView Answer on Stackoverflow