How to paste over without overwriting register

后端 未结 10 2568
粉色の甜心
粉色の甜心 2020-11-28 02:41

Does anyone know of a way to paste over a visually selected area without having the selection placed in the default register?

I know I can solve the problem by alway

相关标签:
10条回答
  • 2020-11-28 03:12

    Try this in your ~/.vimrc:

    xnoremap <expr> p 'pgv"'.v:register.'y'
    
    • xnoremap means that this is only for Visual mode, not Visual + Select modes.

    • <expr> means that {rhs} of the xnoremap {lhs} {rhs} setting is evaluated as an expression.

    • In this case, our expression of 'pgv"'.v:register.'y' is using . for concatenation.

    • v:register is evaluated to the register being used during the fulfillment of the mapping.

    The result of "xp would evaluate to pgv"xy, where x is the register.

    I was helped by an answer to this stackoverflow question: Vim - mapping with an optional register prefix in conjunction with Benoit's answer on this page

    0 讨论(0)
  • 2020-11-28 03:14

    Use the following:

    xnoremap p pgvy
    

    this will reselect and re-yank any text that is pasted in visual mode.

    Edit: in order this to work with "xp you can do:

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

    v:register expands to the last register name used in a normal mode command.

    0 讨论(0)
  • 2020-11-28 03:15

    Luc's function worked well for me after I made a change to support the fact that I have clipboard=unnamed set:

    function! RestoreRegister()
        let @" = s:restore_reg
        if &clipboard == "unnamed"
            let @* = s:restore_reg
        endif
        return ''
    endfunction
    
    0 讨论(0)
  • 2020-11-28 03:23

    "{register}p won't work as you describe. It will replace the selection with the content of the register. You will have instead to do something like:

    " 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()
    

    Which should be fine as long as you don't use a plugin that has a non-nore vmap to p, and that expects a register to be overwritten.

    This code is available as a script there. Ingo Karkat also defined a plugin solving the same issue.

    0 讨论(0)
  • 2020-11-28 03:28

    Luc Hermitte's did the trick! Really good. Here's his solution put in a toggle function, so you can switch between normal behavior and no-replace-register put.

    the command ,u toggles the behavior

    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>
    
    0 讨论(0)
  • 2020-11-28 03:29

    duct-tape programming, but works for me:

    nmap viwp viwpyiw
    nmap vi'p vi'pyi'
    nmap vi"p vi"pyi"
    nmap vi(p vi(pyi(
    nmap vi[p vi[pyi[
    nmap vi<p vi<pyi<
    
    0 讨论(0)
提交回复
热议问题