On 5/05/11 12:23 AM, Andrew Neil wrote:
On Wed, May 4, 2011 at 5:05 PM, Ben Schmidt
<[email protected]>  wrote:
On 4/05/11 11:35 PM, Andrew Neil wrote:

Is there any way to prevent the default register from being clobbered
when using the paste command in visual mode?

By default, when you use the `p` or `P` commands in visual mode, the
selected text overwrites the default register. Is there any way that
you can instruct Vim to write that text to another register?

Not directly. To do that, you'd have to use separate delete and put
commands. I suppose you could probably make a mapping. It could become a
bit complicated dealing with edge cases, but I suppose you could make it
work with a bit of effort. Maybe some wizard will chip in....

Here's a simple solution:

     vnoremap p "_xP

But there's a problem with it: while `P` works fine in most cases, if
the visual selection is at the end of a line then you'd have to use
`p` instead. I think that one of the appealing things about making a
visual selection before pasting is that you don't have to worry about
whether to use `p` or `P` - it simply overwrites the selection with
the contents of the specified register.

Hmm. Maybe a strategy more like this would work:

vnoremap <expr> p
\ '<Esc>:let g:old_unnamed_reg=getreg()<CR>'
\ . ':let g:old_unnamed_type=getregtype()<CR>'
\ . 'gv'.(v:register!='"' ? '"'.v:register : '').'p'
\ . ':call setreg(''"'',g:old_unnamed_reg,g:old_unnamed_type)<CR>'

I haven't tested it much. It might not do nice things in edge cases.

The downside is that the replaced text doesn't get kept anywhere. I
suppose you could extend it to put that in v:register or elsewhere if
desired; just needs another set of getreg(), getregtype() and setreg()
calls.

Interestingly, it seems that executing ""p in visual mode is a
no-op...it doesn't behave the same as just p does. That's why I needed
the ternary operator in there. That's probably a bug.

Ben.



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to