删除时vim切换缓冲区覆盖行为 [英] vim toggling buffer overwrite behavior when deleting

查看:32
本文介绍了删除时vim切换缓冲区覆盖行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vim 很棒,但和很多人一样,当我想复制、删除、然后粘贴时,我真的很生气——删除操作会覆盖 yank 缓冲区.

Vim is great, but like many people I get really annoyed when I want to copy, delete, then paste -- the yank buffer gets overwritten by the delete action.

现在我知道有 101 种变通方法和映射,其中一些在像这样的帖子中列举:有什么方法可以在 vim 中删除而不覆盖上次 yank?

Now I know there are 101 work-arounds and mappings, some of which are enumerated in posts like this one: Any way to delete in vim without overwriting your last yank?

但是所有这些解决方案都有缺点——即使我是一个缓冲大师(我不是).例如,过多的击键 - 而我通常 xxxx 快速删除 4 个字符(只需一次击键,因为我按住它并等待自动重复),我现在切换到 ,x,x,x,x 或无论映射如何,我都必须使用不同的缓冲区.

But all of these solutions have drawbacks -- even I were a buffer-guru (which I'm not). For instance, excess keystrokes -- whereas I normally xxxx to quickly delete 4 characters (just one keystroke cuz I hold it down and wait for autorepeat), it is not practical for me to now switch to ,x,x,x,x or whatever mapping I have to use a different buffer.

真正理想的是简单的模式切换,您可以在其中切换 D、d、X 和 x 键的副作用行为,以便它们交替执行或不将其文本写入一个缓冲区.这样我就可以简单地进入无副作用"模式并彻底删除,然后在我准备好时粘贴.并根据需要重新启用副作用.

What would really be ideal is simply a mode toggle, whereby you can toggle on and off the side-effect behavior of the D, d, X, and x keys so that they alternately do or do not also write their text to a buffer. That way I can simply enter the "no side-effect" mode and delete away to heart's content, then paste when I'm ready. And re-enable side-effects if desired.

有人知道这样做的方法吗?

Does anyone know a way to do this?

[更新:解决方案] 好的,我明白了:我编写了一个切换无副作用"模式的函数......完美运行!在下面查看我接受的正确答案

[UPDATE: SOLUTION] OK I got it: I wrote a function that toggles a "no side-effects" mode... works perfectly! See my accepted correct answer below

[更新 #2] 我的解决方案仍然很好用,当我进行大量删除和粘贴时,我一直在使用它.但与此同时,我还找到了一种更轻松的方法来满足复制、粘贴、删除的特定用例,用于要删除的文本是连续的简单情况.

[UPDATE #2] My solution still works great and I use it all the time when I'm doing a lot of deleting and pasting. But meanwhile I also found a lighter way to meet the specific use-case of copy, paste, delete for simple cases where the text to delete is contiguous.

在正常提取文本后,我使用 v 命令在视觉上突出显示要删除的文本,然后使用 p 命令简单地将其粘贴到上面.无需任何特殊映射即可达到预期效果.

After yanking text normally, I then visually highlight the text to delete using the v command, and then simply paste over it with the p command. That achieves the desired effect without any special mapping.

这个工作流程的唯一问题是,如果我想再次粘贴,原来的粘贴缓冲区会被粘贴到突出显示的文本上的行为覆盖,但这种行为可以通过 .vimrc 中的以下映射轻松改变:

Only problem with this workflow is that if I wanted to paste again, the original paste buffer is overwritten by the act of pasting over the highlighted text, but this behavior is easily changed with the following mapping in .vimrc:

vnoremap p "_dp
vnoremap P "_dP

推荐答案

好的,我明白了——.vimrc 中的这个脚本让我可以有效地切换无缓冲区副作用"模式,从而不再使用 d 和 x 键当无缓冲区副作用"模式被激活时覆盖缓冲区.

OK, I got it -- this script in .vimrc lets me effectively toggle a "no buffer side-effects" mode whereby the d and x keys no longer overwrite the buffer when "no buffer side-effects" mode is activated.

在 .vimrc 中添加这个

Add this in .vimrc

function! ToggleSideEffects()
    if mapcheck("dd", "n") == ""
        noremap dd "_dd
        noremap D "_D
        noremap d "_d
        noremap X "_X
        noremap x "_x
        echo 'side effects off'
    else
        unmap dd
        unmap D
        unmap d
        unmap X
        unmap x
        echo 'side effects on'
    endif
endfunction
nnoremap ,, :call ToggleSideEffects()<CR>

然后使用组合键 ,,(两个逗号)进入和退出此模式

Then to toggle in and out of this mode use the key combination ,, (two commas)

这篇关于删除时vim切换缓冲区覆盖行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆