From 98762faa0762e1f0ede05149c1f6b3a9db93e074 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Sat, 2 Jan 2021 13:41:57 -0800 Subject: fix(vi-mode): control cursor, restore and use visual mode and speed up mode changes (#8004) --- plugins/vi-mode/vi-mode.plugin.zsh | 149 ++++++++++++++++++++++++++++++++----- 1 file changed, 130 insertions(+), 19 deletions(-) (limited to 'plugins/vi-mode/vi-mode.plugin.zsh') diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index c91ba05ba..7ff826ea3 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -1,33 +1,61 @@ +# Control whether to force a redraw on each mode change. +# +# Resetting the prompt on every mode change can cause lag when switching modes. +# This is especially true if the prompt does things like checking git status. +# +# Set to "true" to force the prompt to reset on each mode change. +# Set to "false" to force the prompt *not* to reset on each mode change. +# +# (The default is not to reset, unless we're showing the mode in RPS1). +typeset -g VI_MODE_RESET_PROMPT_ON_MODE_CHANGE +typeset -g VI_KEYMAP=main + +function _vi-mode-set-cursor-shape-for-keymap() { + # https://vt100.net/docs/vt510-rm/DECSCUSR + local _shape=0 + case "${1:-${VI_KEYMAP:-main}}" in + main) _shape=6 ;; # vi insert: line + viins) _shape=6 ;; # vi insert: line + isearch) _shape=6 ;; # inc search: line + command) _shape=6 ;; # read a command name + vicmd) _shape=2 ;; # vi cmd: block + visual) _shape=2 ;; # vi visual mode: block + viopp) _shape=0 ;; # vi operation pending: blinking block + *) _shape=0 ;; + esac + printf $'\e[%d q' "${_shape}" +} + # Updates editor information when the keymap changes. function zle-keymap-select() { # update keymap variable for the prompt - VI_KEYMAP=$KEYMAP + typeset -g VI_KEYMAP=$KEYMAP - zle reset-prompt - zle -R + if [ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]; then + zle reset-prompt + zle -R + fi + _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}" } - zle -N zle-keymap-select -function vi-accept-line() { - VI_KEYMAP=main - zle accept-line +# These "echoti" statements were originally set in lib/key-bindings.zsh +# Not sure the best way to extend without overriding. +function zle-line-init() { + typeset -g VI_KEYMAP=main + (( ! ${+terminfo[smkx]} )) || echoti smkx + _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}" } +zle -N zle-line-init -zle -N vi-accept-line - +function zle-line-finish() { + (( ! ${+terminfo[rmkx]} )) || echoti rmkx + _vi-mode-set-cursor-shape-for-keymap default +} +zle -N zle-line-finish bindkey -v -# use custom accept-line widget to update $VI_KEYMAP -bindkey -M vicmd '^J' vi-accept-line -bindkey -M vicmd '^M' vi-accept-line - -# allow v to edit the command line (standard behaviour) -autoload -Uz edit-command-line -zle -N edit-command-line -bindkey -M vicmd 'v' edit-command-line - # allow ctrl-p, ctrl-n for navigate history (standard behaviour) bindkey '^P' up-history bindkey '^N' down-history @@ -45,12 +73,95 @@ bindkey '^s' history-incremental-search-forward bindkey '^a' beginning-of-line bindkey '^e' end-of-line +if [[ "${terminfo[kpp]}" != "" ]]; then + bindkey "${terminfo[kpp]}" up-line-or-history # [PageUp] - Up a line of history +fi +if [[ "${terminfo[knp]}" != "" ]]; then + bindkey "${terminfo[knp]}" down-line-or-history # [PageDown] - Down a line of history +fi + +# start typing + [Up-Arrow] - fuzzy find history forward +if [[ "${terminfo[kcuu1]}" != "" ]]; then + autoload -U up-line-or-beginning-search + zle -N up-line-or-beginning-search + bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search +fi +# start typing + [Down-Arrow] - fuzzy find history backward +if [[ "${terminfo[kcud1]}" != "" ]]; then + autoload -U down-line-or-beginning-search + zle -N down-line-or-beginning-search + bindkey "${terminfo[kcud1]}" down-line-or-beginning-search +fi + +if [[ "${terminfo[khome]}" != "" ]]; then + bindkey "${terminfo[khome]}" beginning-of-line # [Home] - Go to beginning of line +fi +if [[ "${terminfo[kend]}" != "" ]]; then + bindkey "${terminfo[kend]}" end-of-line # [End] - Go to end of line +fi + +if [[ "${terminfo[kcbt]}" != "" ]]; then + bindkey "${terminfo[kcbt]}" reverse-menu-complete # [Shift-Tab] - move through the completion menu backwards +fi + +bindkey '^?' backward-delete-char # [Backspace] - delete backward +if [[ "${terminfo[kdch1]}" != "" ]]; then + bindkey "${terminfo[kdch1]}" delete-char # [Delete] - delete forward +else + bindkey "^[[3~" delete-char + bindkey "^[3;5~" delete-char + bindkey "\e[3~" delete-char +fi + +() { + local wrap_clipboard_widgets + function wrap_clipboard_widgets() { + # NB: Assume we are the first wrapper and that we only wrap native widgets + # See zsh-autosuggestions.zsh for a more generic and more robust wrapper + local verb="$1" + shift + + local widget + local wrapped_name + for widget in "$@"; do + wrapped_name="_zsh-vi-${verb}-${widget}" + if [ "${verb}" = copy ]; then + eval " + function ${wrapped_name}() { + zle .${widget} + printf %s \"\${CUTBUFFER}\" | clipcopy + } + " + else + eval " + function ${wrapped_name}() { + CUTBUFFER=\"\$(clippaste)\" + zle .${widget} + } + " + fi + zle -N "${widget}" "${wrapped_name}" + done + } + + wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete + wrap_clipboard_widgets paste vi-put-{before,after} + unfunction wrap_clipboard_widgets +} + # if mode indicator wasn't setup by theme, define default if [[ "$MODE_INDICATOR" == "" ]]; then - MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}" + MODE_INDICATOR='%B%F{red}<%b<<%f' fi function vi_mode_prompt_info() { + # If we're using the prompt to display mode info, and we haven't explicitly + # disabled "reset prompt on mode change", then set it here. + # + # We do that here instead of the `if` statement below because the user may + # set RPS1/RPROMPT to something else in their custom config. + : "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:=true}" + echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}" } -- cgit v1.2.3-70-g09d2 From 0e833b622ba43d38bd62227244d831f3c0e4a325 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 2 Jan 2021 23:59:55 +0100 Subject: refactor(vi-mode): remove duplicate bindkey logic and fix syntax --- plugins/vi-mode/vi-mode.plugin.zsh | 111 ++++++++++++------------------------- 1 file changed, 34 insertions(+), 77 deletions(-) (limited to 'plugins/vi-mode/vi-mode.plugin.zsh') diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index 7ff826ea3..f83ea7696 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -31,7 +31,7 @@ function zle-keymap-select() { # update keymap variable for the prompt typeset -g VI_KEYMAP=$KEYMAP - if [ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]; then + if [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]]; then zle reset-prompt zle -R fi @@ -73,84 +73,41 @@ bindkey '^s' history-incremental-search-forward bindkey '^a' beginning-of-line bindkey '^e' end-of-line -if [[ "${terminfo[kpp]}" != "" ]]; then - bindkey "${terminfo[kpp]}" up-line-or-history # [PageUp] - Up a line of history -fi -if [[ "${terminfo[knp]}" != "" ]]; then - bindkey "${terminfo[knp]}" down-line-or-history # [PageDown] - Down a line of history -fi - -# start typing + [Up-Arrow] - fuzzy find history forward -if [[ "${terminfo[kcuu1]}" != "" ]]; then - autoload -U up-line-or-beginning-search - zle -N up-line-or-beginning-search - bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search -fi -# start typing + [Down-Arrow] - fuzzy find history backward -if [[ "${terminfo[kcud1]}" != "" ]]; then - autoload -U down-line-or-beginning-search - zle -N down-line-or-beginning-search - bindkey "${terminfo[kcud1]}" down-line-or-beginning-search -fi - -if [[ "${terminfo[khome]}" != "" ]]; then - bindkey "${terminfo[khome]}" beginning-of-line # [Home] - Go to beginning of line -fi -if [[ "${terminfo[kend]}" != "" ]]; then - bindkey "${terminfo[kend]}" end-of-line # [End] - Go to end of line -fi - -if [[ "${terminfo[kcbt]}" != "" ]]; then - bindkey "${terminfo[kcbt]}" reverse-menu-complete # [Shift-Tab] - move through the completion menu backwards -fi - -bindkey '^?' backward-delete-char # [Backspace] - delete backward -if [[ "${terminfo[kdch1]}" != "" ]]; then - bindkey "${terminfo[kdch1]}" delete-char # [Delete] - delete forward -else - bindkey "^[[3~" delete-char - bindkey "^[3;5~" delete-char - bindkey "\e[3~" delete-char -fi - -() { - local wrap_clipboard_widgets - function wrap_clipboard_widgets() { - # NB: Assume we are the first wrapper and that we only wrap native widgets - # See zsh-autosuggestions.zsh for a more generic and more robust wrapper - local verb="$1" - shift - - local widget - local wrapped_name - for widget in "$@"; do - wrapped_name="_zsh-vi-${verb}-${widget}" - if [ "${verb}" = copy ]; then - eval " - function ${wrapped_name}() { - zle .${widget} - printf %s \"\${CUTBUFFER}\" | clipcopy - } - " - else - eval " - function ${wrapped_name}() { - CUTBUFFER=\"\$(clippaste)\" - zle .${widget} - } - " - fi - zle -N "${widget}" "${wrapped_name}" - done - } - - wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete - wrap_clipboard_widgets paste vi-put-{before,after} - unfunction wrap_clipboard_widgets +function wrap_clipboard_widgets() { + # NB: Assume we are the first wrapper and that we only wrap native widgets + # See zsh-autosuggestions.zsh for a more generic and more robust wrapper + local verb="$1" + shift + + local widget + local wrapped_name + for widget in "$@"; do + wrapped_name="_zsh-vi-${verb}-${widget}" + if [ "${verb}" = copy ]; then + eval " + function ${wrapped_name}() { + zle .${widget} + printf %s \"\${CUTBUFFER}\" | clipcopy + } + " + else + eval " + function ${wrapped_name}() { + CUTBUFFER=\"\$(clippaste)\" + zle .${widget} + } + " + fi + zle -N "${widget}" "${wrapped_name}" + done } +wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete +wrap_clipboard_widgets paste vi-put-{before,after} +unfunction wrap_clipboard_widgets + # if mode indicator wasn't setup by theme, define default -if [[ "$MODE_INDICATOR" == "" ]]; then +if [[ -z "$MODE_INDICATOR" ]]; then MODE_INDICATOR='%B%F{red}<%b<<%f' fi @@ -166,6 +123,6 @@ function vi_mode_prompt_info() { } # define right prompt, if it wasn't defined by a theme -if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then +if [[ -z "$RPS1" && -z "$RPROMPT" ]]; then RPS1='$(vi_mode_prompt_info)' fi -- cgit v1.2.3-70-g09d2 From 79980b00fb146437231c2a110ea49032b842c92c Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 4 Jan 2021 16:41:59 +0100 Subject: fix(vi-mode): hide cursor-change logic behind `VI_MODE_SET_CURSOR` setting Fixes #9570 --- plugins/vi-mode/README.md | 7 +++++++ plugins/vi-mode/vi-mode.plugin.zsh | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'plugins/vi-mode/vi-mode.plugin.zsh') diff --git a/plugins/vi-mode/README.md b/plugins/vi-mode/README.md index fdf3ede73..41faf2de0 100644 --- a/plugins/vi-mode/README.md +++ b/plugins/vi-mode/README.md @@ -22,6 +22,13 @@ plugins=(... vi-mode) The default value is unset, unless `vi_mode_prompt_info` is used, in which case it'll automatically be set to `true`. +- `VI_MODE_SET_CURSOR`: controls whether the cursor style is changed when switching + to a different input mode. Set it to `true` to enable it (default: unset): + + ```zsh + VI_MODE_SET_CURSOR=true + ``` + - `MODE_INDICATOR`: controls the string displayed when the shell is in normal mode. See [Mode indicator](#mode-indicator) for details. diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index f83ea7696..e5b252d83 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -4,13 +4,21 @@ # This is especially true if the prompt does things like checking git status. # # Set to "true" to force the prompt to reset on each mode change. -# Set to "false" to force the prompt *not* to reset on each mode change. +# Unset or set to any other value to do the opposite. # -# (The default is not to reset, unless we're showing the mode in RPS1). +# The default is not to reset, unless we're showing the mode in RPS1. typeset -g VI_MODE_RESET_PROMPT_ON_MODE_CHANGE +# Control whether to change the cursor style on mode change. +# +# Set to "true" to change the cursor on each mode change. +# Unset or set to any other value to do the opposite. +typeset -g VI_MODE_SET_CURSOR + typeset -g VI_KEYMAP=main function _vi-mode-set-cursor-shape-for-keymap() { + [[ "$VI_MODE_SET_CURSOR" = true ]] || return + # https://vt100.net/docs/vt510-rm/DECSCUSR local _shape=0 case "${1:-${VI_KEYMAP:-main}}" in -- cgit v1.2.3-70-g09d2 From 2118d35e017eb8c599f3c25863c8263aca307541 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 4 Jan 2021 21:41:11 +0100 Subject: fix(vi-mode)!: add back edit-command-line key binding as 'vv' (#9573) BREAKING CHANGE: the key binding to open an editor to edit the command line has been moved from being `v` (press v once) to being `vv` (press v twice). Now, the action for `v` is the default `visual-mode`, as is in Vim. Fixes #9573 --- plugins/vi-mode/README.md | 14 ++++++++------ plugins/vi-mode/vi-mode.plugin.zsh | 5 +++++ 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'plugins/vi-mode/vi-mode.plugin.zsh') diff --git a/plugins/vi-mode/README.md b/plugins/vi-mode/README.md index 41faf2de0..b59d5f279 100644 --- a/plugins/vi-mode/README.md +++ b/plugins/vi-mode/README.md @@ -60,6 +60,12 @@ NOTE: some of these key bindings are set by zsh by default when using a vi-mode - `/` : Search backward in history - `n` : Repeat the last `/` +### Vim edition + +- `vv` : Edit current command line in Vim + +NOTE: this used to be bound to `v`. That is now the default (`visual-mode`) + ### Movement - `$` : To the end of the line @@ -99,9 +105,5 @@ NOTE: some of these key bindings are set by zsh by default when using a vi-mode - `C` : Delete to the end of the line and start insert - `r{char}` : Replace the character under the cursor with {char} - `R` : Enter replace mode: Each character replaces existing one -- `x` : Delete [count] characters under and after the cursor -- `X` : Delete [count] characters before the cursor - -### Removed key bindings - -- `v` : Edit current command line in Vim +- `x` : Delete `count` characters under and after the cursor +- `X` : Delete `count` characters before the cursor diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index e5b252d83..0b11a6a7f 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -64,6 +64,11 @@ zle -N zle-line-finish bindkey -v +# allow vv to edit the command line (standard behaviour) +autoload -Uz edit-command-line +zle -N edit-command-line +bindkey -M vicmd 'vv' edit-command-line + # allow ctrl-p, ctrl-n for navigate history (standard behaviour) bindkey '^P' up-history bindkey '^N' down-history -- cgit v1.2.3-70-g09d2 From efcbd9f3480a28ec69c607c46adcbfd8d230ac9f Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 16 Jan 2021 22:55:17 +0100 Subject: fix(vi-mode): ignore `clip*` function errors in yank and put widgets Fixes #9605 --- plugins/vi-mode/vi-mode.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/vi-mode/vi-mode.plugin.zsh') diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh index 0b11a6a7f..7f14961ad 100644 --- a/plugins/vi-mode/vi-mode.plugin.zsh +++ b/plugins/vi-mode/vi-mode.plugin.zsh @@ -100,13 +100,13 @@ function wrap_clipboard_widgets() { eval " function ${wrapped_name}() { zle .${widget} - printf %s \"\${CUTBUFFER}\" | clipcopy + printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true } " else eval " function ${wrapped_name}() { - CUTBUFFER=\"\$(clippaste)\" + CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\" zle .${widget} } " -- cgit v1.2.3-70-g09d2