views:

77

answers:

3

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)

Added: Is it possible that a plugin/script - that act on quit or save put the buffer content to stdout?

A: 

Reading from stdin:

echo "hey" | vim  -

When you :w you'd still have to give it a filename.

Programs that use vim as their EDITOR, like crontab -e pass it a filename so that user can just :x and not worry about filenames.

EDIT

You could also do something like this:

mkfifo /tmp/some_pipe
echo "hey" > /tmp/some_pipe ; cat /tmp/some_pipe

And from another process (or terminal)

vim /tmp/some_pipe

Beware that writing to a pipe will block until something reads from it, and reading will block untill something writes to it, so it might be safer to use regular files.

miedwar
+1  A: 

You probably want to use sed, awk, perl, or some other filter that is designed to transform data from input to output. Vim is not a filter and trying to make it one is difficult or impossible.

msw
+2  A: 

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

Jabir Ali Ouassou
thanks - vipe uses temporary files too but moreutils i did not know yet and some of these utils look very useful
markus