forked from willghatch/zsh-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.plugin.zsh
75 lines (67 loc) · 1.89 KB
/
snippets.plugin.zsh
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
#!/usr/bin/env zsh
# snippets for expansion anywhere in the command line
# taken from http://zshwiki.org/home/examples/zleiab and expanded somewhat
#
# use: add-snippet <key> <expansion>
# then, with cursor just past <key>, run snippet-expand
typeset -Ag snippets
snippet-add() {
# snippet-add <key> <expansion>
snippets[$1]="$2"
}
snippet-expand() {
emulate -L zsh
setopt extendedglob
local MATCH
LBUFFER=${LBUFFER%%(#m)[.\-+:|_a-zA-Z0-9]#}
LBUFFER+=${snippets[$MATCH]:-$MATCH}
}
zle -N snippet-expand
help-list-snippets(){
local help="$(print "Add snippet:";
print "snippet-add <key> <expansion>";
print "Snippets:";
print -a -C 2 ${(kv)snippets})"
if [[ "$1" = "inZLE" ]]; then
zle -M "$help"
else
echo "$help" | ${PAGER:-less}
fi
}
run-help-list-snippets(){
help-list-snippets inZLE
}
zle -N run-help-list-snippets
# set up some default snippets
snippet-add l "less "
snippet-add tl "| less "
snippet-add g "grep "
snippet-add tg "| grep "
snippet-add gl "grep -l"
snippet-add tgl "| grep -l"
snippet-add gL "grep -L"
snippet-add tgL "| grep -L"
snippet-add gv "grep -v "
snippet-add tgv "| grep -v "
snippet-add eg "egrep "
snippet-add teg "| egrep "
snippet-add fg "fgrep "
snippet-add tfg "| fgrep "
snippet-add fgv "fgrep -v "
snippet-add tfgv "| fgrep -v "
snippet-add ag "agrep "
snippet-add tag "| agrep "
snippet-add ta "| ag "
snippet-add p "${PAGER:-less} "
snippet-add tp "| ${PAGER:-less} "
snippet-add h "head "
snippet-add th "| head "
snippet-add t "tail "
snippet-add tt "| tail "
snippet-add s "sort "
snippet-add ts "| sort "
snippet-add v "${VISUAL:-${EDITOR:-nano}} "
snippet-add tv "| ${VISUAL:-${EDITOR:-nano}} "
snippet-add tc "| cut "
snippet-add tu "| uniq "
snippet-add tx "| xargs "