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

How do I run a Vim command without showing error messages or screen output?

Answer

:silent! {command}

Explanation

:silent suppresses all output from a command. :silent! additionally ignores errors — it runs the command silently and continues even if it fails. These are essential in autocommands, mappings, and scripts where error messages would disrupt the editing experience.

Variants

Command Behavior
:silent {cmd} Run cmd, suppress output; errors still abort
:silent! {cmd} Run cmd, suppress output AND ignore errors
:execute 'silent! ' . cmd Programmatic silent execution

Common uses

Source a file only if it exists:

silent! source ~/.vim/local.vim

Strip trailing whitespace on save without error if no matches:

autocmd BufWritePre * silent! %s/\s\+$//e

Call a function that may not exist in all Vim versions:

silent! call some#plugin#function()

Navigate without error messages when at the first/last quickfix entry:

nnoremap ]q :silent! cnext<CR>
nnoremap [q :silent! cprev<CR>

Tips

  • :silent still executes the command — it only suppresses the output/messages
  • After :silent!, check v:errmsg to see if an error occurred: :echo v:errmsg
  • :silent inside a mapping prevents the command line from flashing during the key press
  • Avoid :silent! on interactive commands (like :s///c) — it hides the confirmation prompt
  • :unsilent forces output back on inside a :silent block — useful for logging debug messages

Next

How do I run a search and replace only within a visually selected region?