Filtering through an external command
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
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:
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?