-
Notifications
You must be signed in to change notification settings - Fork 2
/
vip
executable file
·73 lines (62 loc) · 1.8 KB
/
vip
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
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Author: Gokberk Cinbis, Jan 2021
set -e
function __print_help_and_exit__()
{
echo "vip <pattern> [<file1> <file2> ...]"
echo ""
echo "Open files containing <pattern> in vim as tabs."
echo "* grep is run in case-sensitive mode only if <pattern> contains at least one upper letter (similar to smartcase in vim)."
echo "* After loading, vim is made to execute /<pattern>"
echo "* If no filename is given, all files in the currrent directory are fed into grep (ie. *)"
echo ""
echo "EXAMPLES"
echo "* vip section # case insensitive"
echo "* vip sEction # case sensitive"
echo "* vip section *.tex # within *.tex files"
echo "* find . -name "*.tex" | xargs vip section # within .tex files, searched recursively"
exit 1
}
# global variables
reg_exp=""
grep_args=""
function __vim__()
{
if [ $# -eq 0 ]
then
echo "vip::No file matching $reg_exp has been found."
else
vim -c "/$reg_exp" -p "$@"
fi
}
function __main__()
{
local narg=$#
if [ $narg -eq 0 ]
then
__print_help_and_exit__
fi
reg_exp="$1" # global variable
# check reg_exp contains upper letter
if [[ "$reg_exp" =~ [[:upper:]] ]]
then
#echo "reg_exp contains upper letter(s), case-sensitive mode."
grep_args="-sl"
else
#echo "reg_exp does not contain an upper letter, case-insensitive mode."
grep_args="-sil"
fi
if [ $narg -eq 1 ]
then
__vim__ `command grep "$grep_args" "$reg_exp" *`
else
# init/set global variables
local execargs=()
for (( argi=2; argi<=$narg; argi++ )); do
execargs+=( "${!argi}" )
done
__vim__ `command grep "$grep_args" "$reg_exp" "${execargs[@]}"`
fi
}
# ok with multiple quoted arguments
__main__ "$@"