tags:

views:

490

answers:

5

I only know of one instance using registers is via Ctrl-R * 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.

Thanks

+8  A: 

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

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

"kp

To access all currently defined registers type

:reg

FModa3
+5  A: 
  • 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
+7  A: 

Other useful registers:

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

"/ - last search command

": - last 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.

Ant
+2  A: 

I think the secret guru register is the expression = register. It can be used for creative macro 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>
michael
awesome, have to remember this one.
rampion
+5  A: 

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

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).

For more info see :help registers.

nelstrom
How do you paste from default register?
vehomzzz
`p` or `P` pastes from the default register. The longhand equivalent would be `""p` (or `""P`).
nelstrom