Applying a Normal-mode Command to a Range


Normal mode commands can be used to apply an operation over each line in a range.

Suppose you are creating a list in python by yanking and pasting some content from another buffer. After pasting, the items are in place but are missing the trailing , required by python:

Initial Conditions
a·=·[
····"one"
····"two"
····"three"
]
NORMAL33%2:1
 

These could be manually added in various ways, but Vim's :normal command provides a handy solution by allowing a normal mode command to be applied to each line in a range. To demonstrate, we first create a visual selection:

Select Visual Rangev2j
a·=·[
····"one"
····"two"
····"three"
]
VISUAL67%4:1
 

Note that the range could have also been entered manually. Next create the command to execute.

We would like to move the cursor to the end of each line, enter insert mode after the line, then enter the , character. Inserting after the line can be achieve with the A command, so the command to execute for each line is:

A,

and the complete command is:

:normal A,

Executing this command in our buffer provides the desired result:

Execute the Command
a·=·[
····"one",
····"two",
····"three", 
]
NORMAL67%4:12