Filtering through an external command

4.6
(35)

Vim has plenty of capabilities built-in, but at times it’s useful to filter content through an external command. Some common examples include prettifying JSON or XML using json_pp or xmllint or converting text between formats. The most common use-case I have is to convert plain HTML provided by a designer into a HAML template for use in a Rails application.

To filter content through an external command, use Vim’s bang command by prepending the command with a !. You can visually select lines and then run the filter, or filter the entire buffer using %.

For example, running the following would pass the entire buffer into the external command html2haml, and then replace the contents

:%!html2haml

Under the hood, this is working by sending the contents of the buffer to the command via stdin and replacing the contents of the buffer with the value of the command’s stdout.

Here’s a quick example showing that in action:

How useful was this tip?

Average rating 4.6 / 5. Vote count: 35

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts