简体   繁体   English

如何评论/取消评论 vim 中的单行

[英]How to comment/uncomment a single line in vim

I'm new with vim and working on Mac with python, and want to know how to comment/uncomment a single line with vim. Most of the IDEs that I've worked with use Cmd+/ it there something similar?我是 vim 的新手,在 Mac 上使用 python 工作,想知道如何使用 vim 评论/取消注释单行。我使用过的大多数 IDE 都使用 Cmd+/它有类似的东西吗? I tried to find the answer but everything I found was about multiple lines!我试图找到答案,但我发现的一切都是关于多行的!

Comments in Python are prefixed with # Python 中的评论以#为前缀

Working in vim you will need to insert # at some point where you think it's appropriate.在 vim 中工作,您需要在您认为合适的位置插入 #。

Go to that point then type: i #ESC...where ESC is the escape key Go 到那个点然后输入: i #ESC...其中 ESC 是转义键

To delete a comment you'll want to search for #要删除评论,您需要搜索#

Type: /#类型: /#

This will take you to the first occurrence of # from your current position. Once you've established that you've found the comment that you want to delete then...这会将您带到当前 position 中第一次出现的#。一旦您确定您已经找到要删除的评论,然后...

Type: D类型:D

Here are some of the native ways to comment the current line in Python:以下是一些在 Python 中注释当前行的原生方法:

I#<Esc> 
:s/^/#<CR>

See :help I , :help:s , and :help /^ .请参阅:help I:help:s:help /^

And here are some of the native ways to uncomment it:以下是一些取消注释的原生方法:

^x
:s/^#<CR>

See :help ^ and :help x .请参阅:help ^:help x

Vim doesn't have a native equivalent of that shortcut you are used to and the native way to handle comments is not even remotely as efficient as that shortcut. Vim 没有您习惯使用的快捷方式的本机等效项,并且处理评论的本机方式甚至远不如该快捷方式有效。 That is why there have been so many plugins written in the past decades to address such a common need: NERD Commenter , Commentary , etc. I would suggest you pick one and move on.这就是为什么在过去的几十年里编写了如此多的插件来满足这样一个普遍的需求: NERD CommenterCommentary等。我建议你选择一个然后继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM