tags:

views:

1292

answers:

5

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

I know I can solve the problem by always pasting from an expicit register. But it's a pain in the neck to type "xp instead of just p

Thanks

A: 

try -

:set guioptions-=a
:set guioptions-=A
Gowri
Those control whether selected text is added to the windowing system's clipboard (e.g., X11 or Win32), not to Vim's internal copy registers.
Rob Kennedy
+3  A: 

"{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.

Luc Hermitte
Thanks! That did the trick. I guess I'd better learn to script vim sometime. :)
Starr Horne
Actually, I though there was a neat way to fetch the register used, but couldn't remember how. Hence the complexity of the function.
Luc Hermitte
A: 

hi,

off-topic: did you write this comment? http://ifacethoughts.net/2008/05/11/task-management-using-vim/#comment-245152

would be interessted in your vim snippset, yaml approach ;)

Just leave me a message in this thread here.

cheers -- jerik

+3  A: 

I don't like the default vim behavior of copying all text deleted with d, D, c, or C into the default register.

I've gotten around it by mapping d to "_d, c to "_c, and so on.

From my .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
A: 

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>
Joernsn
You can also change s:Repl to return "p" instead of "p@=RestoreRegister()\<cr>" depending on s:putSwap value.
Luc Hermitte