Whenever I want to add a comment to an indented line in vim, I hit Shift-o (open a new row above the current, switch to insert mode) and start typing a Python comment (using #
). That hash is then magically moved to the start of the line (no indentation) and I have to click tab a few times.
每当我想向vim中的缩进行添加注释时,我就按Shift-o(在当前行之上打开新行,切换到insert mode)并开始输入Python注释(使用#)。然后该散列被神奇地移动到行的开头(没有缩进),我必须多次单击tab。
Anyone know how to work around it?
有人知道如何应对吗?
3 个解决方案
#1
56
I suppose you have set smartindent
in your .vimrc
我想你已经在你的。vimrc中加入了smartindent
See :h smartindent
看: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.
I believe you don't need smartindenting while coding python. So just remove it from your settings or add the following to your .vimrc:
我相信您在编写python时不需要修改代码。所以,把它从你的设置中删除或者添加到你的。vimrc中:
au! FileType python setl nosmartindent
#2
9
try putting that in your .vimrc:
试着把它放在你的。
autocmd BufRead *.py inoremap # X<c-h>#
This will make that the insertion of the hash (pound) sign is always indented in Python source files.
这将使得在Python源文件中插入散列(磅)符号总是缩进。
#3
1
You may want to try Nerd Commenter, which is a plugin that allows you to add comments to lines in most languages automatically. You simply place the cursor at the line you are interested in and type ,cSpace and the line will be commented out. The same keystrokes will remove the comment to reveal the line.
您可能想尝试Nerd Commenter,它是一个插件,允许您在大多数语言中自动添加注释。您只需将光标放在感兴趣的行上,输入cSpace,该行就会被注释掉。相同的击键将删除注释,以显示该行。
So if you have:
所以如果你有:
def func():
print("indented") <- cursor position in command mode
Type ,cSpace and you get:
输入cSpace,得到:
def func():
#print("indented")
#1
56
I suppose you have set smartindent
in your .vimrc
我想你已经在你的。vimrc中加入了smartindent
See :h smartindent
看: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.
I believe you don't need smartindenting while coding python. So just remove it from your settings or add the following to your .vimrc:
我相信您在编写python时不需要修改代码。所以,把它从你的设置中删除或者添加到你的。vimrc中:
au! FileType python setl nosmartindent
#2
9
try putting that in your .vimrc:
试着把它放在你的。
autocmd BufRead *.py inoremap # X<c-h>#
This will make that the insertion of the hash (pound) sign is always indented in Python source files.
这将使得在Python源文件中插入散列(磅)符号总是缩进。
#3
1
You may want to try Nerd Commenter, which is a plugin that allows you to add comments to lines in most languages automatically. You simply place the cursor at the line you are interested in and type ,cSpace and the line will be commented out. The same keystrokes will remove the comment to reveal the line.
您可能想尝试Nerd Commenter,它是一个插件,允许您在大多数语言中自动添加注释。您只需将光标放在感兴趣的行上,输入cSpace,该行就会被注释掉。相同的击键将删除注释,以显示该行。
So if you have:
所以如果你有:
def func():
print("indented") <- cursor position in command mode
Type ,cSpace and you get:
输入cSpace,得到:
def func():
#print("indented")