Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use vim registers?

Tags:

vim

vi

People also ask

How do I access vim registers?

You can also access the registers in insert/command mode with Ctrl-r + register name, like in Ctrl-r r . It will just paste the text in your current buffer. You can use the :reg command to see all the registers and their content, or filter just the ones that you are interested with :reg a b c .

How do I enable clipboard in vim?

You need to make sure clipboard is activated (which is probably not the case). if you get "-clipboard" then you would have to install vim again with the "clipboard" functionality. You can do it it by installing "vim-gtk3" or "gvim".

What is Ctrl-R in vim?

#vim. Use CTRL-R " when entering a command in command mode to paste the current paste buffer contents. Substitute " for a buffer name, % for current filename, / for last search term, + for the X clipboard or a host of other substitutions. CTRL-R also works in insert mode, no more skipping back to normal mode to paste!


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

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.


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.


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.


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

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.