How do I run an external shell command from Vim without the press-enter prompt afterward?
Answer
:silent !{cmd}
Explanation
Running :!cmd in Vim shows the command's output and then pauses with a "Press ENTER or type command to continue" prompt. Prefixing with :silent suppresses both the output and the prompt, letting you fire off shell commands without breaking your editing flow.
How it works
:silentis a command modifier that suppresses all displayed output and prompts from the following Ex command:!{cmd}runs{cmd}in the shell- Combined,
:silent !{cmd}executes the shell command invisibly - If the screen is left dirty after the command, add a
| redraw!call or press<C-l>
Example
Stage the current file in Git without interruption:
:silent !git add %
Open the current file in the system default application:
:silent !xdg-open %
Map a key to copy the whole file to the clipboard on macOS and auto-redraw:
nnoremap <leader>y :silent !pbcopy < % \| redraw!<CR>
Tips
%expands to the current filename, so:silent !git add %stages exactly the open file- To also suppress stderr, append shell redirection:
:silent !cmd 2>/dev/null :silent!(with a bang aftersilent) additionally ignores error messages from the command- For commands whose output you do want, drop
silentand just use:!cmd