diff options
Diffstat (limited to 'plugins/sudo')
-rw-r--r-- | plugins/sudo/README.md | 22 | ||||
-rw-r--r-- | plugins/sudo/sudo.plugin.zsh | 56 |
2 files changed, 51 insertions, 27 deletions
diff --git a/plugins/sudo/README.md b/plugins/sudo/README.md index ebfdfd10d..4f3973ef8 100644 --- a/plugins/sudo/README.md +++ b/plugins/sudo/README.md @@ -2,25 +2,13 @@ Easily prefix your current or previous commands with `sudo` by pressing <kbd>esc</kbd> twice -## Enabling the plugin +To use it, add `sudo` to the plugins array in your zshrc file: -1. Open your `.zshrc` file and add `sudo` in the plugins section: - - ```zsh - plugins=( - # all your enabled plugins - sudo - ) - ``` - -2. Reload the source file or restart your Terminal session: - - ```console - $ source ~/.zshrc - $ - ``` +```zsh +plugins=(... sudo) +``` -## Usage examples +## Usage ### Current typed commands diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh index 0b843822e..f2445a762 100644 --- a/plugins/sudo/sudo.plugin.zsh +++ b/plugins/sudo/sudo.plugin.zsh @@ -9,23 +9,59 @@ # ------- # # * Dongweiming <ciici123@gmail.com> +# * Subhaditya Nath <github.com/subnut> +# * Marc Cornellà <github.com/mcornella> # # ------------------------------------------------------------------------------ +__sudo-replace-buffer() { + local old=$1 new=$2 space=${2:+ } + if [[ ${#LBUFFER} -le ${#old} ]]; then + RBUFFER="${space}${BUFFER#$old }" + LBUFFER="${new}" + else + LBUFFER="${new}${space}${LBUFFER#$old }" + fi +} + sudo-command-line() { - [[ -z $BUFFER ]] && zle up-history - if [[ $BUFFER == sudo\ * ]]; then - LBUFFER="${LBUFFER#sudo }" - elif [[ $BUFFER == $EDITOR\ * ]]; then - LBUFFER="${LBUFFER#$EDITOR }" - LBUFFER="sudoedit $LBUFFER" - elif [[ $BUFFER == sudoedit\ * ]]; then - LBUFFER="${LBUFFER#sudoedit }" - LBUFFER="$EDITOR $LBUFFER" + [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)" + + # Save beginning space + local WHITESPACE="" + if [[ ${LBUFFER:0:1} = " " ]]; then + WHITESPACE=" " + LBUFFER="${LBUFFER:1}" + fi + + # Get the first part of the typed command and check if it's an alias to $EDITOR + # If so, locally change $EDITOR to the alias so that it matches below + if [[ -n "$EDITOR" ]]; then + local cmd="${${(Az)BUFFER}[1]}" + if [[ "${aliases[$cmd]} " = (\$EDITOR|$EDITOR)\ * ]]; then + local EDITOR="$cmd" + fi + fi + + if [[ -n $EDITOR && $BUFFER = $EDITOR\ * ]]; then + __sudo-replace-buffer "$EDITOR" "sudoedit" + elif [[ -n $EDITOR && $BUFFER = \$EDITOR\ * ]]; then + __sudo-replace-buffer "\$EDITOR" "sudoedit" + elif [[ $BUFFER = sudoedit\ * ]]; then + __sudo-replace-buffer "sudoedit" "$EDITOR" + elif [[ $BUFFER = sudo\ * ]]; then + __sudo-replace-buffer "sudo" "" else LBUFFER="sudo $LBUFFER" fi + + # Preserve beginning space + LBUFFER="${WHITESPACE}${LBUFFER}" } + zle -N sudo-command-line + # Defined shortcut keys: [Esc] [Esc] -bindkey "\e\e" sudo-command-line +bindkey -M emacs '\e\e' sudo-command-line +bindkey -M vicmd '\e\e' sudo-command-line +bindkey -M viins '\e\e' sudo-command-line |