vimtricks.wiki Concise Vim tricks, one at a time.

How do I prevent a macro from stopping when a command fails?

Answer

Use :s/pat/rep/e flag or :silent! prefix

Explanation

By default, Vim macros abort on the first error — a failed search, a substitute with no matches, or a movement that can't be performed. You can use the e flag or :silent! to make macros error-tolerant.

The problem

When running 100@q, if any command in the macro fails (like a :s with no match on a particular line), the entire macro stops.

Solution 1: The e flag on :s

" Without e flag — stops if no match on a line
:s/old/new/g

" With e flag — silently continues if no match
:s/old/new/ge

Solution 2: :silent! prefix

" Suppress errors from any command
:silent! s/old/new/g
:silent! normal /pattern<CR>
:silent! delete

silent! suppresses both the error message and the error abort.

Example: Robust multi-file macro

Without error handling (fragile):

qq :s/TODO/DONE/g<CR> :wnext<CR> q

With error handling (robust):

qq :silent! s/TODO/DONE/ge<CR> :wnext<CR> q

Now 100@q processes all files even if some don't contain "TODO".

Solution 3: try-catch in Ex commands

:try | s/old/new/g | catch | endtry

Tips

  • The e flag is specific to :s — use it whenever a substitute might not match
  • :silent! works with any Ex command — it's the universal error suppressor
  • Be careful with :silent! — it hides legitimate errors too
  • For macros that run :argdo or :bufdo, always use the e flag
  • Without error handling, macros that should process 100 lines might stop at line 3
  • Documented under :help :s_flags and :help :silent

Next

How do I run the same command across all windows, buffers, or tabs?