vimtricks.wiki Concise Vim tricks, one at a time.

How do I run a normal mode command on every visually selected line?

Answer

:'<,'>normal {command}

Explanation

After making a visual selection, :'<,'>normal {command} runs any normal-mode command on each selected line individually. This is one of Vim's most versatile power tools — you can append text, delete characters, apply operators, or chain multiple motions across dozens of lines in one shot.

How it works

  • :'<,'> is the range for the last visual selection (automatically filled when you press : in visual mode)
  • normal (or norm) executes the following as a normal-mode command sequence on each line in the range
  • Each line starts with the cursor at column 1 for the command, so motions are relative to the beginning of each line

Example

Append a trailing comma to each selected line:

foo
bar
baz

Visually select all three lines, then press : and type normal A,<CR>:

foo,
bar,
baz,

Or prepend // to comment out each line:

:'<,'>normal I// 
// foo
// bar
// baz

Tips

  • Use norm! instead of norm to ignore user mappings and use built-in Vim commands
  • You can chain commands: :'<,'>normal ^d$ (delete everything after the first non-blank character)
  • Combine with a count: :'<,'>normal 3>> to indent each line 3 levels
  • Works with registers: :'<,'>normal @q runs macro q on each line — but norm {command} is more powerful for one-off transformations that don't need a macro

Next

How do I control how many lines Ctrl-U and Ctrl-D scroll?