Skip to content

Instantly share code, notes, and snippets.

@seanpianka
Last active August 26, 2019 22:18
Show Gist options
  • Save seanpianka/b98348852cf31fcb9310fa830478f2b7 to your computer and use it in GitHub Desktop.
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
# 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;
}
@seanpianka
Copy link
Author

seanpianka commented Aug 26, 2019

$ 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 pattern notify in all the files, and every argument afterwards is for limiting which files to search (using grep’s --include flag).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment