From 540b2200afb68a3282419ffb6c49bbf8f642b67e Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Tue, 18 Jan 2022 18:46:14 +0100 Subject: feat(sudo): respect `$SUDO_EDITOR` and `$VISUAL`, switch to `sudo -e` (#10596) --- plugins/sudo/README.md | 16 ++++++++++++++++ plugins/sudo/sudo.plugin.zsh | 15 ++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'plugins/sudo') diff --git a/plugins/sudo/README.md b/plugins/sudo/README.md index 012fc5325..27cd20c18 100644 --- a/plugins/sudo/README.md +++ b/plugins/sudo/README.md @@ -24,6 +24,20 @@ By pressing the esc key twice, you will have the same command with `s $ sudo apt-get install build-essential ``` +The same happens for editing files with your default editor (defined in `$SUDO_EDITOR`, `$VISUAL` or `$EDITOR`, in that order): + +If the editor defined were `vim`: + +```console +$ vim /etc/hosts +``` + +By pressing the esc key twice, you will have the same command with `sudo -e` instead of the editor, that would open that editor with root privileges: + +```console +$ sudo -e /etc/hosts +``` + ### Previous executed commands Say you want to delete a system file and denied: @@ -44,6 +58,8 @@ Password: $ ``` +The same happens for file editing, as told before. + ## Key binding By default, the `sudo` plugin uses EscEsc as the trigger. diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh index e02f88a87..e8d183414 100644 --- a/plugins/sudo/sudo.plugin.zsh +++ b/plugins/sudo/sudo.plugin.zsh @@ -2,7 +2,7 @@ # Description # ----------- # -# sudo or sudoedit will be inserted before the command +# sudo or sudo -e (replacement for sudoedit) will be inserted before the command # # ------------------------------------------------------------------------------ # Authors @@ -11,6 +11,7 @@ # * Dongweiming # * Subhaditya Nath # * Marc Cornellà +# * Carlo Sala # # ------------------------------------------------------------------------------ @@ -35,10 +36,14 @@ sudo-command-line() { LBUFFER="${LBUFFER:1}" fi + # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR + # Else use the default $EDITOR + local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}} + # If $EDITOR is not set, just toggle the sudo prefix on and off if [[ -z "$EDITOR" ]]; then case "$BUFFER" in - sudoedit\ *) __sudo-replace-buffer "sudoedit" "" ;; + sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;; sudo\ *) __sudo-replace-buffer "sudo" "" ;; *) LBUFFER="sudo $LBUFFER" ;; esac @@ -72,9 +77,9 @@ sudo-command-line() { # Check for editor commands in the typed command and replace accordingly case "$BUFFER" in - $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudoedit" ;; - \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudoedit" ;; - sudoedit\ *) __sudo-replace-buffer "sudoedit" "$EDITOR" ;; + $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;; + \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;; + sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;; sudo\ *) __sudo-replace-buffer "sudo" "" ;; *) LBUFFER="sudo $LBUFFER" ;; esac -- cgit v1.2.3-70-g09d2 From 957dca698cd0a0cafc6d2551eeff19fe223f41bd Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 18 Jan 2022 19:03:27 +0100 Subject: style(sudo): clean code style and reorganise logic --- plugins/sudo/sudo.plugin.zsh | 50 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'plugins/sudo') diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh index e8d183414..2a0b3bfc4 100644 --- a/plugins/sudo/sudo.plugin.zsh +++ b/plugins/sudo/sudo.plugin.zsh @@ -17,9 +17,13 @@ __sudo-replace-buffer() { local old=$1 new=$2 space=${2:+ } - if [[ ${#LBUFFER} -le ${#old} ]]; then - RBUFFER="${space}${BUFFER#$old }" - LBUFFER="${new}" + + # if the cursor is positioned in the $old part of the text, make + # the substitution and leave the cursor after the $new text + if [[ $CURSOR -le ${#old} ]]; then + BUFFER="${new}${space}${BUFFER#$old }" + CURSOR=${#new} + # otherwise just replace $old with $new in the text before the cursor else LBUFFER="${new}${space}${LBUFFER#$old }" fi @@ -36,18 +40,21 @@ sudo-command-line() { LBUFFER="${LBUFFER:1}" fi - # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR - # Else use the default $EDITOR - local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}} + { + # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR + # Else use the default $EDITOR + local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}} + + # If $EDITOR is not set, just toggle the sudo prefix on and off + if [[ -z "$EDITOR" ]]; then + case "$BUFFER" in + sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;; + sudo\ *) __sudo-replace-buffer "sudo" "" ;; + *) LBUFFER="sudo $LBUFFER" ;; + esac + return + fi - # If $EDITOR is not set, just toggle the sudo prefix on and off - if [[ -z "$EDITOR" ]]; then - case "$BUFFER" in - sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;; - sudo\ *) __sudo-replace-buffer "sudo" "" ;; - *) LBUFFER="sudo $LBUFFER" ;; - esac - else # Check if the typed command is really an alias to $EDITOR # Get the first part of the typed command @@ -72,7 +79,8 @@ sudo-command-line() { if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \ || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \ || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then - editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below + __sudo-replace-buffer "$cmd" "sudo -e" + return fi # Check for editor commands in the typed command and replace accordingly @@ -83,13 +91,13 @@ sudo-command-line() { sudo\ *) __sudo-replace-buffer "sudo" "" ;; *) LBUFFER="sudo $LBUFFER" ;; esac - fi - - # Preserve beginning space - LBUFFER="${WHITESPACE}${LBUFFER}" + } always { + # Preserve beginning space + LBUFFER="${WHITESPACE}${LBUFFER}" - # Redisplay edit buffer (compatibility with zsh-syntax-highlighting) - zle redisplay + # Redisplay edit buffer (compatibility with zsh-syntax-highlighting) + zle redisplay + } } zle -N sudo-command-line -- cgit v1.2.3-70-g09d2