Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write buffer content to stdout?

Tags:

shell

vim

vi

stdout

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?

like image 337
hooblei Avatar asked Jul 10 '10 14:07

hooblei


3 Answers

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
like image 54
jabirali Avatar answered Nov 15 '22 05:11

jabirali


I think :w !tee would work perfectly,

like image 25
weynhamz Avatar answered Nov 15 '22 03:11

weynhamz


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:

  • How to edit files non-interactively (e.g. in pipeline)? at Vim SE
  • Pipe Vim buffer to stdout at stackoverflow
like image 13
kenorb Avatar answered Nov 15 '22 03:11

kenorb