-
Notifications
You must be signed in to change notification settings - Fork 0
/
grim
executable file
·62 lines (52 loc) · 2.13 KB
/
grim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# TODO^2: make this (and others like it) a printable help message:
# This script, "grim" is GRep into vIM. It is a handy way to find instances of a keyword (with grep) across a directory
# tree, and then open those files at the right location with vim. For example, if you run "grim someuniquegobbledygook"
# from within your $SKIGGETY_UTILS_DIR, it should bring you right here.
. $SKIGGETY_UTILS_DIR/lib/skiggety-utils.bash || exit 1
VIM_OPTIONS="-on"
GREP_OPTIONS="-RlI -D skip"
RG_OPTIONS="-l"
VIM_SEARCH_STRING_SUFFIX=""
while [[ "$1" == -* ]]; do
# accept a -i option for case insensitive grep
if [ "$1" == "-i" ]; then
GREP_OPTIONS="$GREP_OPTIONS $1"
RG_OPTIONS="$RG_OPTIONS $1"
VIM_SEARCH_STRING_SUFFIX="\c"
shift
elif [[ "$1" == "-." ]]; then # might want hidden directories
RG_OPTIONS="$RG_OPTIONS $1"
shift
elif [[ "$1" == --exclude* ]]; then
echo_debug "running grep with option: $1 $2"
GREP_OPTIONS="$GREP_OPTIONS $1 $2"
shift
shift
else
echo "ERROR: option not supported: $1" >&2
echo "TODO: IMPLEMENT more passthrough options for vim or grep?" >&2; exit 1
fi
done
if [ -z "$*" ];then
echo "ERROR: no arguments passed to $(basename $0)" >&2
exit 1
fi
GRIM_TARGET_STRING="$1"; shift
# TODO: DELETE?
if ! [ -z "$*" ]; then
echo "ERROR: no flags allowed after search string"
exit 1
fi
# TODO: rip out old grep?
# don't open vim swap files
# GRIM_FILES=`grep $GREP_OPTIONS "$GRIM_TARGET_STRING" .| grep -v .swp\$`
GRIM_FILES=`rg $RG_OPTIONS "$GRIM_TARGET_STRING" .| grep -v .swp\$ | sort`
# TODO: Maybe other arguments passed in can be sent to vim, so you can to gvim or -O
# TODO: what if a regex gets passed in? can you get the same one to work for grep and vim?
if [ -z "$GRIM_FILES" ]; then
echo No files to open!
else
vim $VIM_OPTIONS $GRIM_FILES +/"$GRIM_TARGET_STRING$VIM_SEARCH_STRING_SUFFIX" # TODO^2: support readonly vim mode by passing a "-R" flag if this program recieves a "-R"
echo_debug "ran: vim $VIM_OPTIONS $GRIM_FILES +/\"$GRIM_TARGET_STRING$VIM_SEARCH_STRING_SUFFIX\""
fi