To open files in Grep's output to Vim's -o -mode

Vim

Vim Problem Overview


How can you put a list of files to Vim's -o -mode?

I have a list of files as Grep's output. I run unsuccessfully

#1

grep -il sid * | vim -o

#2

grep -il sid * | xargs vim -o

#3

grep -il sid * | xargs vim

#4

vim -o `grep -il sid *`

#5

vim -o | grep -il sid *

Vim Solutions


Solution 1 - Vim

Try

vim -p `grep -il sid *`

if you want to open the files in different tabs in the same window.

Solution 2 - Vim

Run:

grep -il sid * | vim -

This tell vim to read the file from stdin, so the output of grep will be in vim. Now, put cursor on file and press gF - this will open the file on the line grep indicated.

You can also use ^WF to open file in a new split.

Solution 3 - Vim

Second one works for me. Third too, although you get only one file visible at the start. 4 is the same as 2 in most cases. First and last should not work by design.

Solution 4 - Vim

Sorry I'm late. Some of these options work for me but with weird side effects. Try this:

vim -o $(grep -il sid *)

Solution 5 - Vim

To invoke vim using xargs, xargs must be invoked with -o:

grep -il sid * | xargs -o vim -o

In case filenames could contain spaces, use grep -Z with xargs -0

grep -ilZ sid * | xargs -0 -o vim -o

Without xargs -o, we would get a warning:

Vim: Warning: Input is not from a terminal

The -o option exists on OSX/BSD. Also, the -o option is found in GNU findutils built from git. This option was added after 4.6.0.

Solution 6 - Vim

The second one should work unless some of those filenames contain whitespace. You should do

grep -il sid * | xargs -d "\n" vim -o

if this is a possibility. -d "\n" tells xargs to use a newline as the delimiter when reading in the arguments; normally it is any whitespace (newline, tab, or space character)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLéo Léopold Hertz 준영View Question on Stackoverflow
Solution 1 - VimasymmetricView Answer on Stackoverflow
Solution 2 - VimdimbaView Answer on Stackoverflow
Solution 3 - VimlioriView Answer on Stackoverflow
Solution 4 - VimSamoView Answer on Stackoverflow
Solution 5 - VimSunil ShetyeView Answer on Stackoverflow
Solution 6 - VimintuitedView Answer on Stackoverflow