sudo.plugin.zsh 3.6 KB

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