Update 30.01.2022
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# sudo
|
||||
|
||||
Easily prefix your current or previous commands with `sudo` by pressing <kbd>esc</kbd> twice
|
||||
Easily prefix your current or previous commands with `sudo` by pressing <kbd>esc</kbd> twice.
|
||||
|
||||
To use it, add `sudo` to the plugins array in your zshrc 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
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Say you want to delete a system file and denied:
|
||||
@@ -43,3 +57,19 @@ $ sudo rm some-system-file.txt
|
||||
Password:
|
||||
$
|
||||
```
|
||||
|
||||
The same happens for file editing, as told before.
|
||||
|
||||
## Key binding
|
||||
|
||||
By default, the `sudo` plugin uses <kbd>Esc</kbd><kbd>Esc</kbd> as the trigger.
|
||||
If you want to change it, you can use the `bindkey` command to bind it to a different key:
|
||||
|
||||
```sh
|
||||
bindkey -M emacs '<seq>' sudo-command-line
|
||||
bindkey -M vicmd '<seq>' sudo-command-line
|
||||
bindkey -M viins '<seq>' sudo-command-line
|
||||
```
|
||||
|
||||
where `<seq>` is the sequence you want to use. You can find the keyboard sequence
|
||||
by running `cat` and pressing the keyboard combination you want to use.
|
||||
|
||||
@@ -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,14 +11,19 @@
|
||||
# * Dongweiming <ciici123@gmail.com>
|
||||
# * Subhaditya Nath <github.com/subnut>
|
||||
# * Marc Cornellà <github.com/mcornella>
|
||||
# * Carlo Sala <carlosalag@protonmail.com>
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
__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
|
||||
@@ -35,14 +40,21 @@ sudo-command-line() {
|
||||
LBUFFER="${LBUFFER:1}"
|
||||
fi
|
||||
|
||||
# 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\ *) __sudo-replace-buffer "sudo" "" ;;
|
||||
*) LBUFFER="sudo $LBUFFER" ;;
|
||||
esac
|
||||
else
|
||||
{
|
||||
# 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
|
||||
|
||||
# Check if the typed command is really an alias to $EDITOR
|
||||
|
||||
# Get the first part of the typed command
|
||||
@@ -67,24 +79,25 @@ 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
|
||||
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
|
||||
fi
|
||||
} always {
|
||||
# Preserve beginning space
|
||||
LBUFFER="${WHITESPACE}${LBUFFER}"
|
||||
|
||||
# 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
|
||||
|
||||
Reference in New Issue
Block a user