1080

I only know of one instance using registers is via CtrlR* whereby I paste text from a clipboard.

What are other uses of registers? How to use them?

Everything you know about VI registers (let's focus on vi 7.2) -- share with us.

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • 29
    I gave a long answer [there](http://stackoverflow.com/questions/3997078/how-to-paste-text-into-vim-command-line/3997110#3997110) on the use of registers – Benoit Oct 22 '11 at 05:39

17 Answers17

1390

Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type

"kyy

Or you can append to a register by using a capital letter

"Kyy

You can then move through the document and paste it elsewhere using

"kp

To paste from system clipboard on Linux

"+p

To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)

"*p

To access all currently defined registers type

:reg
D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
FModa3
  • 14,409
  • 1
  • 17
  • 10
  • 185
    And `"+` is a special register that refers to the system clipboard. – Aaron McDaid Feb 12 '13 at 22:54
  • 22
    In Windows, the Clipboard can be accessed with `"*`; so the command to copy till end of file becomes `"*yG`. – Aditya M P Mar 14 '13 at 11:35
  • Can you append to the numeric registers? – ruffin Jun 05 '13 at 16:45
  • 48
    In X (Linux, possibly also OS-X), `*` is the "mouse highlight" clipboard, and `+` is the Ctrl-XCV clipboard. – dotancohen Jun 13 '13 at 05:49
  • 23
    Is there a way to append to the clipboard register (since there's no capital `+`)? – naitsirhc Aug 13 '13 at 15:30
  • 9
    @dotancohen Technically it's called PRIMARY selection, but I guess "mouse higlight" is easier for people to get :) – remmy Nov 26 '13 at 16:45
  • 1
    @AaronMcDaid I can not use "+ for my vim and the '+clipboard' is enabled. Any suggestions? – hiveer Feb 11 '15 at 07:16
  • How do you copy/move contents from one register to another? I don't want to mess with the text area. – Loves Probability Apr 23 '17 at 06:10
  • what is system clipboard? – kapil Jun 16 '17 at 17:47
  • 2
    In a terminal Vim (i.e. a non-windowing environment) there is no "clipboard" or "copy/paste buffer". So the `+` register will (essentially) always be empty (in that you can't copy in to or out from another app/window/editor). However, it is possible to put stuff in the `+` buffer. Same with the `*` buffer. But they are less useful without the windowing environment. – JDS Oct 12 '17 at 14:38
  • 2
    @naitsirhc: I don't think there's any way to do that using the `"` command, but you can run `let @+ .= @"` to append the contents of the unnamed register to the clipboard register. If you map this to, say, `+`, then you can do, e.g., `yy+` to append the current line to the clipboard register. – Soren Bjornstad Nov 24 '18 at 00:34
  • How to yank text range in a file and paste it in another file (another bash / terminal)? (1) From line 10 to line 20 (2) From current line to the end of the file – Kok How Teh Oct 17 '19 at 02:42
  • To read the content of a specific register, try `:reg ` So for reading register 9, you'd do `:reg 9`. You can also view select multiple ones like `:reg 0,9,=,c` so on and so forth – Ambareesh Apr 19 '20 at 02:50
  • Other instances of `vim` don't recognise that anything was put into the register. – Myridium Jun 30 '20 at 08:01
  • How to past from a specific register when in insert mode? Normally you can insert from the default yank register in insert mode with `ctrl-r "`. – MattCochrane Jul 02 '20 at 01:49
885

I was pleased when I discovered the 0 register. If you yank text without assigning it to a particular register, then it will be assigned to the 0 register, as well as being saved in the default " register. The difference between the 0 and " registers is that 0 is only populated with yanked text, whereas the default register is also populated with text deleted using d/D/x/X/c/C/s/S commands.

I find this useful when I want to copy some text, delete something and replace it with the copied text. The following steps illustrate an example:

  • Yank the text you want to copy with y[motion] - this text is saved in " and 0 registers
  • Delete the text you want to replace with d[motion] - this text is saved in " register
  • Paste the yanked text with "0p

where " is the command to use a register for the next command.

On the final step, if you were to paste from the default register (with p), it would use the text that you had just deleted (probably not what you intended).

Note that p or P pastes from the default register. The longhand equivalent would be ""p (or ""P) and "0 holds the last yank, "1holds the last delete or change.

For more info see :help registers.

kenorb
  • 155,785
  • 88
  • 678
  • 743
nelstrom
  • 18,802
  • 13
  • 54
  • 70
  • 2
    How do you paste from default register? – vehomzzz Oct 01 '09 at 17:31
  • 19
    `p` or `P` pastes from the default register. The longhand equivalent would be `""p` (or `""P`). – nelstrom Oct 02 '09 at 09:08
  • 214
    By the way, the same way "0 holds the last yank, "1 holds the last delete or change. – FireAphis Jun 02 '11 at 15:02
  • Are `"` and `0` register different from `"0`? Can you paste with `0p` and `"0p` and `"p`? – user202448 Aug 10 '11 at 21:33
  • 6
    `"` is the command to use a register for the next command. So `""p` would use the `"` register for the paste command and `"0p` would use the `0` register. – Ramon Marco L. Navarro Aug 18 '11 at 09:33
  • 5
    `0p` would mean go to the first character of the current line and paste from the contents of the default register (`"`). `"p` would be an incomplete command since you need to specify what command to use the register `p` for. – Ramon Marco L. Navarro Aug 18 '11 at 09:35
  • 22
    The same way "1 holds the last delete, "2 holds the second last delete, "3 holds the third last, etc up to "9 – James Dec 05 '11 at 21:45
  • In recent versions of vim, deletions that are smaller than one line go in to the "small deletions register" instead of going in to the numbered registers. References: [Vi and Vim](http://vi.stackexchange.com/questions/4532/numbered-register-doesnt-record-most-deleted-text-within-a-line), [Vi and Vim](http://vi.stackexchange.com/questions/2493/can-i-make-vim-also-save-small-deletions-into-register-1), [Reddit](https://www.reddit.com/r/vim/comments/yyq4a/is_there_anyway_to_configure_vim_to_use_the/) – Christian Long Jan 28 '16 at 18:26
369

One of my favorite parts about registers is using them as macros!

Let's say you are dealing with a tab-delimited value file as such:

ID  Df  %Dev    Lambda
1   0   0.000000    0.313682
2   1   0.023113    0.304332
3   1   0.044869    0.295261
4   1   0.065347    0.286460
5   1   0.084623    0.277922
6   1   0.102767    0.269638
7   1   0.119845    0.261601

Now you decide that you need to add a percentage sign at the end of the %Dev field (starting from 2nd line). We'll make a simple macro in the (arbitrarily selected) m register as follows:

  1. Press: qm: To start recording macro under m register.

  2. EE: Go to the end of the 3rd column.

  3. a: Insert mode to append to the end of this column.

  4. %: Type the percent sign we want to add.

  5. <ESC>: Get back into command mode.

  6. j0: Go to beginning of next line.

  7. q: Stop recording macro

We can now just type @m to run this macro on the current line. Furthermore, we can type @@ to repeat, or 100@m to do this 100 times! Life's looking pretty good.

At this point you should be saying, "But what does this have to do with registers?"

Excellent point. Let's investigate what is in the contents of the m register by typing "mp. We then get the following:

EEa%<ESC>j0

At first this looks like you accidentally opened a binary file in notepad, but upon second glance, it's the exact sequence of characters in our macro!

You are a curious person, so let's do something interesting and edit this line of text to insert a ! instead of boring old %.

EEa!<ESC>j0

Then let's yank this into the n register by typing B"nyE. Then, just for kicks, let's run the n macro on a line of our data using @n....

It added a !.

Essentially, running a macro is like pressing the exact sequence of keys in that macro's register. If that isn't a cool register trick, I'll eat my hat.

APerson
  • 8,140
  • 8
  • 35
  • 49
Francisco
  • 375
  • 2
  • 5
  • 9
  • 80
    This is totally key. I haven't noticed until now that registers are the same as macro buffers. This means you can just store all your macros in text, yank them, and run them. Pretty cool, and a little bit weird. Very Vim. – naught101 Apr 11 '12 at 06:35
  • 6
    You can use `+` instead of `j0` to go to the first non blank character on the next line. – sudormrfbin May 02 '20 at 05:07
  • 3
    This is brilliant. @naught101, it seems weird at first, but it allows you to write out your macro and edit it without the pressure of doing it right while Vim is "recording." Like writing code before running it, debugging it, and running it again until it's right. – mindthief Jul 15 '20 at 22:58
  • 1
    Wow love the `+` @sudormrfbin so usefull (was always a pain to do `j^` because `^` is far from home row), also `-` does the same on the previous line. Thank you! – mDeram Jun 14 '23 at 08:23
115

Other useful registers:

"* or "+ - the contents of the system clipboard

"/ - last search command

": - last command-line command.

Note with vim macros, you can edit them, since they are just a list of the keystrokes used when recording the macro. So you can write to a text file the macro (using "ap to write macro a) and edit them, and load them into a register with "ay$. Nice way of storing useful macros.

mindthief
  • 12,755
  • 14
  • 57
  • 61
Anthony Roy
  • 1,835
  • 3
  • 15
  • 16
  • 6
    Default vim on OSX doesn't include "* integration. You can install a version from homebrew-alt that does. Check with `vim --version` - `-clipboard` means you don't have it, `+clipboard` means you do. – Xavier Shay Aug 11 '11 at 07:26
  • 16
    In X11, `"*` and `"+` are even cooler: `"+` pastes the last text copied with `ctrl+c` (copy buffer), or what ever shortcut you use (or `right-click>copy`). `"*` pastes the last text highlighted with the mouse (selection buffer). So if you copy something with `ctrl+c`, and then highlight something else with the mouse, you can choose which one to paste. This doesn't work in Windows, where these two registers are synonymous. – naught101 Apr 11 '12 at 06:30
  • 1
    Bless you, I've been looking for `"*` and `"+` for years! I thought only gvim had access to the system clipboards, but apparently regular vim does too! – Tarrasch Aug 29 '12 at 14:42
  • @Tarrasch If you use the console vim instead of gvim you'll discover alot more things. I think. visit #vim on freenode too! – trusktr Oct 05 '13 at 23:40
  • 1
    How to enable if one has `-xterm_clipboard` on `vim --version`. Any simple package to install? (debian/apt-get). Thanks. – DrBeco Aug 24 '14 at 22:30
  • @DrBeco: does debian have a `vim-full` package or perhaps even a `gvim-full` available? Try installing a gvim variant, if existing. It should also provide a console version. – mike3996 Oct 21 '14 at 10:21
82

The black hole register _ is the /dev/null of registers.

I use it in my vimrc to allow deleting single characters without updating the default register:

noremap x "_x

and to paste in visual mode without updating the default register:

vnoremap p "_dP
mrucci
  • 4,342
  • 3
  • 33
  • 35
  • 1
    The second mapping is also very useful. See here for an explanation: http://marcorucci.com/blog/#visualModePaste – mrucci Sep 05 '11 at 01:41
  • 24
    the mapping for `x` gets less convenient to transpose chars. – skeept Oct 05 '11 at 19:43
  • 2
    Pressing x doesn't erase the last thing you've yanked, which you'll find is in register 0. Press x a few times, then "0p to paste the stuff you yanked. – trusktr Oct 05 '13 at 23:48
  • 4
    To expand on skeept's remark above: with this `x` mapping you can no longer do the `xp` "command" to switch the current with the next character. Quite useful for typo fixes. We also have `Xp` to switch the current with the previous character. – Daniel Andersson Aug 16 '14 at 08:19
63

If you ever want to paste the contents of the register in an ex-mode command, hit <C-r><registerletter>.

Why would you use this? I wanted to do a search and replace for a longish string, so I selected it in visual mode, started typing out the search/replace expression :%s/[PASTE YANKED PHRASE]//g and went on my day.

If you only want to paste a single word in ex mode, can make sure the cursor is on it before entering ex mode, and then hit <C-r><C-w> when in ex mode to paste the word.

To make it more convenient :

cnoremap <c-g> <c-r>"
cnoremap <C-Right> <c-r><c-w>
Good Pen
  • 625
  • 6
  • 10
agscala
  • 3,835
  • 5
  • 27
  • 25
  • 8
    Pasting text in the `:` command line is the best thing I learned today – Shahbaz Jun 07 '12 at 09:40
  • 1
    You can also use in insert mode – Floegipoky Aug 17 '16 at 18:23
  • 2
    I like writing find/replace regexes first with just `/[write out your regex]` and then to replace just `s///replacement/`. This way you get the benefit of live feedback on your regex before running the replacement. – GabeIsman Mar 29 '17 at 18:41
50

A cool trick is to use "1p to paste the last delete/change (, and then use . to repeatedly to paste the subsequent deletes. In other words, "1p... is basically equivalent to "1p"2p"3p"4p.

You can use this to reverse-order a handful of lines: dddddddddd"1p....

naught101
  • 18,687
  • 19
  • 90
  • 138
46

I think the secret guru register is the expression = register. It can be used for creative mappings.

:inoremap  \d The current date <c-r>=system("date")<cr>

You can use it in conjunction with your system as above or get responses from custom VimL functions etc.

or just ad hoc stuff like

<c-r>=35+7<cr>
0xc0de
  • 8,028
  • 5
  • 49
  • 75
michael
  • 11,667
  • 2
  • 26
  • 25
  • 4
    Check out Derek Wyatt's [video on using the expression register](http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-advanced-tutorial-videos/#expression-register ) – Jeromy Anglim Jan 31 '11 at 05:14
  • 1
    The link from @JeromyAnglim is now here: http://derekwyatt.org/vim/tutorials/advanced/#expression-register – Djizeus Jul 03 '16 at 15:51
20
  • q5 records edits into register 5 (next q stops recording)
  • :reg show all registers and any contents in them
  • @5 execute register 5 macro (recorded edits)
fpmurphy
  • 2,464
  • 1
  • 18
  • 22
  • 15
    I don't like the @ key to launch macros (three keystrokes). So I have mapped F2 to launch the macro stored in register q: `nmap @q` I use register q, because then to save a macro for F2 to playback, I just hit `qq`. speedy macro save and replay. – Andrej Panjkov Aug 11 '11 at 00:24
20

From vim's help page:

CTRL-R {0-9a-z"%#:-=.}                  *c_CTRL-R* *c_<C-R>*
        Insert the contents of a numbered or named register.  Between
        typing CTRL-R and the second character '"' will be displayed
        <...snip...>
        Special registers:
            '"' the unnamed register, containing the text of
                the last delete or yank
            '%' the current file name
            '#' the alternate file name
            '*' the clipboard contents (X11: primary selection)
            '+' the clipboard contents
            '/' the last search pattern
            ':' the last command-line
            '-' the last small (less than a line) delete
            '.' the last inserted text
                            *c_CTRL-R_=*
            '=' the expression register: you are prompted to
                enter an expression (see |expression|)
                (doesn't work at the expression prompt; some
                things such as changing the buffer or current
                window are not allowed to avoid side effects)
                When the result is a |List| the items are used
                as lines.  They can have line breaks inside
                too.
                When the result is a Float it's automatically
                converted to a String.
        See |registers| about registers.  {not in Vi}
        <...snip...>
Brian
  • 1
  • 1
  • 2
17

Use registers in commands with @. E.g.:

echo @a
echo @0
echo @+

Set them in command:

let @a = 'abc'

Now "ap will paste abc.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
17

I use the default register to grep for text in my vim window without having to reach for the mouse.

  1. yank text
  2. :!grep "<CTRL-R>0"<CR>
Matt Mendell
  • 106
  • 1
  • 3
12

One overlooked register is the '.' dot register which contains the last inserted text no matter how it was inserted eg ct] (change till ]). Then you realise you need to insert it elsewhere but can't use the dot repeat method.

 :reg .
 :%s/fred/<C-R>./
zzapper
  • 4,743
  • 5
  • 48
  • 45
  • 2
    Nice, I think it's natural to think about command-based edits differently from actually-typed text, so using the `".` register to avoid clobbering insertions seems useful. – mindthief Jul 15 '20 at 23:12
9

My favorite register is the ':' register. Running @: in Normal mode allows me to repeat the previously ran ex command.

So we can verify some commands in with :MY_commandXXXXX then put MY_commandXXXXX in vimrc

Good Pen
  • 625
  • 6
  • 10
siu
  • 312
  • 1
  • 5
  • 10
8

A big source of confusion is the default register ". It is important to know the way it works. It is much better if the default register is avoided most of the times. The explanation from the Vim documentation:

Vim fills this register with text deleted with the "d", "c", "s", "x" commands
or copied with the yank "y" command, regardless of whether or not a specific
register was used (e.g.  "xdd).  This is like the unnamed register is pointing
to the last used register.

So the default register is actually a pointer to the last used register. When you delete, or yank something this register is going to point to other registers. You can test that by checking the registers. There is always another register that is exactly the same as the default register: the yank register ("0) , the first delete register("1) , small delete register("-) or any other register that was used to delete or yank.

The only exception is the black hole register. Vim doc says:

An exception is the '_' register: "_dd does not store the deleted text in any
register.

Usually you are much better off by using directly: "0, "- and "1-"9 default registers or named registers.

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53
Catalin Ciurea
  • 771
  • 1
  • 9
  • 7
5

My friend Brian wrote a comprehensive article on this. I think it is a great intro to how to use topics. https://www.brianstorti.com/vim-registers/

Madis Nõmme
  • 1,264
  • 2
  • 15
  • 25
3

My favorite feature is the ability to append into registers by using capital letters. For example, say you want to move a subset of imports from buffer X to buffer Y.

  1. Go to line x1 in buffer X.
  2. Type "ayy to replace register a with the content of line x1.
  3. Go to line x5.
  4. Type "Ayy (capital A) to append line x5 at the end of register a.
  5. Go to buffer Y and type "ap to paste
<content of line x1>
<content of line x5>
azizj
  • 3,428
  • 2
  • 25
  • 33