paperlined.org
apps > vim
document updated 12 years ago, on Jul 8, 2012
This is a HACK. (I like hacks :)

Did you know that Vim can be used as a filter, to read things from STDIN, and output to STDOUT?

This page describes how to do this via VimScript

HOWTO: write to STDOUT

It's not possible to do this directly. The only way to do this is to open Vim with a temporary file, have Vim write to that, have the shell read the temporary file, and then delete it.

HOWTO: write to STDERR

HOWTO: read from STDIN

If you specify - as the filename, then it will read the contents of STDIN, and use that as the buffer.

HOWTO: display a Vim variable, and exit

Sometimes you want to know the contents of a Vim variable, from the command-line. How do you do this?

vimpager is a famous example of this.

Steps to do this:

  1. make sure you start with -e
  2. starting it with -X is optional, but a very good idea
  3. use -c 'echo &some_variable' to actually output what you want
  4. use -c q to tell Vim to exit at the end
  5. In the program that calls Vim, you have to strip out the escape characters. There are several ways to do this:

TL;DR: