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