How to write buffer content to stdout?
Solution 1
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
Solution 2
I think :w !tee
would work perfectly,
Solution 3
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
Solution 4
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.
Solution 5
Try something like:
{ FROMCMD | vim - 8>&1 >&9 | tac | tac | TOCMD; } 9>&1
and use ':w !cat >&8' when you are finished editing
'tac | tac' ensures that TOCMD doesn't receive any data until you quite vim, this is only useful if TOCMD writes to the terminal so that it doesn't make a mess while vim is still running.
Notice that running 'less' as your TOCMD (or any other program that do some terminal manipulation) is not going to work as expected because even if you delay the data, the processes still start "at the same time" and may access the terminal at the same time.
hooblei
Updated on June 15, 2021Comments
-
hooblei almost 3 years
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?
-
hooblei almost 14 yearsthanks - vipe uses temporary files too but moreutils i did not know yet and some of these utils look very useful
-
weynhamz over 11 yearsYou can even use pipe in this command. For example, I define the command 'Copy' to copy specific lines to the clipboard using gpaste
command -range=% Copy :<line1>,<line2>w !tee | gpaste
-
not-a-user about 10 yearsNice to learn about
moreutils
. However vipe uses temporary files, which does not fit my application of "decrypt file, modify content, encrypt result". -
xyxyber over 8 yearsI noted, that this solution may not work for all implementations of vim in all environments under all circumstances, but mostly it works well.
-
stephenmm over 8 yearsThis is intriguing but I am not sure how the the "1 s " is doing the sustitution or how the "x!" writes to stdout. (I did try looking up in vim help)
-
xyxyber over 8 yearsI tried it just now under Cygwin, Debian Jessie, Knoppix 7.4, Mageia 5 and Raspbian Jessie and it worked in all this systems in the bash. The result of the substitution is "Hallo yyy", blanks are allowed in vim commands between line range, command and comman parameters, the bar is the command separator in vim.
-
Daniel Que over 7 yearsI didn't know you could execute commands with +{command}. I'm glad I found this, since I was having trouble reading from stdin and writing to stdout at the same time.
-
xyxyber over 7 yearsUnder some system configurations problems can occur, if vim is used via CGI. But I didn't found the difference between this systems. The same scripts work on some systems perfectly and on others not. They can be avoided with storing to an intermediate temporary file.
-
Admin over 6 yearsif I forget to open file without sudo, this allows me to write it
:w !sudo tee someFileThatNeedsSudo
-
dosentmatter almost 6 yearsSeems like
%print
turns empty lines into a line with a single space? Do you know how to work around this? For example,echo -n | vim -es '+%print' '+:q!' /dev/stdin | cat -etv
andecho | vim -es '+%print' '+:q!' /dev/stdin | cat -etv
outputs<space>$
.cat -etv
shows all the invisible characters, so$
==<newline>
.echo -n
is an empty input,echo
is input with a single newline. -
xyxyber over 5 yearsUnder systems with a running X server the extra flag -X prevents from waiting for a X connection.
-
karakays about 5 years
moreutils/vipe
didn't work for me. its behavior is inconsistent on chained pipe operations which causes content of the target file to be lost. -
Clint Pachl about 5 yearsI use this to read and write encrypted files via vim. To open a PGP file:
pgpv < secret.pgp | vim -
. After editing::w !pgpe -co secret.pgp
. In this situation, don't forget to disable swap, backups, vimrc, history, etc. -
Konrad Rudolph almost 5 years
:w !tee
does not work with Vim 8 or NeoVim. Tested on macOS and Linux:echo hello | vim '+wq! !tee'
. Passing-e
also doesn’t work, and adding a colon after+
makes no difference either.