Last active
August 26, 2019 22:18
-
-
Save seanpianka/b98348852cf31fcb9310fa830478f2b7 to your computer and use it in GitHub Desktop.
Combine vim and grep to quickly open files containing expression/pattern in vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim-grep, open all files containing grepped string | |
function vim-grep { | |
GREP_OPTIONS="-rnw $1 -e $2" | |
for var in "${@:3}"; | |
do | |
GREP_OPTIONS+=" --include \"$var\"" | |
done | |
vim $(eval "grep $GREP_OPTIONS" | cut -f1 -d: | uniq | tr "\n" " ") | |
unset GREP_OPTIONS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ vim-grep . notify Jenkinsfile\* \*.go
$1: directory to start searching recursively from
$2: expression to search for
$3..$n: filetypes to
--include
(if not provided, search any filetype)It will grep recursively from a directory for files containing a pattern, then open each of the files in separate vim buffers.
Example:
$ vim-grep . notify Jenkinsfile\*
, it will start recursively searching from.
, grepping for the patternnotify
in all the files, and every argument afterwards is for limiting which files to search (using grep’s--include
flag).