540b2200afb68a3282419ffb6c49bbf8f642b67e

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(sudo): respect `$SUDO_EDITOR` and `$VISUAL`, switch to `sudo -e` (#10596)

Diff

 1diff --git a/plugins/sudo/README.md b/plugins/sudo/README.md
 2index 012fc53254c7097501f7e71b7f7a1a666bdf11e3..27cd20c181d8f750b511547fb08fb73c13d045c4 100644
 3--- a/plugins/sudo/README.md
 4+++ b/plugins/sudo/README.md
 5@@ -24,6 +24,20 @@ By pressing the <kbd>esc</kbd> key twice, you will have the same command with `s
 6 $ sudo apt-get install build-essential
 7 ```
 8 
 9+The same happens for editing files with your default editor (defined in `$SUDO_EDITOR`, `$VISUAL` or `$EDITOR`, in that order):
10+
11+If the editor defined were `vim`:
12+
13+```console
14+$ vim /etc/hosts
15+```
16+
17+By pressing the <kbd>esc</kbd> key twice, you will have the same command with `sudo -e` instead of the editor, that would open that editor with root privileges:
18+
19+```console
20+$ sudo -e /etc/hosts
21+```
22+
23 ### Previous executed commands
24 
25 Say you want to delete a system file and denied:
26@@ -44,6 +58,8 @@ Password:
27 $
28 ```
29 
30+The same happens for file editing, as told before.
31+
32 ## Key binding
33 
34 By default, the `sudo` plugin uses <kbd>Esc</kbd><kbd>Esc</kbd> as the trigger.
35diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh
36index e02f88a872f2d337495627c3f3af53da22dc4b31..e8d183414d14e6aa47b7f1833b5149153f8a8809 100644
37--- a/plugins/sudo/sudo.plugin.zsh
38+++ b/plugins/sudo/sudo.plugin.zsh
39@@ -2,7 +2,7 @@
40 # Description
41 # -----------
42 #
43-# sudo or sudoedit will be inserted before the command
44+# sudo or sudo -e (replacement for sudoedit) will be inserted before the command
45 #
46 # ------------------------------------------------------------------------------
47 # Authors
48@@ -11,6 +11,7 @@
49 # * Dongweiming <ciici123@gmail.com>
50 # * Subhaditya Nath <github.com/subnut>
51 # * Marc Cornellà <github.com/mcornella>
52+# * Carlo Sala <carlosalag@protonmail.com>
53 #
54 # ------------------------------------------------------------------------------
55 
56@@ -35,10 +36,14 @@ sudo-command-line() {
57     LBUFFER="${LBUFFER:1}"
58   fi
59 
60+  # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
61+  # Else use the default $EDITOR
62+  local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
63+
64   # If $EDITOR is not set, just toggle the sudo prefix on and off
65   if [[ -z "$EDITOR" ]]; then
66     case "$BUFFER" in
67-      sudoedit\ *) __sudo-replace-buffer "sudoedit" "" ;;
68+      sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
69       sudo\ *) __sudo-replace-buffer "sudo" "" ;;
70       *) LBUFFER="sudo $LBUFFER" ;;
71     esac
72@@ -72,9 +77,9 @@ sudo-command-line() {
73 
74     # Check for editor commands in the typed command and replace accordingly
75     case "$BUFFER" in
76-      $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudoedit" ;;
77-      \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudoedit" ;;
78-      sudoedit\ *) __sudo-replace-buffer "sudoedit" "$EDITOR" ;;
79+      $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;;
80+      \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;;
81+      sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;;
82       sudo\ *) __sudo-replace-buffer "sudo" "" ;;
83       *) LBUFFER="sudo $LBUFFER" ;;
84     esac