Shell Commands (:!)

Bang sends to the shell. Use as a filter or a one-shot.

Keys: :!, !

:!cmd runs cmd in a shell. :r !cmd inserts its output. :%!cmd filters the buffer through cmd. !{motion}cmd does 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