有谁知道一种方法可以粘贴到视觉上选择的区域而不将选择放置在默认寄存器中?

我知道我可以通过始终从显式寄存器粘贴来解决问题。但打字很头疼 "Xp 而不是仅仅 p

有帮助吗?

解决方案

"{register}p将无法按您的描述工作。它将用寄存器的内容替换选择。你将改为做以下事情:

" I haven't found how to hide this function (yet)
function! RestoreRegister()
  let @" = s:restore_reg
  return ''
endfunction

function! s:Repl()
    let s:restore_reg = @"
    return "p@=RestoreRegister()\<cr>"
endfunction

" NB: this supports "rp that replaces the selection by the contents of @r
vnoremap <silent> <expr> p <sid>Repl()

只要您不使用对p具有非nore vmap的插件并且希望覆盖寄存器,那么这应该没问题。

此代码以脚本形式提供的。 Ingo Karkat还定义了插件来解决同样的问题。

其他提示

我不喜欢将使用dDcC删除的所有文本复制到默认寄存器中的默认vim行为。

我通过将"_d映射到"_c,<=>到<=>等来解决这个问题。

来自我的.vimrc:

"These are to cancel the default behavior of d, D, c, C
"  to put the text they delete in the default register.
"  Note that this means e.g. "ad won't copy the text into
"  register a anymore.  You have to explicitly yank it.
nnoremap d "_d
vnoremap d "_d
nnoremap D "_D
vnoremap D "_D
nnoremap c "_c
vnoremap c "_c
nnoremap C "_C
vnoremap C "_C

使用以下内容:

xnoremap p pgvy

这将重新选择并重新扫描以可视模式粘贴的任何文本。

编辑:为了与"xp一起使用,你可以这样做:

xnoremap p pgv"@=v:register.'y'<cr>

v:register扩展为普通模式命令中使用的最后一个寄存器名称。

.vimrc

xnoremap p "_dP

我是从类似帖子的回复中找到的,但原始来源是 http:// vim。 wikia.com/wiki/Replace_a_word_with_yanked_text 。它提到了一些缺点,但它适用于我。

Luc Hermitte的解决方案就像一个魅力。我用了大约一个星期左右。然后我从 Steve Losh的.vimrc 中发现了一个很好的解决方案如果YankRing是你的插件/捆绑产品阵容的一部分:

function! YRRunAfterMaps()                                                                                                      
    " From Steve Losh, Preserve the yank post selection/put.    
    vnoremap p :<c-u>YRPaste 'p', 'v'<cr>gv:YRYankRange 'v'<cr> 
endfunction  

在你的试试这个 ~/.vimrc:

xnoremap <expr> p 'pgv"'.v:register.'y'
  • xnoremap 意味着这仅适用于 Visual 模式,不 Visual + Select 模式。

  • <expr> 意思是 {rhs}xnoremap {lhs} {rhs} 设置被评估为表达式。

  • 在这种情况下,我们的表达 'pgv"'.v:register.'y' 正在使用 . 用于串联。

  • v:register 评估映射完成期间使用的寄存器。

的结果 "xp 将评估为 pgv"xy, , 在哪里 x 是寄存器。

这个 stackoverflow 问题的答案对我很有帮助: Vim - 使用可选寄存器前缀进行映射和这个结合 伯努瓦的回答 在本页

在我做出改变以支持我有剪贴板=未命名设置的事实后,Luc的功能对我很有用:

function! RestoreRegister()
    let @" = s:restore_reg
    if &clipboard == "unnamed"
        let @* = s:restore_reg
    endif
    return ''
endfunction

Luc Hermitte的伎俩!真的很好。这是他的解决方案放入切换功能,因此您可以在正常行为和无替换寄存器之间切换。

命令,你切换行为

let s:putSwap = 1 
function TogglePutSwap()
    if s:putSwap
        vnoremap <silent> <expr> p <sid>Repl()
        let s:putSwap = 0 
        echo 'noreplace put'
    else
        vnoremap <silent> <expr> p p 
        let s:putSwap = 1 
        echo 'replace put'
    endif
    return
endfunction
noremap ,p :call TogglePutSwap()<cr>

尝试 -

:set guioptions-=a
:set guioptions-=A
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top