Vim Tips Wiki
(merge in from Edit a previously recorded macro (idea was here, but not as clear); tweak wording)
(minor fixes: one see-also has been merged to another)
Line 5: Line 5:
 
|created=2003
 
|created=2003
 
|complexity=basic
 
|complexity=basic
|author=torelg
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=16/11
 
|rating=16/11
Line 90: Line 90:
   
 
==Editing a macro==
 
==Editing a macro==
Suppose that you have recorded a macro in register <tt>a</tt> and you would like to edit the macro. The procedure is explained [[#Saving macros|above]], but here is a summary of the steps:
+
Suppose that you have recorded a macro in register <tt>a</tt> and you would like to edit the macro. The procedure is explained [[#Saving a macro|above]], but here is a summary of the steps:
 
#Type <tt>:let @a='</tt>
 
#Type <tt>:let @a='</tt>
 
#Press Ctrl-R Ctrl-R <tt>a</tt> to insert the current contents of register <tt>a</tt> (type Ctrl-R twice to insert the register exactly).
 
#Press Ctrl-R Ctrl-R <tt>a</tt> to insert the current contents of register <tt>a</tt> (type Ctrl-R twice to insert the register exactly).
Line 99: Line 99:
   
 
==See also==
 
==See also==
*[[VimTip1577|Record a recursive macro]]
+
*[[Record a recursive macro]]
 
*[[Reversing order of blocks of text]] example of recording a macro to move blocks
 
*[[Reversing order of blocks of text]] example of recording a macro to move blocks
*[[VimTip170|Repeating a sequence of commands without defining a macro]]
+
*[[Using command-line history]] repeating ex commands without a macro
*[[VimTip45|Using command-line history]] repeating ex commands without a macro
 
   
 
==References==
 
==References==
Line 112: Line 111:
   
 
==Comments==
 
==Comments==
  +
''Todo'': Need a couple more simple examples.
{{Todo}}
 
*Clean above, and add more content, with an example.
 

Revision as of 04:59, 15 August 2011

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.

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.

See also

References

Comments

Todo: Need a couple more simple examples.