How do I remove the last recorded keystroke from a register or macro in Vim?
:let @a = @a[:-2]
Macros and named registers are just strings, so you can surgically edit them instead of re-recording from scratch.
:let @a = @a[:-2]
Macros and named registers are just strings, so you can surgically edit them instead of re-recording from scratch.
:'<,'>normal! .
When you already made one correct edit, replaying it is usually safer than retyping it by hand.
:'<,'>s/\%V\s\+$//
Sometimes you need to clean alignment artifacts in a rectangular region without touching the rest of each line.
visual-mode #visual-mode #substitution #regex #formatting #editing
:%s/\v(\S+)\s+(\S+)/\2 \1/<CR>
When you need to flip two fields across a whole file, a single substitution is faster and safer than recording a macro.
editing #editing #substitution #regex #ex-commands #formatting
:hide edit {file}<CR>
Normally, trying to :edit another file from a modified buffer triggers a warning and blocks the switch unless you save or force the command.
buffers-windows #buffers #windows #workflow #command-line #editing
:set nrformats-=octal<CR>
By default, Vim may treat numbers with leading zeros as octal when you use and for increment/decrement.
:keepjumps normal! gg=G
Bulk formatting commands are common in cleanup sessions, but they often leave side effects in your navigation history.
:%s/\vfoo\zsbar/baz/g
When your match has a stable prefix but you only want to replace the trailing segment, \zs is often cleaner than introducing extra capture groups.
/pattern/e+1<CR>
Most Vim searches place the cursor at the start of the match.
:undojoin<CR>
By default, separate edits often become separate undo entries, which makes replaying or backing out a multi-part change noisy.
:call setreg('a', @a, 'V')
When you paste from a register, Vim uses that register's stored type: characterwise, linewise, or blockwise.
:let @a .= expand('%:t') . "\n"
Named registers are not just for yanks and deletes.
:bufdo keeppatterns %s/\s\+$//e | update
If you keep many files open during a refactor, cleaning trailing whitespace one buffer at a time is slow and error-prone.
gN
Most users know gn for selecting the next search match, but its counterpart gN is the real power move when you need to work backward through matches.
visual-mode #visual-mode #search #motions #editing #normal-mode
:silent keeppatterns %s/\s\+$//e
Trailing whitespace cleanup is a common housekeeping step, but a plain substitution can leave side effects: it can overwrite your last search pattern (@/) and t
command-line #command-line #ex-commands #editing #search #formatting
f{vi{U
When editing structured text, you often need to transform content inside delimiters without touching the delimiters themselves.
*NgUgn
gn is often treated as a visual selection command, but it is more powerful when used as a motion target for operators.
qagUiwWq2@a
Macros are most powerful when they encode both the edit and the movement to the next target.
mzgUiw`z
When you run an operator like gUiw, Vim can leave your cursor in a slightly different place than where you started.
:snomagic /foo.\+/bar/<CR>
When a pattern is mostly literal text with just a little regex, default magic mode can force extra escaping and make substitutions harder to read.