Fundamentals 8 min read

Master Vim Macros: Automate Repetitive Text Editing in Seconds

This guide explains how to use Vim macros to record and replay sequences of commands, enabling you to quickly repeat text insertion, generate numbered lists, or prepend identical strings to multiple lines, dramatically reducing manual editing effort.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Vim Macros: Automate Repetitive Text Editing in Seconds

Why Use Vim Macros?

When editing plain‑text files you often need to repeat the same text, generate a series of numbered lines, or prepend identical content to each line. Performing these actions manually is tedious and error‑prone, but Vim macros let you automate them with a few keystrokes.

Recording a Macro

Open a new or existing file in Vim.

Press q followed by a macro name (e.g., b) to start recording. The status line will show recording @b.

Perform the editing actions you want to repeat (insert text, move cursor, etc.).

Press q again to stop recording. The macro is now stored under the chosen name.

Running a Macro

Place the cursor where you want the macro to act, then type @ followed by the macro name to execute it once. To repeat it n times, type n@macro (e.g., 4@b runs macro b four times).

Example 1 – Generate 100 Identical Lines

Goal: create 100 lines containing the same phrase.

Create a file file1.txt.

Start recording with qz (macro name z).

Enter insert mode ( i) and type the desired text, e.g., Learning Vim Macros.

Press Esc then q to stop recording.

Now 100@z will produce 100 copies of that line instantly.

Example 2 – Generate a Number Sequence (101‑110)

Goal: produce a list of consecutive numbers.

Create file2.txt and type 101. in insert mode.

Exit insert mode with Esc, then start recording a macro qy (name y).

Copy the current line with yy, paste it below with p, then increment the number using Ctrl‑a.

Stop recording with q.

Running 8@y generates the numbers 101 through 108; adjust the count to reach 110.

Example 3 – Prepend an SQL Insert Statement to Each Line

Goal: turn raw data lines into full INSERT statements.

Open file3.txt containing the raw values.

Start a macro qd (name d).

Enter insert mode ( i) and type insert into items values(, then exit insert mode.

Use w to move to the next word and yw five times to copy the five parts of the line (value, comma, type, comma, brand).

Append ); at the end of the line with a then Esc.

Move to the next line with j and stop the macro with q.

Execute 3@d to apply the macro to the remaining three lines, instantly producing correctly formatted SQL statements.

Conclusion

Vim macros are a powerful tool for any developer who frequently edits repetitive text or code. By recording a sequence of keystrokes once and replaying it as needed, you can save considerable time and avoid manual errors.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Text Editingprogramming fundamentalsMacros
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.