vi-mode.plugin.zsh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Control whether to force a redraw on each mode change.
  2. #
  3. # Resetting the prompt on every mode change can cause lag when switching modes.
  4. # This is especially true if the prompt does things like checking git status.
  5. #
  6. # Set to "true" to force the prompt to reset on each mode change.
  7. # Unset or set to any other value to do the opposite.
  8. #
  9. # The default is not to reset, unless we're showing the mode in RPS1.
  10. typeset -g VI_MODE_RESET_PROMPT_ON_MODE_CHANGE
  11. # Control whether to change the cursor style on mode change.
  12. #
  13. # Set to "true" to change the cursor on each mode change.
  14. # Unset or set to any other value to do the opposite.
  15. typeset -g VI_MODE_SET_CURSOR
  16. typeset -g VI_KEYMAP=main
  17. function _vi-mode-set-cursor-shape-for-keymap() {
  18. [[ "$VI_MODE_SET_CURSOR" = true ]] || return
  19. # https://vt100.net/docs/vt510-rm/DECSCUSR
  20. local _shape=0
  21. case "${1:-${VI_KEYMAP:-main}}" in
  22. main) _shape=6 ;; # vi insert: line
  23. viins) _shape=6 ;; # vi insert: line
  24. isearch) _shape=6 ;; # inc search: line
  25. command) _shape=6 ;; # read a command name
  26. vicmd) _shape=2 ;; # vi cmd: block
  27. visual) _shape=2 ;; # vi visual mode: block
  28. viopp) _shape=0 ;; # vi operation pending: blinking block
  29. *) _shape=0 ;;
  30. esac
  31. printf $'\e[%d q' "${_shape}"
  32. }
  33. # Updates editor information when the keymap changes.
  34. function zle-keymap-select() {
  35. # update keymap variable for the prompt
  36. typeset -g VI_KEYMAP=$KEYMAP
  37. if [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]]; then
  38. zle reset-prompt
  39. zle -R
  40. fi
  41. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  42. }
  43. zle -N zle-keymap-select
  44. # These "echoti" statements were originally set in lib/key-bindings.zsh
  45. # Not sure the best way to extend without overriding.
  46. function zle-line-init() {
  47. local prev_vi_keymap
  48. prev_vi_keymap="${VI_KEYMAP:-}"
  49. typeset -g VI_KEYMAP=main
  50. [[ "$prev_vi_keymap" != 'main' ]] && [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]] && zle reset-prompt
  51. (( ! ${+terminfo[smkx]} )) || echoti smkx
  52. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  53. }
  54. zle -N zle-line-init
  55. function zle-line-finish() {
  56. typeset -g VI_KEYMAP=main
  57. (( ! ${+terminfo[rmkx]} )) || echoti rmkx
  58. _vi-mode-set-cursor-shape-for-keymap default
  59. }
  60. zle -N zle-line-finish
  61. bindkey -v
  62. # allow vv to edit the command line (standard behaviour)
  63. autoload -Uz edit-command-line
  64. zle -N edit-command-line
  65. bindkey -M vicmd 'vv' edit-command-line
  66. # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
  67. bindkey '^P' up-history
  68. bindkey '^N' down-history
  69. # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
  70. bindkey '^?' backward-delete-char
  71. bindkey '^h' backward-delete-char
  72. bindkey '^w' backward-kill-word
  73. # allow ctrl-r and ctrl-s to search the history
  74. bindkey '^r' history-incremental-search-backward
  75. bindkey '^s' history-incremental-search-forward
  76. # allow ctrl-a and ctrl-e to move to beginning/end of line
  77. bindkey '^a' beginning-of-line
  78. bindkey '^e' end-of-line
  79. function wrap_clipboard_widgets() {
  80. # NB: Assume we are the first wrapper and that we only wrap native widgets
  81. # See zsh-autosuggestions.zsh for a more generic and more robust wrapper
  82. local verb="$1"
  83. shift
  84. local widget
  85. local wrapped_name
  86. for widget in "$@"; do
  87. wrapped_name="_zsh-vi-${verb}-${widget}"
  88. if [ "${verb}" = copy ]; then
  89. eval "
  90. function ${wrapped_name}() {
  91. zle .${widget}
  92. printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true
  93. }
  94. "
  95. else
  96. eval "
  97. function ${wrapped_name}() {
  98. CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\"
  99. zle .${widget}
  100. }
  101. "
  102. fi
  103. zle -N "${widget}" "${wrapped_name}"
  104. done
  105. }
  106. wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete
  107. wrap_clipboard_widgets paste vi-put-{before,after}
  108. unfunction wrap_clipboard_widgets
  109. # if mode indicator wasn't setup by theme, define default, we'll leave INSERT_MODE_INDICATOR empty by default
  110. if [[ -z "$MODE_INDICATOR" ]]; then
  111. MODE_INDICATOR='%B%F{red}<%b<<%f'
  112. fi
  113. function vi_mode_prompt_info() {
  114. # If we're using the prompt to display mode info, and we haven't explicitly
  115. # disabled "reset prompt on mode change", then set it here.
  116. #
  117. # We do that here instead of the `if` statement below because the user may
  118. # set RPS1/RPROMPT to something else in their custom config.
  119. : "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:=true}"
  120. echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/$INSERT_MODE_INDICATOR}"
  121. }
  122. # define right prompt, if it wasn't defined by a theme
  123. if [[ -z "$RPS1" && -z "$RPROMPT" ]]; then
  124. RPS1='$(vi_mode_prompt_info)'
  125. fi