How do I run a normal mode command from the ex command line without triggering my custom key mappings?
Answer
:norm! {command}
Explanation
:normal {command} runs a sequence of Normal mode keystrokes from the command line (or a range of lines with :%normal), but it applies your custom key mappings. Add a bang — :normal! {command} — and Vim executes the raw, built-in command without any remapping. This distinction is critical when writing portable scripts, functions, or macros that must behave identically regardless of the user's configuration.
How it works
:norm {cmd}: applies mappings — if you havennoremap x dd,:norm xrunsdd:norm! {cmd}: ignores all mappings —:norm! xalways deletes a single character- The
!variant guarantees predictable, mapping-independent behavior - Works with ranges:
:%norm! A;appends a semicolon to every line using the built-inA, regardless of anyAremapping - Accepts the full
<CR>,<Esc>, and other special key notations when invoked from a function viaexecute
Example
Suppose you have nnoremap w b in your config to reverse w and b. Running:
:%norm w
behaves like b (your remapping), not w. But:
:%norm! w
always moves to the next word boundary as the built-in w normally does.
Tips
- Always use
:norm!inside autocommands, plugins, and shared vimrc snippets to avoid mapping conflicts - Combine with a range to apply a Normal command to every line:
:'<,'>norm! >>indents all selected lines using the real>>, not a remapped version - Use
:execute "norm! \<C-a>"in scripts when the command includes special keys