如何在 Vim 中注释掉 Python 代码块
我想知道 Vim 中是否有任何键映射允许我缩进某些代码行(无论这些行是在可视模式下选择的,还是当前光标位置上方/下方的 n 行)。
因此,基本上将以下内容转换
def my_fun(x, y):
return x + y
为
#def my_fun(x, y):
# return x + y
我可以使用 #
或 """
来注释相关行。理想情况下,我也希望使用相同的键映射如果给定的行已被注释掉,请取消注释这些行。
I was wondering if there was any key mapping in Vim to allow me to indent certain lines of code (whether those lines have been selected in visual mode, or n lines above/below current cursor position).
So basically something that converts the following
def my_fun(x, y):
return x + y
to
#def my_fun(x, y):
# return x + y
I am okay with using either #
or """
for commenting out the relevant lines. Ideally, I would also like the same keymapping to uncomment the lines if the given lines have been commented out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
第 1 步:转到您要评论的第一行的第一列。
第 2 步: 按:Ctrl +v 并选择要评论的行:
第 3 步: Shift-
I
#space (进入左侧插入模式,输入要插入的字符。选择将消失,但其中的所有行将在步骤 4 后修改。)第 4 步: Esc
Step 1: Go to the the first column of the first line you want to comment.
Step 2: Press: Ctrl+v and select the lines you want to comment:
Step 3: Shift-
I
#space (Enter Insert-at-left mode, type chars to insert. The selection will disappear, but all lines within it will be modified after Step 4.)Step 4: Esc
手动一种方式
one way manually
您可以将以下映射添加到您的 .vimrc
突出显示您的块:
#
以注释第一列中的行。-#
以同样的方式取消注释。You could add the following mapping to your .vimrc
Highlight your block with:
#
to comment your lines from the first column.-#
to uncomment the same way.使用以下命令突出显示您的块:ShiftV
使用以下命令注释所选块:
:norm i#
(小写 i)要取消注释,请突出显示您的块再次,并取消注释:
:norm ^x
:norm
命令对每个选定的行执行一个操作。注释将在每行的开头插入一个#
,取消注释将删除该#
。Highlight your block with: ShiftV
Comment the selected block out with:
:norm i#
(lower case i)To uncomment, highlight your block again, and uncomment with:
:norm ^x
The
:norm
command performs an action for every selected line. Commenting will insert a#
at the start of every line, and uncommenting will delete that#
.我通常会扫出一个可视块 (
),然后搜索第一个字符并将其替换为:(使用选定的可视块进入命令模式会自动将 '<,'> 放在命令行)然后我可以通过清除相同的视觉块来取消注释该块,并且:
I usually sweep out a visual block (
<C-V>
), then search and replace the first character with:(Entering command mode with a visual block selected automatically places '<,'> on the command line) I can then uncomment the block by sweeping out the same visual block and:
有一些很好的插件可以帮助注释/取消注释行。例如书呆子评论者。
来自 NERD 评论者的快捷方式示例:
完整文档位于此处 。
There are some good plugins to help comment/uncomment lines. For example The NERD Commenter.
Sample shortcuts from the NERD Commenter:
Full documentation is located here.
无需插件或映射。尝试使用内置的“norm”命令,该命令实际上可以在每个选定的行上执行您想要的任何操作。
添加 # 条评论
删除 # 条评论
或者,如果您的评论缩进,您可以执行
:norm ^x
请注意,这些只是前面的普通 vim 命令通过“:norm”在每一行执行它们。
在此处的答案之一中使用“norm”命令的更详细答案
在 Vim 中注释/取消注释行的快速方法是什么?
No plugins or mappings required. Try the built-in "norm" command, which literally executes anything you want on every selected line.
Add # Comments
Remove # Comments
Or if your comments are indented you can do
:norm ^x
Notice that these are just ordinary vim commands being preceded by ":norm" to execute them on each line.
More detailed answer for using "norm" command in one of the answers here
What's a quick way to comment/uncomment lines in Vim?
我的
.vimrc
中有以下几行:只要它们以
#
开头(# 后面有空格),快捷键就会保留您的光标位置和注释。例如:注释后:
取消注释后:
I have the following lines in my
.vimrc
:The shortcuts preserve your cursor position and your comments as long as they start with
#
(there is space after #). For example:After commenting:
After uncomennting:
NERDcommenter 是一个优秀的评论插件,它可以自动检测许多文件类型及其关联的评论字符。使用 Pathogen 安装非常简单。
使用
cc
进行评论。使用cu
取消注释。并使用c
切换注释。(vim 中默认的
键是\
)NERDcommenter is an excellent plugin for commenting which automatically detects a number of filetypes and their associated comment characters. Ridiculously easy to install using Pathogen.
Comment with
<leader>cc
. Uncomment with<leader>cu
. And toggle comments with<leader>c<space>
.(The default
<leader>
key in vim is\
)坦率地说,我为该链接使用了一个tcomment插件。它可以处理几乎所有语法。它定义了很好的动作,将它与一些特定于 python 的文本块匹配器一起使用使其成为一个强大的工具。
Frankly I use a tcomment plugin for that link. It can handle almost every syntax. It defines nice movements, using it with some text block matchers specific for python makes it a powerful tool.
vim 有很多评论插件 - 其中一些是多语言的 - 不仅仅是 python。如果您使用像 Vundle 这样的插件管理器,那么您可以搜索它们(一旦您安装了 Vundle),使用例如:
您将得到一个结果窗口。或者,您可以在 vim-scripts 中搜索评论插件。
正如其他答案中提到的 NERDCommenter 是一个不错的选择 - 有关使用它的更多信息,请参阅 这个答案。注意:
键通常是 \。例如,注释掉一行 - 输入: \ccThere's a lot of comment plugins for vim - a number of which are multi-language - not just python. If you use a plugin manager like Vundle then you can search for them (once you've installed Vundle) using e.g.:
And you will get a window of results. Alternatively you can just search vim-scripts for comment plugins.
As mentioned in other answers NERDCommenter is good one - for more info on using it see this answer. Note: That the
<leader>
key is usually \. E.g. so to comment out a line - type: \cc一个非常小的轻量级插件:vim-commentary。
gcc 注释一行
gcgc 取消注释。查看插件页面了解更多信息。
v+k/j 突出显示该块,然后 gcc 注释该块。
A very minimal light weight plugin: vim-commentary.
gcc to comment a line
gcgc to uncomment. check out the plugin page for more.
v+k/j highlight the block then gcc to comment that block.
CtrlK 进行注释(可视模式):
CtrlU 进行注释取消注释(视觉模式):
CtrlK for comment (Visual Mode):
CtrlU for uncomment (Visual Mode):