vi-mode.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # Control how the cursor appears in the various vim modes. This only applies
  17. # if $VI_MODE_SET_CURSOR=true.
  18. #
  19. # See https://vt100.net/docs/vt510-rm/DECSCUSR for cursor styles
  20. typeset -g VI_MODE_CURSOR_NORMAL=2
  21. typeset -g VI_MODE_CURSOR_VISUAL=6
  22. typeset -g VI_MODE_CURSOR_INSERT=6
  23. typeset -g VI_MODE_CURSOR_OPPEND=0
  24. typeset -g VI_KEYMAP=main
  25. function _vi-mode-set-cursor-shape-for-keymap() {
  26. [[ "$VI_MODE_SET_CURSOR" = true ]] || return
  27. # https://vt100.net/docs/vt510-rm/DECSCUSR
  28. local _shape=0
  29. case "${1:-${VI_KEYMAP:-main}}" in
  30. main) _shape=$VI_MODE_CURSOR_INSERT ;; # vi insert: line
  31. viins) _shape=$VI_MODE_CURSOR_INSERT ;; # vi insert: line
  32. isearch) _shape=$VI_MODE_CURSOR_INSERT ;; # inc search: line
  33. command) _shape=$VI_MODE_CURSOR_INSERT ;; # read a command name
  34. vicmd) _shape=$VI_MODE_CURSOR_NORMAL ;; # vi cmd: block
  35. visual) _shape=$VI_MODE_CURSOR_VISUAL ;; # vi visual mode: block
  36. viopp) _shape=$VI_MODE_CURSOR_OPPEND ;; # vi operation pending: blinking block
  37. *) _shape=0 ;;
  38. esac
  39. printf $'\e[%d q' "${_shape}"
  40. }
  41. function _visual-mode {
  42. typeset -g VI_KEYMAP=visual
  43. _vi-mode-set-cursor-shape-for-keymap "$VI_KEYMAP"
  44. zle .visual-mode
  45. }
  46. zle -N visual-mode _visual-mode
  47. function _vi-mode-should-reset-prompt() {
  48. # If $VI_MODE_RESET_PROMPT_ON_MODE_CHANGE is unset (default), dynamically
  49. # check whether we're using the prompt to display vi-mode info
  50. if [[ -z "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" ]]; then
  51. [[ "${PS1} ${RPS1}" = *'$(vi_mode_prompt_info)'* ]]
  52. return $?
  53. fi
  54. # If $VI_MODE_RESET_PROMPT_ON_MODE_CHANGE was manually set, let's check
  55. # if it was specifically set to true or it was disabled with any other value
  56. [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE}" = true ]]
  57. }
  58. # Updates editor information when the keymap changes.
  59. function zle-keymap-select() {
  60. # update keymap variable for the prompt
  61. typeset -g VI_KEYMAP=$KEYMAP
  62. if _vi-mode-should-reset-prompt; then
  63. zle reset-prompt
  64. zle -R
  65. fi
  66. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  67. }
  68. zle -N zle-keymap-select
  69. # These "echoti" statements were originally set in lib/key-bindings.zsh
  70. # Not sure the best way to extend without overriding.
  71. function zle-line-init() {
  72. local prev_vi_keymap="${VI_KEYMAP:-}"
  73. typeset -g VI_KEYMAP=main
  74. [[ "$prev_vi_keymap" != 'main' ]] && _vi-mode-should-reset-prompt && zle reset-prompt
  75. (( ! ${+terminfo[smkx]} )) || echoti smkx
  76. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  77. }
  78. zle -N zle-line-init
  79. function zle-line-finish() {
  80. typeset -g VI_KEYMAP=main
  81. (( ! ${+terminfo[rmkx]} )) || echoti rmkx
  82. _vi-mode-set-cursor-shape-for-keymap default
  83. }
  84. zle -N zle-line-finish
  85. bindkey -v
  86. # allow vv to edit the command line (standard behaviour)
  87. autoload -Uz edit-command-line
  88. zle -N edit-command-line
  89. bindkey -M vicmd 'vv' edit-command-line
  90. # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
  91. bindkey '^P' up-history
  92. bindkey '^N' down-history
  93. # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
  94. bindkey '^?' backward-delete-char
  95. bindkey '^h' backward-delete-char
  96. bindkey '^w' backward-kill-word
  97. # allow ctrl-r and ctrl-s to search the history
  98. bindkey '^r' history-incremental-search-backward
  99. bindkey '^s' history-incremental-search-forward
  100. # allow ctrl-a and ctrl-e to move to beginning/end of line
  101. bindkey '^a' beginning-of-line
  102. bindkey '^e' end-of-line
  103. function wrap_clipboard_widgets() {
  104. # NB: Assume we are the first wrapper and that we only wrap native widgets
  105. # See zsh-autosuggestions.zsh for a more generic and more robust wrapper
  106. local verb="$1"
  107. shift
  108. local widget
  109. local wrapped_name
  110. for widget in "$@"; do
  111. wrapped_name="_zsh-vi-${verb}-${widget}"
  112. if [ "${verb}" = copy ]; then
  113. eval "
  114. function ${wrapped_name}() {
  115. zle .${widget}
  116. printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true
  117. }
  118. "
  119. else
  120. eval "
  121. function ${wrapped_name}() {
  122. CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\"
  123. zle .${widget}
  124. }
  125. "
  126. fi
  127. zle -N "${widget}" "${wrapped_name}"
  128. done
  129. }
  130. wrap_clipboard_widgets copy \
  131. vi-yank vi-yank-eol vi-yank-whole-line \
  132. vi-change vi-change-eol vi-change-whole-line \
  133. vi-kill-line vi-kill-eol vi-backward-kill-word \
  134. vi-delete vi-delete-char vi-backward-delete-char
  135. wrap_clipboard_widgets paste \
  136. vi-put-{before,after}
  137. unfunction wrap_clipboard_widgets
  138. # if mode indicator wasn't setup by theme, define default, we'll leave INSERT_MODE_INDICATOR empty by default
  139. if [[ -z "$MODE_INDICATOR" ]]; then
  140. MODE_INDICATOR='%B%F{red}<%b<<%f'
  141. fi
  142. function vi_mode_prompt_info() {
  143. echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/$INSERT_MODE_INDICATOR}"
  144. }
  145. # define right prompt, if it wasn't defined by a theme
  146. if [[ -z "$RPS1" && -z "$RPROMPT" ]]; then
  147. RPS1='$(vi_mode_prompt_info)'
  148. fi