How do I run a normal mode command on every line in a range?
Answer
:%normal command
Explanation
The :normal command executes normal mode commands programmatically on a range of lines. This is one of the most powerful batch-editing techniques in Vim.
How it works
:%normal A;appends a semicolon to every line in the file:10,20normal dddeletes lines 10 through 20- The command runs as if you pressed those keys on each line
Example
Add // comment prefix to lines 5-15:
:5,15normal I//
Delete the first word on every line:
:%normal dw
Tips
- Use
normal!to ignore user mappings (use built-in meanings) - Works with the global command:
:g/TODO/normal A // FIXME - Combine with visual selection:
:'<,'>normal @ato run macro on selection - The command string is literal — no need for quotes around it