-
Notifications
You must be signed in to change notification settings - Fork 0
/
m
executable file
·75 lines (66 loc) · 1.63 KB
/
m
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
72
73
74
75
#!/usr/bin/env bash
# alias mw='m web' # search web
# alias mf='m file' # search file and open it
# alias mm='m man' # search cheat
# alias ms='m vim' # open result in vim
DIR=$(dirname ~)
HOME=$DIR/$USER
WIKIHOME="$HOME/wiki"
WIKIWEB="https://github.com/yantze/wiki"
case $1 in
"man")
if [[ -z $2 ]]; then
echo "require second word"
exit;
fi
cheat $2
;;
"vim")
vim +Ack\ "$2"\ ~/wiki
;;
"web")
search=${@:2}
open "https://github.com/yantze/wiki/search?q=$search"
;;
"webfile")
keyword=${2%.md}
filepath=$(find $WIKIHOME -type f -iname "*$keyword*.md" -o -iname "*$keyword*.txt" | head -n 1)
if [[ -n $filepath ]]; then
open "${WIKIWEB}/blob/master/${filepath##*wiki/}"
fi
;;
"file")
if [[ -z $2 ]]; then
vim +FZF "$WIKIHOME"
exit
fi
if [[ ${2: -1} = "!" ]]; then
keyword="${2%?}" # full name search
elif [[ ${2: -1} = "$" ]]; then
keyword="*${2%?}" # suffix name search
elif [[ ${2:0:1} = "^" ]]; then
keyword="${2:1}*" # prefix name search
else
keyword="*${2}*"
fi
filepath=$(find "$WIKIHOME" -type f \( -iname "$keyword" -not -iname "*.js" \) -not -path "$WIKIHOME/node_modules/*" | grep -v "un~" | grep -v "\.git/" | head -n 1)
if [[ -n $filepath ]]; then
vim $filepath
fi
;;
*)
if [[ $1 ]]; then
# hash ag 2>/dev/null || {
# if not installed ag
# grep -r -n -B 1 -A 2 "$1" $manfile
# grep --color=auto -rn -B 1 -A 2 "$1" $manfile
# -h 隐藏文件名
# -r 递归查找
# -n 显示行号
# }
# else
# ag --pager less -B 1 -A 2 "$1" $WIKIHOME
rg -p -B 1 -A 2 "$1" $WIKIHOME | less
fi
;;
esac