注释(#)在Vim中以插入模式进入行首

Jon*_*tke 57 python vim comments indentation

每当我想在vim中为缩进行添加注释时,我点击Shift- o(在当前上方打开一个新行,切换到插入模式)并开始键入Python注释(使用#).然后,该哈希神奇地移动到行的开头(没有缩进),我必须单击选项卡几次.

有谁知道如何解决它?

Max*_*Kim 64

我想你set smartindent的.vimrc中有

看到 :h 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.
Run Code Online (Sandbox Code Playgroud)

我相信你在编写python时不需要smartindenting.所以只需将其从您的设置中删除或将以下内容添加到.vimrc:

au! FileType python setl nosmartindent
Run Code Online (Sandbox Code Playgroud)

  • 我知道这是旧的,但是对于Python来说:"smartindent",请参阅http://stackoverflow.com/a/18415867/1858225 (4认同)
  • 我认为对于python`smartindent`是没用的.Python程序员不需要自动插入缩进1.在以'{'结尾的行之后; 2.在以'}'开头的行之前.来自`cinwords`的关键字由python filetype缩进正确处理. (3认同)

Hae*_*aes 9

试着把它放在.vimrc中:

autocmd BufRead *.py inoremap # X<c-h>#
Run Code Online (Sandbox Code Playgroud)

这将使得散列(磅)符号的插入总是在Python源文件中缩进.