" 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()
只要你不使用一个有一个非 nore vmap 到 p 的插件,并且这个插件期望一个寄存器被覆盖,那么就没问题。
"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
Luc Hermitte 的表演成功了!非常好。下面是他在 toggle 函数中提供的解决方案,这样您就可以在正常行为和无替换寄存器 put 之间进行切换。
命令,u 触发行为
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>
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