How do I run normal mode commands on a range while ignoring custom mappings?
Answer
:%norm! A;
Explanation
The :norm! (or :normal!) command executes normal mode keystrokes while ignoring all user-defined mappings. The ! is critical in scripts and when applying changes to ranges — it ensures the command behaves consistently regardless of what mappings exist in your vimrc. Without it, remapped keys could produce unexpected results.
How it works
:norm— Executes the following characters as if typed in normal mode.!— The bang forces Vim to use the default key meanings, bypassing allnmap,nnoremap, and plugin mappings.:%norm! A;— Appends a semicolon to the end of every line in the file, using the defaultAbehavior regardless of any remapping.
Example
Append semicolons to every line in a file:
Before:
let x = 1
let y = 2
let z = 3
After :%norm! A;
let x = 1;
let y = 2;
let z = 3;
Comment out a range of lines (lines 5-15):
:5,15norm! I//
Tips
- Always use
:norm!(with bang) in scripts and plugin code to avoid depending on user mappings. - Works with visual selection: select lines, then
:norm!applies to each selected line. - You can chain multiple keystrokes:
:norm! 0dwA,deletes the first word and appends a comma on each line. - The command stops at the first error — use
:silent! norm!to continue past lines where the command fails.