Filtering Text Through a Command

Send text out, get it back transformed

:%!command pipes the whole buffer through a program and replaces it with the output:

:%!sort             " sort every line
:%!sort -u          " sort and drop duplicates
:%!column -t        " align into columns
:%!jq .             " reformat JSON

Just part of the file

The ! operator takes a motion, exactly like d or y:

!2jsort             " filter this line and the next two through sort
!ipsort             " filter the current paragraph

That is the same operator-plus-motion grammar you already know, now driving external programs.

The mental model

  • :!cmd — run it, show me the output
  • :r !cmd — run it, put the output in my file
  • :%!cmd — send my file through it, replace it with the result