Is there any chance to write the content of the current vim buffer to stdout?
I'd like to use vim to edit content that was passed via stdin - without the need of a temporary file to retrieve the modified content (on Linux/Unix).
Is it possible that a plugin/script - that act on quit or save put the 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 | vipe
cut -d' ' -f2 /etc/mtab | vipe | less
< /dev/null vipe
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/stdin
foo
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/stdin
This 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With