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
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
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
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.
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'
And suddenly, the contents of your buffer is:
# ATTENTION, EVERYONE
THIS MARKDOWN FILE CONTAINS SOME **PRETTY INTERESTING** STUFF.
I __MEAN__ IT.
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'
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.
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)
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.
Cool! I’ll have to put those ones on my cheat sheet! Thanks!
This is awesome! I'm huge fan-girl of VIM and love reading tricks like this. :)
Glad you like it!
Awesome! Very useful. Thanks!
This is really useful! Was not aware vim had these superpowers built-in!