1 # Control whether to force a redraw on each mode change.
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.
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.
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.
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
17 typeset -g VI_KEYMAP=main
19 function _vi-mode-set-cursor-shape-for-keymap() {
20 [[ "$VI_MODE_SET_CURSOR" = true ]] || return
22 # https://vt100.net/docs/vt510-rm/DECSCUSR
24 case "${1:-${VI_KEYMAP:-main}}" in
25 main) _shape=6 ;; # vi insert: line
26 viins) _shape=6 ;; # vi insert: line
27 isearch) _shape=6 ;; # inc search: line
28 command) _shape=6 ;; # read a command name
29 vicmd) _shape=2 ;; # vi cmd: block
30 visual) _shape=2 ;; # vi visual mode: block
31 viopp) _shape=0 ;; # vi operation pending: blinking block
34 printf $'\e[%d q' "${_shape}"
37 # Updates editor information when the keymap changes.
38 function zle-keymap-select() {
39 # update keymap variable for the prompt
40 typeset -g VI_KEYMAP=$KEYMAP
42 if [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]]; then
46 _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
48 zle -N zle-keymap-select
50 # These "echoti" statements were originally set in lib/key-bindings.zsh
51 # Not sure the best way to extend without overriding.
52 function zle-line-init() {
54 prev_vi_keymap="${VI_KEYMAP:-}"
55 typeset -g VI_KEYMAP=main
56 [[ "$prev_vi_keymap" != 'main' ]] && [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]] && zle reset-prompt
57 (( ! ${+terminfo[smkx]} )) || echoti smkx
58 _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
62 function zle-line-finish() {
63 typeset -g VI_KEYMAP=main
64 (( ! ${+terminfo[rmkx]} )) || echoti rmkx
65 _vi-mode-set-cursor-shape-for-keymap default
67 zle -N zle-line-finish
71 # allow vv to edit the command line (standard behaviour)
72 autoload -Uz edit-command-line
73 zle -N edit-command-line
74 bindkey -M vicmd 'vv' edit-command-line
76 # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
77 bindkey '^P' up-history
78 bindkey '^N' down-history
80 # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
81 bindkey '^?' backward-delete-char
82 bindkey '^h' backward-delete-char
83 bindkey '^w' backward-kill-word
85 # allow ctrl-r and ctrl-s to search the history
86 bindkey '^r' history-incremental-search-backward
87 bindkey '^s' history-incremental-search-forward
89 # allow ctrl-a and ctrl-e to move to beginning/end of line
90 bindkey '^a' beginning-of-line
91 bindkey '^e' end-of-line
93 function wrap_clipboard_widgets() {
94 # NB: Assume we are the first wrapper and that we only wrap native widgets
95 # See zsh-autosuggestions.zsh for a more generic and more robust wrapper
101 for widget in "$@"; do
102 wrapped_name="_zsh-vi-${verb}-${widget}"
103 if [ "${verb}" = copy ]; then
105 function ${wrapped_name}() {
107 printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true
112 function ${wrapped_name}() {
113 CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\"
118 zle -N "${widget}" "${wrapped_name}"
122 wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete vi-delete-char
123 wrap_clipboard_widgets paste vi-put-{before,after}
124 unfunction wrap_clipboard_widgets
126 # if mode indicator wasn't setup by theme, define default, we'll leave INSERT_MODE_INDICATOR empty by default
127 if [[ -z "$MODE_INDICATOR" ]]; then
128 MODE_INDICATOR='%B%F{red}<%b<<%f'
131 function vi_mode_prompt_info() {
132 # If we're using the prompt to display mode info, and we haven't explicitly
133 # disabled "reset prompt on mode change", then set it here.
135 # We do that here instead of the `if` statement below because the user may
136 # set RPS1/RPROMPT to something else in their custom config.
137 : "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:=true}"
139 echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/$INSERT_MODE_INDICATOR}"
142 # define right prompt, if it wasn't defined by a theme
143 if [[ -z "$RPS1" && -z "$RPROMPT" ]]; then
144 RPS1='$(vi_mode_prompt_info)'