macro
:!
:! runs a shell command and shows its output; with a range, :[range]!cmd pipes those lines through cmd and REPLACES them with its output. The bridge from Vim to the rest of your Unix toolbox.
:!{cmd} :[range]!{cmd} (command mode) Variations
| Keystroke | Effect |
|---|---|
| :!! | Re-run the last :! command |
| :r !{cmd} | Read shell output INTO the buffer at cursor |
| :%!sort | Sort the whole buffer through sort(1) |
| :.!date | Replace current line with `date` output |
Examples
:!ls -la Run ls without leaving Vim
:%!jq . Format the whole buffer as JSON via jq
:r !curl -s https://api.example.com Read remote HTTP response into the buffer
Gotcha
% expands to the current filename in :! — a naked % in :!rm % deletes the file you're editing. Escape it (\%) if you need a literal percent sign.
Related
:w / :saveas
Write (save) the current buffer to disk. Without an argument :w saves to the current file; with an argument it writes a copy without switching buffers. Use :saveas when you want to rename the buffer to the new file and keep editing that one.
q / @
q{register} starts recording a macro into that register; press q again to stop. Play it back with @{register}, and repeat the last macro with @@. Macros are just recorded keystrokes — anything you can do by hand, you can automate.
:s and :%s
Substitute (find-and-replace) within a range of lines. Without a range :s works on the current line only; :%s uses % as shorthand for the whole file (equivalent to :1,$s). Add flags like g (all matches per line) and c (confirm).