sudo.plugin.zsh 1.36 KB
Newer Older
dongweiming's avatar
dongweiming committed
1
2
3
4
# ------------------------------------------------------------------------------
# Description
# -----------
#
5
# sudo or sudoedit will be inserted before the command
dongweiming's avatar
dongweiming committed
6
7
8
9
10
11
12
13
14
15
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Dongweiming <ciici123@gmail.com>
#
# ------------------------------------------------------------------------------

sudo-command-line() {
16
17
18
19
20
21
22
23
    [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
    if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
        if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
            RBUFFER=" ${BUFFER#$EDITOR }"
            LBUFFER="sudoedit"
        else
            LBUFFER="sudoedit ${LBUFFER#$EDITOR }"
        fi
24
    elif [[ $BUFFER == sudoedit\ * ]]; then
25
26
27
28
29
30
31
32
33
34
35
36
37
        if [[ ${#LBUFFER} -le 8 ]]; then
            RBUFFER=" ${BUFFER#sudoedit }"
            LBUFFER="$EDITOR"
        else
            LBUFFER="$EDITOR ${LBUFFER#sudoedit }"
        fi
    elif [[ $BUFFER == sudo\ * ]]; then
        if [[ ${#LBUFFER} -le 4 ]]; then
            RBUFFER="${BUFFER#sudo }"
            LBUFFER=""
        else
            LBUFFER="${LBUFFER#sudo }"
        fi
Fabio Strozzi's avatar
Fabio Strozzi committed
38
39
40
    else
        LBUFFER="sudo $LBUFFER"
    fi
dongweiming's avatar
dongweiming committed
41
42
43
}
zle -N sudo-command-line
# Defined shortcut keys: [Esc] [Esc]
44
45
46
bindkey -M emacs '\e\e' sudo-command-line
bindkey -M vicmd '\e\e' sudo-command-line
bindkey -M viins '\e\e' sudo-command-line