Last Updated: February 25, 2016
· oelmekki

Open all files matching a pattern

Grep has an useful option to let editors consume its output : -l. No need to run grep, then to paste each file name :

$ vim -p $(grep -rl foo)

Will open all files containing "foo" (replace with your favorite editor syntax).

It also works with git grep :

$ vim -p $(git grep -l foo)

will open all indexed files containing "foo".

Here is an helper script :

$ cat /usr/local/bin/vim_contains 
#!/usr/bin/env bash
vim -p $(grep -rl "$1")

It lets use this command to open all files containing "my_pattern", recursively from current directory :

vim_contains my_pattern
Say Thanks
Filed Under