vi-mode.plugin.zsh 5.2 KB

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