vi-mode.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Set to "false" to force the prompt *not* to reset on each mode change.
  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. typeset -g VI_KEYMAP=main
  12. function _vi-mode-set-cursor-shape-for-keymap() {
  13. # https://vt100.net/docs/vt510-rm/DECSCUSR
  14. local _shape=0
  15. case "${1:-${VI_KEYMAP:-main}}" in
  16. main) _shape=6 ;; # vi insert: line
  17. viins) _shape=6 ;; # vi insert: line
  18. isearch) _shape=6 ;; # inc search: line
  19. command) _shape=6 ;; # read a command name
  20. vicmd) _shape=2 ;; # vi cmd: block
  21. visual) _shape=2 ;; # vi visual mode: block
  22. viopp) _shape=0 ;; # vi operation pending: blinking block
  23. *) _shape=0 ;;
  24. esac
  25. printf $'\e[%d q' "${_shape}"
  26. }
  27. # Updates editor information when the keymap changes.
  28. function zle-keymap-select() {
  29. # update keymap variable for the prompt
  30. typeset -g VI_KEYMAP=$KEYMAP
  31. if [ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]; then
  32. zle reset-prompt
  33. zle -R
  34. fi
  35. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  36. }
  37. zle -N zle-keymap-select
  38. # These "echoti" statements were originally set in lib/key-bindings.zsh
  39. # Not sure the best way to extend without overriding.
  40. function zle-line-init() {
  41. typeset -g VI_KEYMAP=main
  42. (( ! ${+terminfo[smkx]} )) || echoti smkx
  43. _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
  44. }
  45. zle -N zle-line-init
  46. function zle-line-finish() {
  47. (( ! ${+terminfo[rmkx]} )) || echoti rmkx
  48. _vi-mode-set-cursor-shape-for-keymap default
  49. }
  50. zle -N zle-line-finish
  51. bindkey -v
  52. # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
  53. bindkey '^P' up-history
  54. bindkey '^N' down-history
  55. # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
  56. bindkey '^?' backward-delete-char
  57. bindkey '^h' backward-delete-char
  58. bindkey '^w' backward-kill-word
  59. # allow ctrl-r and ctrl-s to search the history
  60. bindkey '^r' history-incremental-search-backward
  61. bindkey '^s' history-incremental-search-forward
  62. # allow ctrl-a and ctrl-e to move to beginning/end of line
  63. bindkey '^a' beginning-of-line
  64. bindkey '^e' end-of-line
  65. if [[ "${terminfo[kpp]}" != "" ]]; then
  66. bindkey "${terminfo[kpp]}" up-line-or-history # [PageUp] - Up a line of history
  67. fi
  68. if [[ "${terminfo[knp]}" != "" ]]; then
  69. bindkey "${terminfo[knp]}" down-line-or-history # [PageDown] - Down a line of history
  70. fi
  71. # start typing + [Up-Arrow] - fuzzy find history forward
  72. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  73. autoload -U up-line-or-beginning-search
  74. zle -N up-line-or-beginning-search
  75. bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
  76. fi
  77. # start typing + [Down-Arrow] - fuzzy find history backward
  78. if [[ "${terminfo[kcud1]}" != "" ]]; then
  79. autoload -U down-line-or-beginning-search
  80. zle -N down-line-or-beginning-search
  81. bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
  82. fi
  83. if [[ "${terminfo[khome]}" != "" ]]; then
  84. bindkey "${terminfo[khome]}" beginning-of-line # [Home] - Go to beginning of line
  85. fi
  86. if [[ "${terminfo[kend]}" != "" ]]; then
  87. bindkey "${terminfo[kend]}" end-of-line # [End] - Go to end of line
  88. fi
  89. if [[ "${terminfo[kcbt]}" != "" ]]; then
  90. bindkey "${terminfo[kcbt]}" reverse-menu-complete # [Shift-Tab] - move through the completion menu backwards
  91. fi
  92. bindkey '^?' backward-delete-char # [Backspace] - delete backward
  93. if [[ "${terminfo[kdch1]}" != "" ]]; then
  94. bindkey "${terminfo[kdch1]}" delete-char # [Delete] - delete forward
  95. else
  96. bindkey "^[[3~" delete-char
  97. bindkey "^[3;5~" delete-char
  98. bindkey "\e[3~" delete-char
  99. fi
  100. () {
  101. local wrap_clipboard_widgets
  102. function wrap_clipboard_widgets() {
  103. # NB: Assume we are the first wrapper and that we only wrap native widgets
  104. # See zsh-autosuggestions.zsh for a more generic and more robust wrapper
  105. local verb="$1"
  106. shift
  107. local widget
  108. local wrapped_name
  109. for widget in "$@"; do
  110. wrapped_name="_zsh-vi-${verb}-${widget}"
  111. if [ "${verb}" = copy ]; then
  112. eval "
  113. function ${wrapped_name}() {
  114. zle .${widget}
  115. printf %s \"\${CUTBUFFER}\" | clipcopy
  116. }
  117. "
  118. else
  119. eval "
  120. function ${wrapped_name}() {
  121. CUTBUFFER=\"\$(clippaste)\"
  122. zle .${widget}
  123. }
  124. "
  125. fi
  126. zle -N "${widget}" "${wrapped_name}"
  127. done
  128. }
  129. wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete
  130. wrap_clipboard_widgets paste vi-put-{before,after}
  131. unfunction wrap_clipboard_widgets
  132. }
  133. # if mode indicator wasn't setup by theme, define default
  134. if [[ "$MODE_INDICATOR" == "" ]]; then
  135. MODE_INDICATOR='%B%F{red}<%b<<%f'
  136. fi
  137. function vi_mode_prompt_info() {
  138. # If we're using the prompt to display mode info, and we haven't explicitly
  139. # disabled "reset prompt on mode change", then set it here.
  140. #
  141. # We do that here instead of the `if` statement below because the user may
  142. # set RPS1/RPROMPT to something else in their custom config.
  143. : "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:=true}"
  144. echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
  145. }
  146. # define right prompt, if it wasn't defined by a theme
  147. if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then
  148. RPS1='$(vi_mode_prompt_info)'
  149. fi