Visual-mode pasting modifies unnamed buffer. Correct?

6 views
Skip to first unread message

Ted Pavlic

unread,
Feb 24, 2009, 10:45:42 AM2/24/09
to vim_use
I've looked in:

:help v_p
:help registers

and I cannot find this behavior documented, and so I wonder if it's a bug.

If I visually select a block and hit "p" to paste from the unnamed
buffer, the block I'm overwriting gets put into the unnamed buffer.
There are two strange consequences of this:

1. If I need to make several replacements down my file, I have to do a
new copy between each replacement (or use a new buffer).

2. If I select the same block over and over again, hitting "p" will
toggle the block back and forth from its old state to its new state.

Is this the desired behavior? If so, shouldn't it be documented?

Also, is there any way to disable this behavior? I'm happier knowing
that only "d", "c", "s", 'x", and "y" put things into the unnamed buffer.

Thanks --
Ted


--
Ted Pavlic <t...@tedpavlic.com>

Please visit my ALS association page:
http://web.alsa.org/goto/tedpavlic
My family appreciates your support in the fight to defeat ALS.

Ben Fritz

unread,
Feb 24, 2009, 11:33:21 AM2/24/09
to vim_use


On Feb 24, 9:45 am, Ted Pavlic <t...@tedpavlic.com> wrote:
> I've looked in:
>
>         :help v_p
>         :help registers
>
> and I cannot find this behavior documented, and so I wonder if it's a bug.
>

It looks like it is documented...sort of. From :help v_p:

"(Implementation detail:
it actually works by first putting the register after the selection
and then
deleting the selection.)"

I take this to mean it will first do an <esc>`<P and then do a `<d`>
or something similar.

Doing these commands will in fact override the unnamed register,
though the documentation could certainly be made more clear.

> If I visually select a block and hit "p" to paste from the unnamed
> buffer, the block I'm overwriting gets put into the unnamed buffer.
> There are two strange consequences of this:
>
> 1. If I need to make several replacements down my file, I have to do a
> new copy between each replacement (or use a new buffer).

Better idea: instead of just p, hit "0p to always paste from the yank
register. Or even use a mapping like :vnoremap p "0p

>
> 2. If I select the same block over and over again, hitting "p" will
> toggle the block back and forth from its old state to its new state.
>

That's actually kind of cool...and lets you move blocks around easier.

> Is this the desired behavior? If so, shouldn't it be documented?
>
> Also, is there any way to disable this behavior? I'm happier knowing
> that only "d", "c", "s", 'x", and "y" put things into the unnamed buffer.
>

:vnoremap p "_d"0P or similar ought to do it.

Luc Hermitte

unread,
Feb 24, 2009, 11:51:39 AM2/24/09
to vim use
Hello,

"Ben Fritz" <fritzo...@gmail.com> wrote :


> > Also, is there any way to disable this behavior? I'm happier
> > knowing that only "d", "c", "s", 'x", and "y" put things into the
> > unnamed buffer.
>
> :vnoremap p "_d"0P or similar ought to do it.

In order to avoid overwriting the @" register, but still being able to
paste the content of any register, I came up with this [1]

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

" This supports "rp that permits to replace the visual selection with
" the contents of @r
vnoremap <silent> <expr> p <sid>Repl()

"------------------------------------------------------------------------

[1] This dozen lines is maintained there:
http://code.google.com/p/lh-vim/source/browse/misc/trunk/macros/repl-visual-no-reg-overwrite.vim

--
Luc Hermitte
http://lh-vim.googlecode.com/
http://hermitte.free.fr/vim/

Tim Chase

unread,
Feb 24, 2009, 12:21:57 PM2/24/09
to vim...@googlegroups.com
> and I cannot find this behavior documented, and so I wonder if it's a bug.
>
> If I visually select a block and hit "p" to paste from the unnamed
> buffer, the block I'm overwriting gets put into the unnamed buffer.
> There are two strange consequences of this:
>
> 1. If I need to make several replacements down my file, I have to do a
> new copy between each replacement (or use a new buffer).
>
> 2. If I select the same block over and over again, hitting "p" will
> toggle the block back and forth from its old state to its new state.
>
> Is this the desired behavior? If so, shouldn't it be documented?


It's not so much a bug...the help on "v_p" mentions

"Implementation detail: it actually works by first putting the
register after the selection and then deleting the selection."

The deletion is what's throwing you because it goes to the
unnamed register. You might try something like one of the
following:

:vnoremap <f4> "_c<c-r>"
:vnoremap <f4> <esc>Pgv"_d

which will do the deletion to the black-hole register. The only
catch is that you can't paste from a named register with these
mappings.

-tim

Bram Moolenaar

unread,
Feb 24, 2009, 11:58:37 PM2/24/09
to Ted Pavlic, vim_use

Ted Pavlic wrote:

> I've looked in:
>
> :help v_p
> :help registers
>
> and I cannot find this behavior documented, and so I wonder if it's a bug.
>
> If I visually select a block and hit "p" to paste from the unnamed
> buffer, the block I'm overwriting gets put into the unnamed buffer.
> There are two strange consequences of this:
>
> 1. If I need to make several replacements down my file, I have to do a
> new copy between each replacement (or use a new buffer).
>
> 2. If I select the same block over and over again, hitting "p" will
> toggle the block back and forth from its old state to its new state.
>
> Is this the desired behavior? If so, shouldn't it be documented?
>
> Also, is there any way to disable this behavior? I'm happier knowing
> that only "d", "c", "s", 'x", and "y" put things into the unnamed buffer.

No, it cannot be disabled. Deleted text always has to go somewhere.

I agree that this isn't easy to understand. Let me add this text to the
help:

The previously selected text is put in the unnamed register. If
you want to put the same text into a Visual selection several
times you need to use another register. E.g., yank the text to
copy, Visually select the text to replace and use "0p . You can
repeat this as many times as you like, the unnamed register will
be changed each time.

--
hundred-and-one symptoms of being an internet addict:
128. You can access the Net -- via your portable and cellular phone.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ted Pavlic

unread,
Feb 25, 2009, 7:49:26 AM2/25/09
to Bram Moolenaar, vim_use
>> Also, is there any way to disable this behavior? I'm happier knowing
>> that only "d", "c", "s", 'x", and "y" put things into the unnamed buffer.
>
> No, it cannot be disabled. Deleted text always has to go somewhere.
>
> I agree that this isn't easy to understand. Let me add this text to the
> help:
>
> The previously selected text is put in the unnamed register. If
> you want to put the same text into a Visual selection several
> times you need to use another register. E.g., yank the text to
> copy, Visually select the text to replace and use "0p . You can
> repeat this as many times as you like, the unnamed register will
> be changed each time.

That passage is clear to me. (I assume it's an addition to ":help v_p"?)

Ted Pavlic

unread,
Feb 25, 2009, 7:55:35 AM2/25/09
to vim_use
> Better idea: instead of just p, hit "0p to always paste from the yank
> register. Or even use a mapping like :vnoremap p "0p

I see -- that makes sense. The downside of that particular remap is that
sometimes I might want to paste out of another buffer. But "0p is better
than having to do a p and then finding the text to copy again.

>> 2. If I select the same block over and over again, hitting "p" will
>> toggle the block back and forth from its old state to its new state.
> That's actually kind of cool...and lets you move blocks around easier.

At first, I suppose the "swap" character of the whole thing seems neat,
but I have a feeling it's not really useful in practice.

Still, I'm persuaded by the argument that deleted stuff (especially big
blocks) have to be put somewhere.

> :vnoremap p "_d"0P or similar ought to do it.

Again, it would be nice if there was a remap that would still allow for
pasting out of other buffers...

Thanks! --

Reply all
Reply to author
Forward
0 new messages