One of Vim’s lesser-known operators is !. If y, d, and v are the jocks, then c, <,and > are the theater kids, and ! is the kid who has to eat with the teachers because no one likes them.

I assume you understand text objects and the basics of UNIX and piping.

! takes a range, asks for a UNIX command (which I’ll call a filter, since most of the useful commads are filters) on Vim’s command line, and runs the filter on it by using that range as STDIN. It replaces the range with the output of the command.

For example, placing your cursor (in Vim) on the next line and typing the sequence :.!wc -w would replace the line with the text 5

testing testing one two three

Let’s break that command down:

1. : opens Vim’s command mode.
2. . selects the line the cursor is on.
3. ! means the rest of the line is interpreted as a UNIX command.
4. wc -w counts the words in the selected region, which is 5.

## Usage Examples

### cURL

Say you want to copy the contents of a file from the web and you have the URL copied. One way to do it is to put your cursor on an empty region (to avoid replacing some text you don’t want to replace) and typing :.!curl (url of file you want)

### Word Count and Undo

a long block of text ...


Visually select the text, or use a text object like !ip (filter in paragraph). Then run the following command using the filter.

wc -w


That’ll tell you how many words are in that region. Hit u to undo the text replacement. After all, you wouldn’t want to lose all those hard-typed words, would you?

### Evaluate Code / Cheap Calculator

Say you’re in a Python file and you have a code block like the following:

if 2+2:
print("hi")
else:
pass


Filter command: !python.

You can use this as a quick way of evaluating a code block, or as a poor man’s calculator. This works independent of the filetype. I just tested it in a Markdown file.

### Lining up tables

You have some text in a table and it’s not well-aligned

1  2
3      4


!column -t will turn it into a nice table like this:

1 2
3 4


### Sort / Unique

!sort and !uniq and !sort | uniq (or the more concise !sort -u) do what you’d imagine they do.

## Go Out and Try It

I’m sure you’ll find some uses for it. If any of them come in especially handy, let me know