Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active November 14, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/836a5d9d756ab728eef175c1f4cbd24e to your computer and use it in GitHub Desktop.
Save krisleech/836a5d9d756ab728eef175c1f4cbd24e to your computer and use it in GitHub Desktop.
using vim registers

Usually you would yank some text and then paste it elsewhere, as such:

y$ (yank to end of line) p (paste text)

But what if, between these two commands you need to delete text, e.g. dd, which will mean p will paste the text just deleted, not that which was yanked initially.

Instead of plain yank, yank to a register:

the quick brown fox

"kY (in register k yank line Y). You can also do the other usual types of yank, e.g. y$ (yank to end of line), yW (yank word) etc.

Note: k is arbitrary and can be any letter.

You can now delete some lines of text, dd, dW etc., and otherwise edit the text.

And then get back the text yanked in to register k with:

"kp (from register k paste)

I find it useful to map this on the fly (to avoid cluttering up my vimrc):

:map ,yy "kY
:map ,pp "kp

,yy will yank a line to register k and ,pp will paste a line from register k.

Another method is to paste from register 0 which always contains the last yanked item (i.e. unlike plain p does includes deleted text).

"0p

Again you could map this:

:map ,pp "0p

Then I just yank, do some editting then ,pp to paste the last yanked text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment