I\'d like to use Vim in the middle of a pipe. This existing post looks like what I\'d like to do, except I was hoping to do it without Python\'s help -- only with bash. [It it
To print buffer to shell standard output, vim
needs to start in Ex mode, otherwise it'll open "normal" way with its own window and clear any output buffers on quit.
Here is the simplest working example:
$ echo foo | vim +%p -escq /dev/stdin
foo
which is equivalent to:
$ echo foo | vim -es '+%print' '+:q!' /dev/stdin
foo
Special file descriptor to standard input needs to be specified (/dev/stdin
) in order to prevent extra annoying messages.
And here are some examples with parsing strings:
$ echo This is example. | vim '+s/example/test/g' '+%p' -escq! /dev/stdin
This is test.
$ echo This is example. | vim - '+s/example/test/g' '+%p' -escq!
Vim: Reading from stdin...
This is test.
Related:
Take a look at vipe
which is part of moreutils. It allows you to use any editor as part of a pipe.
ls -al | vipe | less
To use it with vim
just make sure to set it as your default editor in your bashrc
or cshrc
or whatever shell you use.
EDITOR=vim
UPDATE: If you want a bash
only solution you could use a script like this
#!/bin/bash
# create temporary file
TMPFILE=`mktemp /tmp/vipe.bashXXXXXXXX`
cat > ${TMPFILE}
vim ${TMPFILE} < /dev/tty > /dev/tty
cat ${TMPFILE}
rm ${TMPFILE}
For a more portable version please replace
vim ${TMPFILE}
with
${EDITOR} ${TMPFILE}
You cannot simply put vim inside a pipe, because then Vim cannot display its UI.
ls | vim - | more # Does not work
One way to solve this is to use gvim -f -
inside the pipe, as it opens in a separate window. You need to write the file via :w >> /dev/stdout
and then :quit!
.
Alternatively (and the only way in a console-only non-graphical environment), you could write your own script / function vimAndMore
that takes the command that should be following vim in the pipe as an argument, and goes like this:
vimAndMore()
{
TMPFILE=/tmp/pipecontents
# Slurp stdin into the temp file.
cat - > "$TMPFILE" || exit $?
# Reconnect stdin to the terminal, so that Vim doesn't complain with "Warning:
# Input is not from a terminal", and the terminal is kept intact.
exec 0</dev/tty
# Launch the editor.
"${EDITOR:-vim}" "$TMPFILE" || exit $?
# Carry on with the pipe.
cat "$TMPFILE" | exec "$@"
rm "$TMPFILE"
}
And change the pipe to this:
ls | vimAndMore | more
> ls -1 fred*.c | vim -
will result in Vim opening an unnamed file containing a list of files fred*
Perhaps I misunderstood your question though...
Alternative interpretation:
See this page which describes a technique to
Without saving the current buffer you want to send its contents to an interpreter (python, scheme, whatever), and you want that interpreter to run in a freshly spawned xterm. A new xterm window is useful because running the shell within Vim does not allow simultaneously inspection of program output and the code buffer, and Vim's shell is weak in several respects.
From my very limited experience, it's not possible to make Vim write to stdout
. I don't think you can easily put Vim in a pipe.
You could try the command vipe
from this package.