How to write buffer content to stdout? How to write buffer content to stdout? shell shell

How to write buffer content to stdout?


Since you use Linux/Unix, you might also be interested in trying out moreutils. It provides a command called vipe, which reads from stdin, lets you edit the text in $EDITOR, and then prints the modified text to stdout.

So make sure you set your editor to Vim:

export EDITOR=vim

And then you can try these examples:

cat /etc/fstab | vipecut -d' ' -f2 /etc/mtab | vipe | less


I think :w !tee would work perfectly,


To print buffer to shell standard output, vim needs to start in Ex mode, otherwise it'll open the "normal" way with its own window and clear any output buffers on quit.

Here is the simplest working example:

$ echo foo | vim -es '+%print' '+:q!' /dev/stdinfoo

The special file descriptor to standard input needs to be specified (/dev/stdin) in order to prevent extra annoying messages.

And here are some string parsing examples:

$ echo This is example. | vim -es '+s/example/test/g' '+%print' '+:q!' /dev/stdinThis is test.$ echo This is example. | vim - -es '+s/example/test/g' '+%print' '+:q!'Vim: Reading from stdin...This is test.

Here is a simple example using ex which is equivalent to vi -e:

ex -s +%p -cq /etc/hosts

Related: