2

This question already has an answer here:

I saw an editor command which started like this:

:%! sort

What does that mean? Specifically, the :%!.

marked as duplicate by Jair López, Community Nov 1 '16 at 0:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7

From :help :%:

%       equal to 1,$ (the entire file)        *:%*

From :help :range!:

:{range}![!]{filter} [!][arg]               *:range!*
            Filter {range} lines through the external program
            {filter}.

That is, %! sort and 1,$! sort are equivalent and they pass the text in the current buffer, from line 1 to the last line (that's what $ stands for), through the external sort command. After that, those lines are replaced by the command output. This is another explanation by @Carpetsmoker

That's useful not only for sorting a file. See some other examples here:

  • I still don't understand what this means for the above example I've given, how does it interact with the command line? – theonlygusti Oct 31 '16 at 17:02
  • 1
    @theonlygusti, you might want to also look at :help filter. An excerpt: "A filter is a program that accepts text at standard input, changes it in some way, and sends it to standard output. You can use the commands below to send some text through a filter, so that it is replaced by the filter output. Examples of filters are "sort", which sorts lines alphabetically...." – Wildcard Oct 31 '16 at 22:32
3
:%! command

pipes the current file's contents to command's stdin.

So, :%! sort is pretty much the same as (from a shell) cat file | sort.

Not the answer you're looking for? Browse other questions tagged or ask your own question.