Vim - 视觉块:删除而不是插入

发布于 2024-11-18 20:45:31 字数 111 浏览 2 评论 0原文

我经常使用视觉块,然后在注释掉大量代码时插入多行。这对于在多行的同一位置插入文本非常有用,但我不知道如何稍后使用可视块模式删除此文本,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人的歌 2024-11-25 20:45:32

您正在寻找 x

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh

然后是 Visual-block-select,x

root:/root:/bin/bash
daeaemon:/usr/sbin:/bin/sh
bin/bin:/bin/sh
sys/dev:/bin/sh

我经常使用它,出于完全相同的原因 - 注释和取消注释大块代码。

You're looking for x:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh

Then visual-block-select, x:

root:/root:/bin/bash
daeaemon:/usr/sbin:/bin/sh
bin/bin:/bin/sh
sys/dev:/bin/sh

I use this frequently, for exactly the same reason -- commenting and uncommenting large blocks of code.

夏见 2024-11-25 20:45:32

这并没有直接回答这个问题(sarnold 已经这样做了),但我建议有更有效的方法来(取消)注释代码块。我有一个 CommentToggle 函数,它可以注释或取消注释当前行,具体取决于它是否以“comchar”开头。

function! CommentToggle(comchar)
    let firstchar = matchstr(getline("."),"[^ ]")
    if firstchar == a:comchar
        sil exe 'normal ^xx'
    else 
        sil exe 'normal ^i' . a:comchar . ' '
    endif
endfunction

因此,对于 perl 文件,您可以映射:

nnoremap <silent> <leader>c :call CommentToggle('#')<CR>

并按 3 \ c (un-) 注释从光标位置开始的三行。

您还可以编写视觉模式映射:

vnoremap <silent> <leader>c :call CommentToggle('#')<CR>

允许您选择一个视觉区域并按 \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".

function! CommentToggle(comchar)
    let firstchar = matchstr(getline("."),"[^ ]")
    if firstchar == a:comchar
        sil exe 'normal ^xx'
    else 
        sil exe 'normal ^i' . a:comchar . ' '
    endif
endfunction

So, for perl files you can map:

nnoremap <silent> <leader>c :call CommentToggle('#')<CR>

and pressing 3 \ c (un-)comments three lines from the cursor position.

You can also write a visual-mode mapping:

vnoremap <silent> <leader>c :call CommentToggle('#')<CR>

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.

念﹏祤嫣 2024-11-25 20:45:32

古拉什王子的答案与前导选项卡不一致。

我更改了它,将制表符添加到模式中,尽管在注释和取消注释后行会失去缩进。

function! CommentToggle( comchar )
        let firstchar = matchstr( getline( "." ), "[^ \t]" )
        if firstchar == a:comchar
                sil exe 'normal ^2x'
        else
                sil exe 'normal ^i' . a:comchar . ' '
        endif
endfunction

我更喜欢将注释字符添加到行中的第一个位置,对 Prince Goulash 函数的修改可以达到目的:

function! CommentToggle( comchar )
        let firstchar = matchstr( getline( "." ), "[^ \t]" )
        if firstchar == a:comchar
                sil exe 'normal ^2x'
        else
                sil exe 'normal gI' . a:comchar . ' '
        endif
endfunction

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.

function! CommentToggle( comchar )
        let firstchar = matchstr( getline( "." ), "[^ \t]" )
        if firstchar == a:comchar
                sil exe 'normal ^2x'
        else
                sil exe 'normal ^i' . a:comchar . ' '
        endif
endfunction

I like more adding the comment char to first position in line, this modification to Prince Goulash's function does the trick:

function! CommentToggle( comchar )
        let firstchar = matchstr( getline( "." ), "[^ \t]" )
        if firstchar == a:comchar
                sil exe 'normal ^2x'
        else
                sil exe 'normal gI' . a:comchar . ' '
        endif
endfunction
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文