Recording a macro is a great way to perform a one-time task, or to get things done quickly when you don't want to mess with Vim script or mappings, or if you do not yet know how to do it more elegantly.
In Vim, the word "macro" may refer to:
- A sequence of commands recorded to a register (this tip).
- A mapping to expand a sequence of typed keys to a longer sequence (see tutorial).
- A script written in the Vim script language (stored in a file with extension
.vim– see :help script).
Recording a macro
Each register is identified by a letter
To enter a macro, type:
To execute the macro <number> times (once by default), type:
So, the complete process looks like:
| || start recording to register |
| ||your complex series of commands|
| ||stop recording|
| ||execute your macro|
| ||execute your macro again|
Running a macro
Use this mapping as a convenient way to play a macro recorded to register
:nnoremap <Space> @q
- Start recording keystrokes by typing
- End recording with
q(first press Escape if you are in insert mode).
- Play the recorded keystrokes by hitting space.
Suppose you have a macro which operates on the text in a single line. You can run the macro on each line in a visual selection in a single operation:
- Visually select some lines (for example, type
vipto select the current paragraph).
:normal @qto run the macro from register
qon each line.
Viewing a macro
You can use the
:registers command to view the current contents of any or all register values in Vim. For example, use
:reg to view all registers, or
:reg a to view only what you have recorded into register
:reg abx will show the contents of registers a, b, and x.
Saving a macro
There are two primary ways of saving a macro for later use.
The simplest way occurs by default if you run Vim in nocompatible mode (which is the default if you have a vimrc). Simply by including the proper text in your
'viminfo' option or leaving the nocompatible default alone, Vim will automatically write all your registers to a file and restore them at startup.
By default, the content of each register is saved, and will be available next time you run Vim. For example, you might record a macro to register
a, then exit from Vim with
:q!. On restarting Vim, you can press
@a to run the macro from register
viminfo' option can disable the saving of registers. If
:set viminfo? shows a value including (for example)
s10 then up to 50 lines and 10KB will be saved in each register. If either item is zero, no registers are saved. :help 'viminfo'
The second way to save a macro for later use (especially if you think you might overwrite the macro by accident, therefore restoring the wrong value from the viminfo file) is to use a let command in your vimrc.
For a very simple example, suppose you have recorded a macro to jump to the first occurrence of the letter a in a line into register a, using the following key sequence in normal mode:
If you want to restore this macro whenever you start Vim, regardless of what might be in your viminfo file, you would edit your vimrc and add the following line:
Assuming that you already have the macro recorded, you can easily insert the register contents rather that typing them all again. While entering the above line, after typing
let @a=', simply type Ctrl-R Ctrl-R a to insert the contents of the
a register. The double Ctrl-R is to insert the contents literally, rather than interpreting them as if typed.
Single quote characters (') are recommended and used in the above examples, because they allow you to type almost everything literally, whereas in double-quotes (") you need to escape certain characters. Compare '\1\2\3"abc"' with "\\1\\2\\3\"abc\"", for example. However, if your register must itself contain single quote characters, you use a second single-quote character to escape it. For example, if you want your register to contain "Vim's quote handling is a little tricky" you would type
:let @a='"Vim''s quote handling is a little tricky"'
Note however, that the above method using
:let will not work as expected for any macros which you make ending in a <CR> or <NL> character (carriage return or newline). This is because, as documented in :help :let-@, Vim will treat the register as "linewise" under these conditions. The reason for this is to make registers set with
:let act "the right way" when dealing with yanked/deleted text, but it can cause headaches when dealing with recorded macros. Possible workarounds include using the setreg() function or adding "no-op" commands to the end of the macro, such as
a<Esc>. See the discussion on vim_dev about unexpected behavior of the :let command for details.
Appending to a macro
If you have recorded a macro that is almost right, but you need to add a few commands to it, you can easily append the commands to an existing macro instead of recording the whole thing over again. Simply replace the register letter with a capital letter.
For example, if you recorded into register
qa...q, you could add to the macro (without replacing it) using
Editing a macro
Suppose that you have recorded a macro in register
a and you would like to edit the macro. The procedure is explained above, but here is a summary of the steps:
- Press Ctrl-R Ctrl-R
ato insert the current contents of register
a(type Ctrl-R twice to insert the register exactly).
- Edit the text as required.
- Append an apostrophe (
') to finish the command, and press Enter.
:reg ato view the new value in the register.
@ato execute the contents of register
Note the caveat above about macros which end in <CR> or <NL>.
- Record a recursive macro
- Reversing order of blocks of text example of recording a macro to move blocks
- Using command-line history repeating ex commands without a macro
- Recursive repeats for quick editing of structured text
- Easy playback of recorded keys
- Recording keys for repeated jobs
Todo: Need a couple more simple examples.
Is it possible to have different sets of registers for different jobs, par example one set for editing xsl files with shortcuts for various <xsl:xxx> tags in them, another one for editing html, xml, sql etc.? --annonymous user
- No, this is not possible. Registers in Vim are global across the entire Vim instance, unlike mappings, abbreviations, etc. which can be local to a buffer. --Fritzophrenic (talk) 21:49, January 15, 2013 (UTC)
- I should say "not possible without some scripting". There is a BufEnter and BufLeave autocmd event, and also a FileType event, which could be combined with saving and restoring of every single named register if desired. I know of no existing plugins which do this. --Fritzophrenic (talk) 21:51, January 15, 2013 (UTC)
- I use BufEnter to map CTRL+/ to a command to comment out the selected lines when I am editing various files. Different file types require different comment strategies, so I use BufEnter to figure out which sequence should be used for each file.
au BufEnter *.js :vmap ^_ :s/^/\/\// <Enter> :nohlsearch <Enter> au BufEnter *.py :vmap ^_ :s/^/#/ <Enter> :nohlsearch <Enter>
- -- Michael S. Meeks 14:11, October 9, 2013 (UTC)