Vi and Vim Macro Tutorial: How to Record and Play

Using macro functions in Vim, you can record and replay a sequence of actions within the editor.

This article describes how to perform recording and playback inside the Vi and Vim editor with two detailed examples. To learn how to work in Vim, read the latest beginner’s guide to Vim.

Sequence of recording and playback steps inside Vim

  1. Start recording by pressing Q followed by a lowercase character to name the macro
  2. Perform any typical edits inside the Vim editor to be recorded
  3. Stop recording by pressing Q
  4. Play the recorded macro by pressing @ followed by the macro name
  5. To repeat macros multiple times, press: NN @ macro name, where NN is a number

Example 1: Sequence of generating numbers within a file using Vim Macro

1. Run the file andreyex-test.txt to create the sequence.

$ vim andreyex-test.txt

2. Go to insert mode and enter “1.” as shown below

Type: Esc and then 1.

$ vim andreyex-test.txt
1.

3. Start recording and log into history.

Type: Esc q then a

  • q indicates to start recording
  • and shows where to store records in register A
  • when you are ok, it will display the “entry” at the bottom of vi.

4. Copy the 1st line to the 2nd line to combine two lines with one, as shown below

Type: Esc yy then p

  • woo copies the current line
  • R will paste the line that was just copied
$ vim andreyex-test.txt
1.
1.

Note: Vim will still show the entry at the bottom, as shown below.

5. Increase the number.

Type: Control a
While positioning the cursor on the 2nd line, press Ctrl + A, which will increase the number to 2, as shown below.

$ vim andreyex-test.txt
1.
2.

Note: Vim will still display the entry at the bottom.

6. Stop recording

Type: q
Press the q button to stop recording. You will notice that there is currently no message entry at the bottom of Vim.

7. Repeat the record button 98 times.

Type: 98 @ a

  • Now repeat this work by typing 98 @ a
  • @a repeats macro “a” once.
  • 98 @ a repeats the state of macros “a” 98 times generating sequence number 1 – 100 as shown below using macros.

Vim Macro Record and Playback

Example 2: Repeat Vim Macro with Different Arguments

This example explains how one can execute the same command, with a different input for it, i.e. the same command, with different arguments.

Before executing the macro: andreyex-password.sql

$ vim andreyex-password.sql
Annette
Warren
Anthony
Preston
Kelly
Taylor
Stiller
Dennis
Schwartz

After recording and executing the macro: andreyex-password.sql

$ vim andreyex-password.sql
ALTER USER Annette IDENTIFIED BY 'Annette';
ALTER USER Warren IDENTIFIED BY 'Warren';
ALTER USER Anthony IDENTIFIED BY 'Anthony ';
ALTER USER Preston IDENTIFIED BY 'Preston';
ALTER USER Kelly IDENTIFIED BY 'Kelly ';
ALTER USER Taylor IDENTIFIED BY 'Taylor';
ALTER USER Stiller IDENTIFIED BY 'Stiller';
ALTER USER Dennis IDENTIFIED BY 'Dennis';
ALTER USER Schwart IDENTIFIED BY 'Schwart';

1. Open andreyex-password.sql, which has only names.

$ vim andreyex-password.sql
Annette
Warren
Anthony
Preston
Kelly
Taylor
Stiller
Dennis
Schwartz

2. Start recording and save it to register

Type: qa

  • q indicates to start recording
  • a shows store records in register a
  • When you click OK, it will display the recording message at the bottom of the VI.

3. Go to insert mode and type ALTER USER

Type: I (uppercase) followed by “ALTER USER”
Place the cursor anywhere on the first line and then press I. which will take you to the first character of the line. ALTER USER type

4. Copy the next word (ie title)

Type: Esc w yw

  • Press Esc and then press w to move to the next word (name).
  • yw, copies the current word (name).

5. Go to end, type IDENTIFIED BY

Type: Esc A followed by “IDENTIFIED BY”

  • Press Esc and A to move the cursor to the end of the line, then enter a space.
  • Type “IDENTIFIED BY”

6. Paste the copied name

Type: Esc p
Press Esc and then type p to paste the name you copied in step # 4.

7. Complete the quote at the end.

Type: Esc A, followed by ‘;
Press Esc and A to go to the end of the line, and ;

8. Move to the next line and stop recording.

Type: Esc j then q

  • j to go to the next line.
  • q to stop recording

Note: The write message shown at the bottom of the VI has now disappeared. At this stage, andreyex-password.sql will look like this.

Vim Recording Macros

9. Repeat a macro with arguments on the appropriate line

Type: 8 @ a

  • Now repeat this work 8 times by typing 8 @ a
  • @a repeats macro “a” once.
  • 8 @ a repeats the state of macros “a” 8 times, completing the rest of the line automatically, as shown below

Vim Macro Play recording

Sidebar