-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.plugin.zsh
49 lines (44 loc) · 1.5 KB
/
watch.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
# ------------------------------------------------------------------------------
# Description
# -----------
#
# plugin based on official sudo plugin
# watch will be inserted before the command
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Enrico Falco <enrico9034@gmail.com>
#
# ------------------------------------------------------------------------------
watch-command-line() {
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
# Save beginning space
local WHITESPACE=""
if [[ ${LBUFFER:0:1} == " " ]] ; then
WHITESPACE=" "
LBUFFER="${LBUFFER:1}"
fi
# Expand aliases
unset 'functions[_watch-plugin-expand]' # Clear function set by previous call
functions[_watch-plugin-expand]=${LBUFFER} # Save LBUFFER to functions file, this will expand the aliases
(($+functions[_watch-plugin-expand])) && LBUFFER=${functions[_watch-plugin-expand]#$'\t'} # Get the (expanded) function content back and clear the first tab(s)
if [[ $BUFFER == watch\ * ]]; then
if [[ ${#LBUFFER} -le 5 ]]; then
RBUFFER="${BUFFER#watch }"
LBUFFER=""
else
LBUFFER="${LBUFFER#watch }"
fi
else
LBUFFER="watch $LBUFFER"
fi
# Preserve beginning space
LBUFFER="${WHITESPACE}${LBUFFER}"
}
zle -N watch-command-line
# Defined shortcut keys: Alt+w
bindkey -M emacs '^[w' watch-command-line
bindkey -M vicmd '^[w' watch-command-line
bindkey -M viins '^[w' watch-command-line