参见英文答案 >
How to configure vim to not put comments at the beginning of lines while editing python files7答案我使用Vim和编辑Python脚本。
自动缩进工作相当不错,一般来说,但是当我开始一个新行,并键入’#’键入一个注释,Vim unindents行为我。
例如,如果有
def foo():
和我按enter键,Vim将缩进正确
def foo(): pass
但是,如果不是键入pass,我键入#,它自动取消缩进
def foo(): # comment class Thing(): def __init__(self): pass # comment line gets unindented all the way
我的.vimrc文件如下。任何人都知道为什么会发生这种情况?
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
设置smartindent使Vim的行为像你描述的我,而使用nosmartindent(这是我倾向于使用),它的行为像你喜欢它。
更新:从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.
这似乎是。
更新:可能不需要打扰以下…我会离开这里为增加信息值。