Vim Tips Wiki
(→‎Comments: expand on answer, technically probably possible)
(add table of contents (not sure why it's gone))
Line 18: Line 18:
 
*A mapping to expand a sequence of typed keys to a longer sequence (see [[Mapping keys in Vim - Tutorial (Part 1)|tutorial]]).
 
*A mapping to expand a sequence of typed keys to a longer sequence (see [[Mapping keys in Vim - Tutorial (Part 1)|tutorial]]).
 
*A script written in the Vim script language (stored in a file with extension <code>.vim</code> – see {{help|script}}).
 
*A script written in the Vim script language (stored in a file with extension <code>.vim</code> – see {{help|script}}).
  +
__TOC__
 
 
==Recording a macro==
 
==Recording a macro==
 
Each register is identified by a letter <code>a</code> to <code>z</code>.
 
Each register is identified by a letter <code>a</code> to <code>z</code>.

Revision as of 16:19, 11 February 2013

Duplicate tip

This tip is very similar to the following:

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

Tip 398 Printable Monobook Previous Next

created 2003 · complexity basic · version 6.0


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 a to z.

To enter a macro, type:

q<letter><commands>q

To execute the macro <number> times (once by default), type:

<number>@<letter>

So, the complete process looks like:

qq start recording to register q
... your complex series of commands
q stop recording
@q execute your macro
@@ execute your macro again

Running a macro

Use this mapping as a convenient way to play a macro recorded to register q:

:nnoremap <Space> @q
  • Start recording keystrokes by typing qq.
  • 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 vip to select the current paragraph).
  • Type :normal @q to run the macro from register q on 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 a. Typing :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 a.

The 'viminfo' option can disable the saving of registers. If :set viminfo? shows a value including (for example) <50 and 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:

qa0faq

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:

let @a='0fa'

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 a using qa...q, you could add to the macro (without replacing it) using qA...q.

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:

  1. Type :let @a='
  2. Press Ctrl-R Ctrl-R a to insert the current contents of register a (type Ctrl-R twice to insert the register exactly).
  3. Edit the text as required.
  4. Append an apostrophe (') to finish the command, and press Enter.
  5. Enter :reg a to view the new value in the register.
  6. Type @a to execute the contents of register a.

Note the caveat above about macros which end in <CR> or <NL>.

See also

References

Comments

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)