Home » Linux » redirection and vim

redirection and vim

Posted by: admin November 30, 2017 Leave a comment

Questions:

I was wondering if there’s a way to see the output of any command,
straight inside vim, rather than first redirecting it into a file and
then opening that file.

E.x. I need something like
$ gvim < diff -r dir1/ dir2/

This gives ambiguous redirect error message

I just want to see the diffs between dir1 and dir2 straight inside
gvim.

Can any one provide a nice hack?

Answers:
diff file1 file2 | vim -R -

The -R makes it read-only so you don’t accidentally modify the input (which may or may not be your desired behavior). The single dash tells vim to reads its input over standard input. Works for other commands, too.

Questions:
Answers:

Also, when already in Vim:

:r! diff file1 file2

Questions:
Answers:

vim -d file1 file2

Questions:
Answers:

Although I would also suggest vimdiff or vim -d for the case of looking at a diff, I just have to share this (more general) approach for using vim usage in pipes: vipe (from the moreutils package in Ubuntu).

For example:

find -name ‘*.png’ | vipe | xargs rm

would allow you to first edit (in vim) the list of .png files found before passing it to xargs rm.

Questions:
Answers:

jst use gvimdiff instead
or vimdiff
to paste the output of a command straight into vim, for example ls, try
:%r!ls

Questions:
Answers:

BTW, there is a DirDiff plugin.

Questions:
Answers:

You can do this with

diff -r dir1/ dir2/ | gvim -

the ‘-‘ option to vim (or gvim) tells vim to open STDIN

Questions:
Answers:

I often use vimdiff -g <file1> <file2>

Questions:
Answers:

One of the most simple and convenient ways is to do it like this:

vimdiff -R <file1> <file2>

Again the ‘-R’ flag is to open it for read-only mode to avoid any accidental changes.

Questions:
Answers:

What you are looking for is called process substitution:

vim <(diff -r dir1/ dir2/)

But the DirDiff plugin mentioned by Luc is much more useful for comparing directories.