Shell Commands (:!)
Bang sends to the shell. Use as a filter or a one-shot.
:!cmdrunscmdin a shell.:r !cmdinserts its output.:%!cmdfilters the buffer throughcmd.!{motion}cmddoes the same in normal mode.
Shell Commands
Vim doesn't try to do everything itself โ it gives you the shell as a tool you can pipe through. Three patterns: run, insert output, filter buffer.
| Command | Effect |
|---|---|
:!ls -la |
Run shell command, show output |
:!python % |
Run current file (% expands to filename) |
:r !date |
Insert output of date below cursor |
:r !curl -s https://api... |
Pull URL response into buffer |
:%!sort |
Replace buffer with shell-sorted version |
:%!jq . |
Pretty-print the JSON buffer |
:'<,'>!sort -u |
Sort + dedup visual selection |
!}sort |
Normal mode: filter to next paragraph through sort |
Watch
See also: External Filter Tricks, File Operations