Parent directory

sudo.plugin.zsh

3776 bytes
  1# ------------------------------------------------------------------------------
  2# Description
  3# -----------
  4#
  5# sudo or sudo -e (replacement for sudoedit) will be inserted before the command
  6#
  7# ------------------------------------------------------------------------------
  8# Authors
  9# -------
 10#
 11# * Dongweiming <ciici123@gmail.com>
 12# * Subhaditya Nath <github.com/subnut>
 13# * Marc Cornellà <github.com/mcornella>
 14# * Carlo Sala <carlosalag@protonmail.com>
 15#
 16# ------------------------------------------------------------------------------
 17
 18__sudo-replace-buffer() {
 19  local old=$1 new=$2 space=${2:+ }
 20
 21  # if the cursor is positioned in the $old part of the text, make
 22  # the substitution and leave the cursor after the $new text
 23  if [[ $CURSOR -le ${#old} ]]; then
 24    BUFFER="${new}${space}${BUFFER#$old }"
 25    CURSOR=${#new}
 26  # otherwise just replace $old with $new in the text before the cursor
 27  else
 28    LBUFFER="${new}${space}${LBUFFER#$old }"
 29  fi
 30}
 31
 32sudo-command-line() {
 33  # If line is empty, get the last run command from history
 34  [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
 35
 36  # Save beginning space
 37  local WHITESPACE=""
 38  if [[ ${LBUFFER:0:1} = " " ]]; then
 39    WHITESPACE=" "
 40    LBUFFER="${LBUFFER:1}"
 41  fi
 42
 43  {
 44    # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
 45    # Else use the default $EDITOR
 46    local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
 47
 48    # If $EDITOR is not set, just toggle the sudo prefix on and off
 49    if [[ -z "$EDITOR" ]]; then
 50      case "$BUFFER" in
 51        sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
 52        sudo\ *) __sudo-replace-buffer "sudo" "" ;;
 53        *) LBUFFER="sudo $LBUFFER" ;;
 54      esac
 55      return
 56    fi
 57
 58    # Check if the typed command is really an alias to $EDITOR
 59
 60    # Get the first part of the typed command
 61    local cmd="${${(Az)BUFFER}[1]}"
 62    # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
 63    local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
 64    # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
 65    local editorcmd="${${(Az)EDITOR}[1]}"
 66
 67    # Note: ${var:c} makes a $PATH search and expands $var to the full path
 68    # The if condition is met when:
 69    # - $realcmd is '$EDITOR'
 70    # - $realcmd is "cmd" and $EDITOR is "cmd"
 71    # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
 72    # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
 73    # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
 74    # or
 75    # - $realcmd is "cmd" and $EDITOR is "cmd"
 76    # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
 77    # or
 78    # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
 79    if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
 80      || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
 81      || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
 82      __sudo-replace-buffer "$cmd" "sudo -e"
 83      return
 84    fi
 85
 86    # Check for editor commands in the typed command and replace accordingly
 87    case "$BUFFER" in
 88      $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;;
 89      \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;;
 90      sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;;
 91      sudo\ *) __sudo-replace-buffer "sudo" "" ;;
 92      *) LBUFFER="sudo $LBUFFER" ;;
 93    esac
 94  } always {
 95    # Preserve beginning space
 96    LBUFFER="${WHITESPACE}${LBUFFER}"
 97
 98    # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
 99    zle && zle redisplay # only run redisplay if zle is enabled
100  }
101}
102
103zle -N sudo-command-line
104
105# Defined shortcut keys: [Esc] [Esc]
106bindkey -M emacs '\e\e' sudo-command-line
107bindkey -M vicmd '\e\e' sudo-command-line
108bindkey -M viins '\e\e' sudo-command-line