DEV Community

Cover image for Vim Filters
Ryan Palo
Ryan Palo

Posted on • Updated on • Originally published at assertnotmagic.com

Vim Filters

Quick tip time! Let's talk about Vim filters. This is a way to execute any external command and pipe the results into your current buffer. This is a great way to get the power of your shell into vim without having to learn much VimScript. Here are the basics:

Reading command input into the current buffer

This is the simplest method:

:r !ls
Enter fullscreen mode Exit fullscreen mode

This command will read the results of ls into your buffer at your cursor location. You can also specify the specific line to insert after. The next command will read the results of the ls command into your current buffer after line 4.

:4r !ls
Enter fullscreen mode Exit fullscreen mode

Sending buffer contents as input to an external command

You can also send lines of your buffer out to be replaced with the results of the command. A common thing to do is to operate on the whole buffer you're working on.

:%!sort
Enter fullscreen mode Exit fullscreen mode

The % selects the whole buffer, and then the ! sends the selected lines out to the external sort command. The whole buffer contents will be replaced with the results of the command.

For example. Let's say you're working on a text file.

# Attention, Everyone!

This markdown file contains some **pretty interesting** stuff.

I __mean__ it.
Enter fullscreen mode Exit fullscreen mode

But something's just not quite there. It needs some more zoom -- some more whammy! Try combining it with a slick Ruby one liner!

:%!ruby -ne 'puts $_.upcase'
Enter fullscreen mode Exit fullscreen mode

And suddenly, the contents of your buffer is:

# ATTENTION, EVERYONE

THIS MARKDOWN FILE CONTAINS SOME **PRETTY INTERESTING** STUFF.

I __MEAN__ IT.
Enter fullscreen mode Exit fullscreen mode

This will work with any command. You can use shell commands, or you can run it through a Python or Node script. It gives you the power to select the best (or your favorite) tool for the job, instead of locking you into Vim's capabilities alone. And you can use other motions if you just want to replace a portion of your buffer.

:!!ruby -ne 'puts $_.upcase'
Enter fullscreen mode Exit fullscreen mode

Two exclamation points will operate on the current line.

It also works on visually selected lines. Select a couple lines in visual mode:

v         " Visual mode activated
jj        " Select next two lines
:!sort    " Sorts the lines that were selected.
Enter fullscreen mode Exit fullscreen mode

Protip: If you accidentally run a command and blow away your file somehow, don't panic. Simply press u in Normal mode to undo the operation.

Hope this comes in handy!


Originally posted on assert_not magic?

Top comments (6)

Collapse
 
joschasa profile image
André

By the way, the last two examples can be achieved by only using vim features as well:

:sort will sort all lines in the current buffer and gU will uppercase any text given, so 'gggUG' would uppercase the whole buffer.

Collapse
 
rpalo profile image
Ryan Palo

Cool! I’ll have to put those ones on my cheat sheet! Thanks!

Collapse
 
jesswest profile image
Jess West (she/her)

This is awesome! I'm huge fan-girl of VIM and love reading tricks like this. :)

Collapse
 
rpalo profile image
Ryan Palo

Glad you like it!

Collapse
 
einenlum profile image
Yann Rabiller

Awesome! Very useful. Thanks!

Collapse
 
rrampage profile image
Raunak Ramakrishnan

This is really useful! Was not aware vim had these superpowers built-in!