sudo.plugin.zsh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # ------------------------------------------------------------------------------
  2. # Description
  3. # -----------
  4. #
  5. # sudo or sudoedit will be inserted before the command
  6. #
  7. # ------------------------------------------------------------------------------
  8. # Authors
  9. # -------
  10. #
  11. # * Dongweiming <ciici123@gmail.com>
  12. #
  13. # ------------------------------------------------------------------------------
  14. sudo-command-line() {
  15. [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
  16. if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
  17. if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
  18. RBUFFER=" ${BUFFER#$EDITOR }"
  19. LBUFFER="sudoedit"
  20. else
  21. LBUFFER="sudoedit ${LBUFFER#$EDITOR }"
  22. fi
  23. elif [[ $BUFFER == sudoedit\ * ]]; then
  24. if [[ ${#LBUFFER} -le 8 ]]; then
  25. RBUFFER=" ${BUFFER#sudoedit }"
  26. LBUFFER="$EDITOR"
  27. else
  28. LBUFFER="$EDITOR ${LBUFFER#sudoedit }"
  29. fi
  30. elif [[ $BUFFER == sudo\ * ]]; then
  31. if [[ ${#LBUFFER} -le 4 ]]; then
  32. RBUFFER="${BUFFER#sudo }"
  33. LBUFFER=""
  34. else
  35. LBUFFER="${LBUFFER#sudo }"
  36. fi
  37. else
  38. LBUFFER="sudo $LBUFFER"
  39. fi
  40. }
  41. zle -N sudo-command-line
  42. # Defined shortcut keys: [Esc] [Esc]
  43. bindkey -M emacs '\e\e' sudo-command-line
  44. bindkey -M vicmd '\e\e' sudo-command-line
  45. bindkey -M viins '\e\e' sudo-command-line