You can use the %p command to print the file being edited to the standard output.
In this example, Vim reads its standard input and sends it to the standard output:
$ (echo one; echo two; echo three; echo four) | \
vim - -nes -u NONE -c ':%p' -c ':q!' | \
tail -n +2 | \
grep t
two
three
Notes:
vim -: read from standard input
-n: don't create swapfile, use memory only
-e: start in ex mode
-s: silent or batch mode
-u NONE: don't read the .vimrc (If you want your .vimrc to be read, skip this option. But different people have different .vimrc-s, so if you don't write -u NONE, your code may not work for a different person or even for you if you change your .vimrc.)
-c <something>: run something as a command
-c ':%p': print the content of the buffer to the standard output
-c 'q!': quit without saving
tail -n +2: throw the first line of the Vim output away (that is the line 'Vim: Reading from stdin...')
In the following example, Vim does actually something useful: it deletes the first column of the standard input.
$ (echo one; echo two; echo three; echo four) | \
vim - -nes -u NONE -c ':exec "normal G\<c-v>ggx"' -c ':%p' -c ':q!' | \
tail -n +2
ne
wo
hree
our
Notes:
:exec: execute the following command (command-line command, not normal mode command)
normal: a command-line command that executes the given normal mode commands
G: go to the last line of the file
\<c-v>: start block selection
gg: go to the first line
x: delete the selected characters
EDIT:
Can I tell vim issue the ":%p" right after I ":wq" from the interactive session?
You can tell Vim things like "run command X before exiting", using e.g. the
VimLeave autocommand (see :help autocommand, :help VimLeave):
$ (echo "line 1"; echo "line 2") | \
vim - -nes -u NONE -c ':autocmd VimLeave * :%p'
Vim: Reading from stdin...
:q! # you type :q!
line 1
line 2
The problem is with the ":%p" command. If you use the "-e" argument, you won't
get the visual editor. If you don't use "-e", Vim won't print the result of
":%p" to the standard output but to the terminal.
I will make a different suggestion in another post.