Last active
December 17, 2015 23:59
-
-
Save glts/5693322 to your computer and use it in GitHub Desktop.
A grep with Vim regular expressions
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
#!/bin/bash | |
vgrep() { | |
local re=${1:-^} | |
local files=() | |
for arg in "${@:2}"; do | |
[ -f "${arg}" ] && files+=( "${arg}" ) | |
done | |
if [ -z "${files[*]:1}" ]; then | |
vim -Ni NONE -nes <<<"argdo g/${re////\\/}/p" -- "${files[@]}" | |
else | |
vim -Ni NONE -nes <<END -- "${files[@]}" 2>&1 | sed -e '1d' -e 's/\r$//' | |
argdo g/${re////\\/}/verbose echo expand('%').':'.getline('.') | |
verbose echo "\n" | |
END | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Companion article at http://glts.github.io/2013/06/08/using-vim-as-grep.html.