vim cut and paste history

60

10

When I'm 'cutting' in vim, I believe there are registers that keep a history of all the recent things I've cut. How do I access those registers?

For example, let's say I cut each one of these words consecutively

  • 'Hello'
  • 'World'
  • 'And'
  • 'Vim'

Note that I'm not actually saving these edits into particular registers, I'm just using 'd' four times consecutively.

user4941

Posted 2010-01-31T01:10:06.393

Reputation: 947

Answers

103

The

:help registers

command reveals that there are 10 numbered registers ("0 to "9).

Register "0 is the most recent thing yanked; register "1 has the most recent deleted text, register "2 the previous deletion, "3 has the deletion before that, and so on.

If you delete each line in turn, registers "1, "2, "3 and "4 will contain "Vim", "And", "World" & "Hello", respectively.

You can verify this by using the :reg (or :registers) command:

:reg
""   Vim^J
"1   Vim^J
"2   And^J
"3   World^J
"4   Hello^J

So after deleting the four lines one at a time, you could recover the 2nd line ("World") with

"3p

because it's the third most recent deletion.

njd

Posted 2010-01-31T01:10:06.393

Reputation: 9 743

3This answer leaves out an important detail: Only deletions of one line or more are stored in the history this way. If you delete the words in the way that OP described in the question (using d four times, instead of dd) then all but the last deleted one are lost. – Alexander Rechsteiner – 2016-07-13T16:23:54.327

This is perfect; however, what I want the most recent 'yanked' text? – Alexey – 2010-10-26T17:01:07.433

So you would use: p (which means the same as "0p) – njd – 2010-10-27T18:30:22.797

1Also pay attention to the special registers, especially "*" and "+", when you read :help registers. Very useful. – Daniel Andersson – 2012-02-15T09:09:42.840

3Gods, you learn something new every day as a vimmer. It's great. Thanks, @njd. – ELLIOTTCABLE – 2012-11-22T16:42:12.593

8

In addition to njd's answer, this can be simplified with the YankRing plugin. As well as making it easier to browse the previous yanks, you can configure some keys to allow you to pop previous yanks off the 'stack'. This allows you to do:

yy    " Copy first line
yy    " Copy second line
yy    " Copy third line
yy    " Copy fourth line
" Assumes you've mapped ,p to be the pop command: choose your preferred key or key-combination
,p    " Paste fourth line and pop it off the Yank Ring
,p    " Paste third line and pop it off the Yank Ring
,p    " Paste second line and pop it off the Yank Ring
,p    " Paste first line and pop it off the Yank Ring

Al.

Posted 2010-01-31T01:10:06.393

Reputation: 2 396

4Could you add how to map ,p to pop the previous yank? – Andrew Wood – 2013-05-02T19:47:22.630