Technology
 

Macros

From Vim Tips Wiki

(Redirected from Make a Macro)

Tip 398 Previous Next created 2003 · complexity basic · author torelg · version 6.0


In Vim, the word "macro" may refer to:

  • A sequence of commands recorded to a register.
  • A mapping to expand a sequence of typed keys to a longer sequence.
  • A script written in the Vim script language (stored in a file with extension .vim).

This tip is concerned with the first meaning (recording a command into a register).

There are often many ways to accomplish a task in Vim. Recording a macro is a great way to accomplish a one-time task, to get things done quickly when you don't want to mess with Vim script or mappings, or if you just plain don't know how to do it more elegantly (yet).

Contents

[edit] 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:

qqrecord to register q
your complex series of commands
qstop recording
@qexecute your macro
@@execute your macro again

[edit] Appending to an existing 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 then add to the macro (without replacing it) using qA...q.

[edit] Quickly run a macro

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

:nnoremap <Space> @q

Start recording keystrokes by typing qq.

End recording with q (first press Escape if you're in insert mode).

Play the recorded keystrokes by hitting space.

[edit] Viewing macros

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 the registers, or :reg a to view only what you have recorded into register 'a'.

[edit] Saving macros

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.

[edit] See also

[edit] References

[edit] Comments

 TO DO 

  • Clean above, and add more content, with an example.
  • Note that can edit a macro by changing register contents.
  • Add more :help links? Note to visitors: this is really easy to do, for example, just add {{help|'viminfo'}} to add a link as follows: :help 'viminfo'.