Vim - 视觉块:删除而不是插入
我经常使用视觉块,然后在注释掉大量代码时插入多行。这对于在多行的同一位置插入文本非常有用,但我不知道如何稍后使用可视块模式删除此文本,Backspace、Del 和 d 都不起作用。我正在使用 MacVim。
I often use visual block then inserting on multiple lines when for example commenting out a lot of code. This is great for inserting text in the same position on multiple lines but I can't figure out how to delete this text later using visual block mode, Backspace, Del and d all don't work. I am using MacVim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找 x:
然后是 Visual-block-select,
x
:我经常使用它,出于完全相同的原因 - 注释和取消注释大块代码。
You're looking for x:
Then visual-block-select,
x
:I use this frequently, for exactly the same reason -- commenting and uncommenting large blocks of code.
这并没有直接回答这个问题(sarnold 已经这样做了),但我建议有更有效的方法来(取消)注释代码块。我有一个 CommentToggle 函数,它可以注释或取消注释当前行,具体取决于它是否以“comchar”开头。
因此,对于 perl 文件,您可以映射:
并按 3 \ c (un-) 注释从光标位置开始的三行。
您还可以编写视觉模式映射:
允许您选择一个视觉区域并按 \c 来(取消)注释它们。
这一特殊函数仅适用于单字符注释(“#”、“%”等),但可以直接将其扩展到更长的字符串(例如“//”),甚至更复杂的替换,例如 HTML评论。
希望这有帮助。
This isn't directly answering the question (sarnold has already done so), but I would suggest there are more efficient ways of (un-)commenting code blocks. I have a CommentToggle function which either comments or uncomments the current line, depending on whether or not it begins with the "comchar".
So, for perl files you can map:
and pressing 3 \ c (un-)comments three lines from the cursor position.
You can also write a visual-mode mapping:
allowing you to select a visual region and press \c to (un-)comment them all.
This particular function only works for one-character comments ("#", "%", etc.), but it is straightforward to extend it to longer strings (e.g. "//"), and even more complex replacements, such as HTML comments.
Hope this helps.
古拉什王子的答案与前导选项卡不一致。
我更改了它,将制表符添加到模式中,尽管在注释和取消注释后行会失去缩进。
我更喜欢将注释字符添加到行中的第一个位置,对 Prince Goulash 函数的修改可以达到目的:
Prince Goulash's answer doesn't work in lines with leading tabs.
I changed it, adding the tab character to the pattern, although lines lose their indent after comment and uncomment.
I like more adding the comment char to first position in line, this modification to Prince Goulash's function does the trick: