Pipe a buffer through any Unix tool
Vim integrates with the shell so deeply that for many text-processing tasks you don't need a plugin — you pipe the buffer through sort, jq, awk, python, or whatever, and read the output back in. Vim becomes a frontend on top of every Unix tool you already know.
Three flavors of shell integration
:!cmd— run a shell command, show the output (does not modify the buffer).:r !cmd— run a shell command, insert its stdout below the cursor.:%!cmd— pipe the entire buffer through the command and replace it with the output.:'<,'>!cmddoes the same for a visual selection.
The killer one-liners
:%!sort sorts the file. :%!sort -u sorts and dedupes. :%!python -m json.tool formats JSON. :%!jq . reformats and validates JSON. :%!sed 's/foo/bar/g' for sed power-users. :'<,'>!sort sorts a visual selection only.
The :% token
The % in :%!cmd, :%s/..., :%y always means the entire file. Other range tokens you'll see: . = current line, $ = last line, 'a,'b = from mark a to mark b, '<,'> = the visual selection.
The global command
:g is one of the most powerful and underused commands in Vim. It runs a command on every line that matches a pattern. The anti-form :v (or :g!) runs on lines that don't match. Combined with :normal it lets you apply Normal-mode actions to filtered subsets of lines.
:g is the find-and-do command. :%s is find-and-replace. :g is find-and-do-anything. Once you internalize :g/pattern/cmd, you'll see refactor opportunities the rest of your team manually grep-and-edits.Advanced substitution recipes
Capture groups make substitution programmatic. Combined with \v (very-magic) the syntax is finally readable. Common recipes:
- Swap two words:
:%s/\v(\w+) (\w+)/\2 \1/g. - Capitalize first letter of every word:
:%s/\v(\w+)/\u\1/g. - Uppercase entire matches:
:%s/.*/\U&/. - Wrap matches:
:%s/\v(\d+)/[\1]/g.