-
Notifications
You must be signed in to change notification settings - Fork 1
/
doas.plugin.zsh
61 lines (51 loc) · 1.53 KB
/
doas.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
# ------------------------------------------------------------------------------
# Description
# -----------
#
# doas will be inserted before the command
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Dongweiming <[email protected]>
# * Subhaditya Nath <github.com/subnut>
# * Marc Cornellà <github.com/mcornella>
# * Fyodor Doletov <github.com/Senderman> (doas fork)
# ------------------------------------------------------------------------------
if [ -z "${SUDOBIN}" ]; then
SUDOBIN="doas"
fi
__doas-replace-buffer() {
local old=$1 new=$2 space=${2:+ }
if [[ ${#LBUFFER} -le ${#old} ]]; then
RBUFFER="${space}${BUFFER#$old }"
LBUFFER="${new}"
else
LBUFFER="${new}${space}${LBUFFER#$old }"
fi
}
doas-command-line() {
# If line is empty, get the last run command from history
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
# Save beginning space
local WHITESPACE=""
if [[ ${LBUFFER:0:1} = " " ]]; then
WHITESPACE=" "
LBUFFER="${LBUFFER:1}"
fi
# Toggle doas prefix on/off
case "$BUFFER" in
"${SUDOBIN}"\ *) __doas-replace-buffer "${SUDOBIN}" "" ;;
*) LBUFFER="${SUDOBIN} $LBUFFER" ;;
esac
# Preserve beginning space
LBUFFER="${WHITESPACE}${LBUFFER}"
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
zle redisplay
}
zle -N doas-command-line
# Defined shortcut keys: [Esc] [Esc]
bindkey -M emacs '\e\e' doas-command-line
bindkey -M vicmd '\e\e' doas-command-line
bindkey -M viins '\e\e' doas-command-line