1

can anyone give me an example of how !! works? The documentation says:

!!{filter}      Filter [count] lines through the external program {filter}.
7

!! (and more generally the ! operator) provides vim builtin support for all the unix filters that have evolved over the ages: this includes cut, paste, sort, uniq, awk, sed, even bash. Basically a filter is anything that can transform stdin to stdout (possibly doing something meaningful—the identity filter is just cat).

By providing access to these tools, vim avoids re-inventing them and allows you to combine them however you see fit. Table editing, cleaning a csv file, even interpreting lines of a shell script on the fly... the possibilities are about as limitless as the construction of *nix pipelines.


For a concrete example, type

echo Hello World

In a new file, then !!sh - and hit enter (I think I have that syntax right).

Also see this answer of mine for more complex techniques.

| improve this answer | |
  • That's a good answer. what's difference between "!" and "!!"? – user12457 Mar 25 '19 at 16:44
  • !! is like ! for a single line. ! can work on arbitrary ranges, so for example !3j, !ap or V/}<CR>! – D. Ben Knoble Mar 25 '19 at 16:46
  • @user12457 also see :help ! – D. Ben Knoble Mar 25 '19 at 22:54
3

A classic example for this filter is to sort the input using the Shell command sort. So vi does not have to implement its own sorting algorithm but could plugin external commands to do that.

Nowadays, Vim comes with its own sort implementation as :sort command, mainly because some systems did not provide a sort binary.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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