views:

346

answers:

2

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
+6  A: 

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.

Michał Marczyk
...ok you posted the docs. I was just a couple seconds too late :P (so I removed my post)
Terence Honles
@Terence: Yeah, this happens not infrequently around here. I've been on the removing end all too often myself. ;-)
Michał Marczyk
+2  A: 

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:

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.

Jefromi
Jefromi,I don't understand your answer. vim is already selecting the python indentation plugin - the problem is that the plugin happens to indent illogically, removing indentation completely. So your proposed solution doesn't fix the problem as explained in the question.
Thomas Vander Stichele
`smartindent` will cause line-initial `#` to unindent regardless of whether `filetype indent` is turned on or not. So, you have to set `nosmartindent` to fix things anyway.
Michał Marczyk
Ah, right. I should clarify: I really meant using filetype indent *instead of* other indent settings.
Jefromi
And Thomas, I was thrown off by you saying that `autoindent` was working well - that's an option setting, separate from the filetype indent plugins (though I believe the python indent plugin enables autoindent, because it gets half the job done).
Jefromi
...and the fact that you didn't have any mention of `filetype` in your .vimrc.
Jefromi