How do I run a normal mode command that bypasses all user-defined key mappings?
Answer
:norm!
Explanation
The :normal! command (abbreviated :norm!) executes a sequence of Normal mode keystrokes while completely ignoring user-defined mappings and abbreviations. Without the !, :normal respects your custom key maps, which can cause unexpected behavior when you use it in scripts or functions. Using :norm! guarantees that every keystroke is interpreted as Vim's built-in default, regardless of the user's configuration.
How it works
:norm— runs keystrokes in Normal mode, obeying any custom mappings:norm!— runs keystrokes in Normal mode, bypassing all mappings and abbreviations- Accepts a range:
:%norm! >>runs>>(indent) on every line using the built-in behavior
Example
Suppose your vimrc remaps j to move by display lines:
nnoremap j gj
When a script uses :norm j, Vim fires your mapping and executes gj instead of moving one logical line. With !:
:norm j " executes gj (your mapping fires)
:norm! j " executes j (built-in: move one logical line)
A safe, mapping-independent function looks like:
function! IndentAll()
:%normal! >>
endfunction
Tips
- Always use
:norm!in Vimscript functions and plugins to prevent user mappings from interfering with your automation - To pass special keys like
<Esc>or<CR>, use:execute::exe "norm! \<lt>Esc>" - Works with ranges:
:'<,'>norm! I//prepends//to each selected line using the built-inI, not any remapped version - Without
!: if the user has remappedd,:norm ddmay not delete the line as expected