How might I more easily use a filename found by grep
as an argument to vim
:
$ grep -r foo * lib/beatles/john.c lib/pantera/phil.c lib/pinkfloyd/roger.c lib/pinkfloyd/richard.c lib/whitestripes/elephant/jack.c $ vim lib/pinkfloyd/roger.c
To autocomplete with Bash, I need to type " l \t
p \t
i \t
r \t
o \t
" because many other files match. Is there an easer way to say "Give me the third found file?" Maybe something like one of these:
$ vim $3 // Third line $ vim 'roger' // Only line with string 'roger' $ vim TabTabTab // Each Tab completes a different filename from grep
I do use Tmux and I know that I can go up and select the filename and then paste it. But that is still a bit clumsy, even as a VIM user. There must be a better way!
Note that I am not looking for a solution to script the grep output, rather this is something that I will be using manually as I grep for and open files with VIM.
EDIT: I am looking for a general solution, not one that will always target 'roger' or always target the third item.
grep -r foo /path/of/search/* | grep 'roger'
ThirdFile=grep -r foo /path/of/search/* | head -3 | tail -1