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
vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.
autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/style of the code you are editing (especially for C). When having it on you also should have autoindent on.
Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With