1 # ------------------------------------------------------------------------------
5 # sudo or sudo -e (replacement for sudoedit) will be inserted before the command
7 # ------------------------------------------------------------------------------
11 # * Dongweiming <ciici123@gmail.com>
12 # * Subhaditya Nath <github.com/subnut>
13 # * Marc Cornellà <github.com/mcornella>
14 # * Carlo Sala <carlosalag@protonmail.com>
16 # ------------------------------------------------------------------------------
18 __sudo-replace-buffer() {
19 local old=$1 new=$2 space=${2:+ }
21 # if the cursor is positioned in the $old part of the text, make
22 # the substitution and leave the cursor after the $new text
23 if [[ $CURSOR -le ${#old} ]]; then
24 BUFFER="${new}${space}${BUFFER#$old }"
26 # otherwise just replace $old with $new in the text before the cursor
28 LBUFFER="${new}${space}${LBUFFER#$old }"
33 # If line is empty, get the last run command from history
34 [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
36 # Save beginning space
38 if [[ ${LBUFFER:0:1} = " " ]]; then
40 LBUFFER="${LBUFFER:1}"
44 # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
45 # Else use the default $EDITOR
46 local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
48 # If $EDITOR is not set, just toggle the sudo prefix on and off
49 if [[ -z "$EDITOR" ]]; then
51 sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
52 sudo\ *) __sudo-replace-buffer "sudo" "" ;;
53 *) LBUFFER="sudo $LBUFFER" ;;
58 # Check if the typed command is really an alias to $EDITOR
60 # Get the first part of the typed command
61 local cmd="${${(Az)BUFFER}[1]}"
62 # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
63 local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
64 # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
65 local editorcmd="${${(Az)EDITOR}[1]}"
67 # Note: ${var:c} makes a $PATH search and expands $var to the full path
68 # The if condition is met when:
69 # - $realcmd is '$EDITOR'
70 # - $realcmd is "cmd" and $EDITOR is "cmd"
71 # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
72 # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
73 # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
75 # - $realcmd is "cmd" and $EDITOR is "cmd"
76 # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
78 # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
79 if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
80 || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
81 || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
82 __sudo-replace-buffer "$cmd" "sudo -e"
86 # Check for editor commands in the typed command and replace accordingly
88 $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;;
89 \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;;
90 sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;;
91 sudo\ *) __sudo-replace-buffer "sudo" "" ;;
92 *) LBUFFER="sudo $LBUFFER" ;;
95 # Preserve beginning space
96 LBUFFER="${WHITESPACE}${LBUFFER}"
98 # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
103 zle -N sudo-command-line
105 # Defined shortcut keys: [Esc] [Esc]
106 bindkey -M emacs '\e\e' sudo-command-line
107 bindkey -M vicmd '\e\e' sudo-command-line
108 bindkey -M viins '\e\e' sudo-command-line