sudo.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. __sudo-replace-buffer() {
  18. local old=$1 new=$2 space=${2:+ }
  19. if [[ ${#LBUFFER} -le ${#old} ]]; then
  20. RBUFFER="${space}${BUFFER#$old }"
  21. LBUFFER="${new}"
  22. else
  23. LBUFFER="${new}${space}${LBUFFER#$old }"
  24. fi
  25. }
  26. sudo-command-line() {
  27. # If line is empty, get the last run command from history
  28. [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
  29. # Save beginning space
  30. local WHITESPACE=""
  31. if [[ ${LBUFFER:0:1} = " " ]]; then
  32. WHITESPACE=" "
  33. LBUFFER="${LBUFFER:1}"
  34. fi
  35. # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
  36. # Else use the default $EDITOR
  37. local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
  38. # If $EDITOR is not set, just toggle the sudo prefix on and off
  39. if [[ -z "$EDITOR" ]]; then
  40. case "$BUFFER" in
  41. sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
  42. sudo\ *) __sudo-replace-buffer "sudo" "" ;;
  43. *) LBUFFER="sudo $LBUFFER" ;;
  44. esac
  45. else
  46. # Check if the typed command is really an alias to $EDITOR
  47. # Get the first part of the typed command
  48. local cmd="${${(Az)BUFFER}[1]}"
  49. # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
  50. local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
  51. # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
  52. local editorcmd="${${(Az)EDITOR}[1]}"
  53. # Note: ${var:c} makes a $PATH search and expands $var to the full path
  54. # The if condition is met when:
  55. # - $realcmd is '$EDITOR'
  56. # - $realcmd is "cmd" and $EDITOR is "cmd"
  57. # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
  58. # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
  59. # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
  60. # or
  61. # - $realcmd is "cmd" and $EDITOR is "cmd"
  62. # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
  63. # or
  64. # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
  65. if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
  66. || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
  67. || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
  68. editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below
  69. fi
  70. # Check for editor commands in the typed command and replace accordingly
  71. case "$BUFFER" in
  72. $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudo -e" ;;
  73. \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudo -e" ;;
  74. sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "$EDITOR" ;;
  75. sudo\ *) __sudo-replace-buffer "sudo" "" ;;
  76. *) LBUFFER="sudo $LBUFFER" ;;
  77. esac
  78. fi
  79. # Preserve beginning space
  80. LBUFFER="${WHITESPACE}${LBUFFER}"
  81. # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
  82. zle redisplay
  83. }
  84. zle -N sudo-command-line
  85. # Defined shortcut keys: [Esc] [Esc]
  86. bindkey -M emacs '\e\e' sudo-command-line
  87. bindkey -M vicmd '\e\e' sudo-command-line
  88. bindkey -M viins '\e\e' sudo-command-line