There's a bunch of ways to target a single file from a list of paths:
Pipe Them into Vim
To target a single file from a list of paths, you can pipe them all into Vim
(using your grep example):
grep -rl "needle" . | vim -
You can then navigate to the single file name you're looking for with normal motions and open it with gf
.
The slight downside of this approach is that you then have an unsaved buffer with the paths in it. To prevent Vim complaining about this when you try to quit, you can tell Vim this file doesn't need to be saved by setting 'nomodified'
or by specifying a nofile
'buftype'
:
grep -rl "needle" . | vim - +'set nomod'
grep -rl "needle" . | vim - +'set buftype=nofile'
Use the quickfix
Vim allows you to pre-populate the quickfix with a list of files:
vim +copen -q <(grep -rn "needle" .)
You can then jump between the matches with :cn
and :cp
, or switch to the
quickfix window, navigate to the file you want and open it with Enter.
The downside of this is that it will load the first file into a buffer along
with any of the others that you visit via the quickfix.
:read !
the List Inside Vim
Another way you can get a shell command's output into Vim is with its :read !
command.
:r !grep -rl "needle" .
Like in the pipe solution, you can then open the file you're interested in with gf
.
Select the File Outside Vim
There are any number of ways you could do this, but they're generally outside
the scope of this site. One popular way is to install a
fuzzy finder, such as fzf.