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
:silentstill executes the command — it only suppresses the output/messages- After
:silent!, checkv:errmsgto see if an error occurred::echo v:errmsg :silentinside 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 :unsilentforces output back on inside a:silentblock — useful for logging debug messages