Vim Tips Wiki
Register
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 144 Printable Monobook Previous Next

created 2001 · complexity basic · author Sameer Chabungbam · version 6.0


One of Vim's most useful features is its ability to record what you type for later playback. :help recording This is most useful for repeated jobs which cannot easily be done with ".".

To start recording, press q in normal mode followed by a letter (a to z). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text. To stop recording, again press q while in normal mode.

To stop recording while in insert mode press <Ctrl-O>q.

To playback your keystrokes, press @ followed by the letter previously chosen. Typing @@ repeats the last playback.

See also[]

Comments[]

To delete a recording just record nothing over it. For example, qxq erases whatever was recorded to register x.

or :call setreg('x', '')
or :let @x = ''

Here is an example to replace string OldString with NewString contained in multiple *.cpp files:

vim *.cpp
qx            # start recording to register x
:%s/OldString/NewString/g
:wnext
q             # stop recording
@x            # playback to see if it works correctly
999@x         # repeat 999 times to complete the job

One way to edit a recording (for example, to register x) is to paste it into a new buffer, then edit the buffer, then copy the results back into the register. For example:

:new      # new buffer
"xp       # paste register x into the buffer
[edit the keystrokes]
<Esc>     # return to normal mode
0"xy$     # go to beginning of line; into register x, yank to end of line
:bd!      # delete the new buffer without saving
@x        # execute modified recording

Alternatively, edit the contents of register x in the command line:

:let @x="<Ctrl-R><Ctrl-R>x"

The name @x identifes register x. At the command line, Ctrl-R pastes the contents of the named register into the command line, thus making it available for editing.

be careful about newlines

Advertisement