mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-23 21:01:58 +00:00
cd17aed9e1
Co-authored-by: Marc Cornellà <marc.cornella@live.com>
57 lines
1.6 KiB
Bash
57 lines
1.6 KiB
Bash
# ------------------------------------------------------------------------------
|
|
# Description
|
|
# -----------
|
|
#
|
|
# sudo or sudoedit will be inserted before the command
|
|
#
|
|
# ------------------------------------------------------------------------------
|
|
# Authors
|
|
# -------
|
|
#
|
|
# * Dongweiming <ciici123@gmail.com>
|
|
#
|
|
# ------------------------------------------------------------------------------
|
|
|
|
sudo-command-line() {
|
|
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
|
|
|
|
# Save beginning space
|
|
local WHITESPACE=""
|
|
if [[ ${LBUFFER:0:1} == " " ]] ; then
|
|
WHITESPACE=" "
|
|
LBUFFER="${LBUFFER:1}"
|
|
fi
|
|
|
|
if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
|
|
if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
|
|
RBUFFER=" ${BUFFER#$EDITOR }"
|
|
LBUFFER="sudoedit"
|
|
else
|
|
LBUFFER="sudoedit ${LBUFFER#$EDITOR }"
|
|
fi
|
|
elif [[ $BUFFER == sudoedit\ * ]]; then
|
|
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
|
|
else
|
|
LBUFFER="sudo $LBUFFER"
|
|
fi
|
|
|
|
# Preserve beginning space
|
|
LBUFFER="${WHITESPACE}${LBUFFER}"
|
|
}
|
|
zle -N sudo-command-line
|
|
# Defined shortcut keys: [Esc] [Esc]
|
|
bindkey -M emacs '\e\e' sudo-command-line
|
|
bindkey -M vicmd '\e\e' sudo-command-line
|
|
bindkey -M viins '\e\e' sudo-command-line
|