1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-11 00:10:08 +00:00

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

This commit is contained in:
Carlo Sala 2022-01-18 18:46:14 +01:00 committed by GitHub
parent 4f2d8b4d4c
commit 540b2200af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View file

@ -24,6 +24,20 @@ By pressing the <kbd>esc</kbd> key twice, you will have the same command with `s
$ sudo apt-get install build-essential $ 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 <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:
```console
$ sudo -e /etc/hosts
```
### Previous executed commands ### Previous executed commands
Say you want to delete a system file and denied: 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 ## Key binding
By default, the `sudo` plugin uses <kbd>Esc</kbd><kbd>Esc</kbd> as the trigger. By default, the `sudo` plugin uses <kbd>Esc</kbd><kbd>Esc</kbd> as the trigger.

View file

@ -2,7 +2,7 @@
# Description # Description
# ----------- # -----------
# #
# sudo or sudoedit will be inserted before the command # sudo or sudo -e (replacement for sudoedit) will be inserted before the command
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Authors # Authors
@ -11,6 +11,7 @@
# * Dongweiming <ciici123@gmail.com> # * Dongweiming <ciici123@gmail.com>
# * Subhaditya Nath <github.com/subnut> # * Subhaditya Nath <github.com/subnut>
# * Marc Cornellà <github.com/mcornella> # * Marc Cornellà <github.com/mcornella>
# * Carlo Sala <carlosalag@protonmail.com>
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -35,10 +36,14 @@ sudo-command-line() {
LBUFFER="${LBUFFER:1}" LBUFFER="${LBUFFER:1}"
fi 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 $EDITOR is not set, just toggle the sudo prefix on and off
if [[ -z "$EDITOR" ]]; then if [[ -z "$EDITOR" ]]; then
case "$BUFFER" in case "$BUFFER" in
sudoedit\ *) __sudo-replace-buffer "sudoedit" "" ;; sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
sudo\ *) __sudo-replace-buffer "sudo" "" ;; sudo\ *) __sudo-replace-buffer "sudo" "" ;;
*) LBUFFER="sudo $LBUFFER" ;; *) LBUFFER="sudo $LBUFFER" ;;
esac esac
@ -72,9 +77,9 @@ sudo-command-line() {
# Check for editor commands in the typed command and replace accordingly # Check for editor commands in the typed command and replace accordingly
case "$BUFFER" in case "$BUFFER" in
$editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudoedit" ;; $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;;
\$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudoedit" ;; \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;;
sudoedit\ *) __sudo-replace-buffer "sudoedit" "$EDITOR" ;; sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;;
sudo\ *) __sudo-replace-buffer "sudo" "" ;; sudo\ *) __sudo-replace-buffer "sudo" "" ;;
*) LBUFFER="sudo $LBUFFER" ;; *) LBUFFER="sudo $LBUFFER" ;;
esac esac