Skip to content

Workaround to use surrounds while keeping KEYTIMEOUT=1 #33

Closed
@FreddieOliveira

Description

@FreddieOliveira

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions