A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/softmoth/zsh-vim-mode/issues/33 below:

Workaround to use surrounds while keeping KEYTIMEOUT=1 · Issue #33 · softmoth/zsh-vim-mode · GitHub

This isn't actually a issue, it's more like an informative notice. All workarounds presented in the README.md has one or other drawback, but the one showed bellow adresses this issue flawless (or I think so).

The strategy is to remap the c, d and y keys to user defined widgets, thus removing its original bindings vi-delete, vi-change and vi-yank.

These widgets will then read the next character pressed and if its an s, it will call the surround widget, otherwise it will call the vi widget with the proper numeric-argument. One special attention must be given to repeated key commands, like cc, dd and yy, so it won't call our widget recursively.

Here's the code:

function change-hack() {
  read -k 1 option

  if [[ $option == 's' ]]; then
    zle -U Tcs
  elif [[ $option == 'c' ]]; then
    zle vi-change-whole-line
  else
    zle -U ${NUMERIC}Tvc$option
  fi
}

function delete-hack() {
  read -k 1 option

  if [[ $option == 's' ]]; then
    zle -U Tds
  elif [[ $option == 'd' ]]; then
    zle kill-whole-line
  else
    zle -U ${NUMERIC}Tvd$option
  fi
}

function yank-hack() {
  read -k 1 option

  if [[ $option == 's' ]]; then
    zle -U Tys
  elif [[ $option == 'y' ]]; then
    zle vi-yank-whole-line
  else
    zle -U ${NUMERIC}Tvy$option
  fi
}

zle -N change-hack
zle -N delete-hack
zle -N yank-hack
autoload -Uz surround
zle -N delete-surround surround
zle -N change-surround surround
zle -N add-surround surround
bindkey -M vicmd 'Tcs' change-surround
bindkey -M vicmd 'Tds' delete-surround
bindkey -M vicmd 'Tys' add-surround
bindkey -M vicmd 'Tvd' vi-delete
bindkey -M vicmd 'Tvc' vi-change
bindkey -M vicmd 'Tvy' vi-yank
bindkey -M vicmd 'c' change-hack
bindkey -M vicmd 'd' delete-hack
bindkey -M vicmd 'y' yank-hack
bindkey -M visual S add-surround

This workaround has two advantages over the others:

  1. The value of KEYTIMEOUT doesn't interfere on its working;
  2. You can press the first key and wait as much as desired to press the second key.

The only drawback is the ugly hack it is itself.

Note, however, that I came up with this yesterday night, so it wasn't tested throughout. Pls, let me know if something doesn't work.

lkhphuc, korniychuk, maximgurin and edxds


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4