Last Updated: September 30, 2021
·
834
· ryrych

Using Vim buffer as a standard input and output for shell commands

Vim buffer can be used both as an input and output for other shell commands.

Output example

:!ls

Input example

Suppose that you liked the [Today I learned][1] idea and after some time you would like to get some statistics about your writing using [lingua][2] module.

#!/usr/bin/env ruby

require 'lingua'
input = ARGF.read

report = Lingua::EN::Readability.new(input)
puts report.report

By the way the file content above has been inserted with :read ~/lingua.rb. Now getting statistics is as easy as this:

:write ! ruby ~/linuga.rb

Number of paragraphs           259 
Number of sentences            136 
Number of words                1400 
Number of characters           9885 

Average words per sentence     10.29 
Average syllables per word     1.48 

Flesch score                   70.94 
Flesh-Kincaid grade level      5.92 
Fog Index                      7.75 

Please note that the whole buffer content has been redirected to standard input that lingua.rb accepts.

[1]: https://twitter.com/wryrych/status/718537786442313730
[2]: http://pressure.to/ruby/