vi-mode.plugin.zsh 5.1 KB

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