Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to cancel certain shortcut keys? #680

Open
1trapbox opened this issue Jan 15, 2024 · 2 comments
Open

Is there any way to cancel certain shortcut keys? #680

1trapbox opened this issue Jan 15, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@1trapbox
Copy link

What do you want?

Can be customized to cancel certain shortcut keys

Why do you want this?

Conflicts with other tools, e.g atuin ^r

Who else would benefit from this?

Anyone who wants to disable some of the built-in shortcuts

How should it work?

I'm using atuin as a history tool.
This uses the same ^r shortcut as zsh-autocomplete.
I had to place the bindkey '^r' _atuin_search_widget at the bottom of .zshrc
but when I reboot the system, press ctrl + r, the zsh-autocomplete history widget still appears.
I'm currently only able to get ctrl + r, using the atuin widget, to work via source ~/.zshrc.
Is there a more convenient way to disable some keys? Like adding environment variables, etc.

@1trapbox 1trapbox added the enhancement New feature or request label Jan 15, 2024
@1trapbox
Copy link
Author

.autocomplete__key-bindings

I tried modifying this file. comment some code snippets.
but ctrl + r, still brings up the history widget.

#!/bin/zsh
zmodload zsh/complist
zmodload -F zsh/parameter p:funcstack p:functions
zmodload -F zsh/terminfo p:terminfo

local -Pa prefix=( '\e'{\[,O} )
typeset -ga _autocomplete__key_up=( ${^prefix}A )
typeset -ga _autocomplete__key_down=( ${^prefix}B )
typeset -ga _autocomplete__key_alt_up=( '\e'$^_autocomplete__key_up '\e[1;3A' )
typeset -ga _autocomplete__key_alt_down=( '\e'$^_autocomplete__key_down '\e[1;3B' )

${0}:bind() {
  local -P keymap=$1 widget=$2
  shift 2
  builtin bindkey -M "$keymap" "${@:^^widget}"
}

${0}:bind-menu() {
  0=${0%:*}
  ${0}:bind isearch "$@"
  ${0}:bind menuselect "$@"
}

${0}:bound() {
  [[ $( builtin bindkey -M "$1" "$3" ) == \"[^[:space:]]##\"\ $2 ]]
}

${0}:rebind() {
  0=${0%:*}

  local -P keymap=$1 old=$2 new=$3 key=
  shift 3
  for key; do
    ${0}:bound "$keymap" "$old" "$key" &&
        builtin bindkey -M "$keymap" "$key" "$new"
  done
}

${0}:unbind() {
  0=${0%:*}
  ${0}:bound "$1" "$2" "$3" &&
      builtin bindkey -M "$1" -r "$2"
}

${0}:bind main    up-line-or-search $_autocomplete__key_up[@]
${0}:bind-menu    up-history        $_autocomplete__key_up[@]
${0}:bind main  down-line-or-select $_autocomplete__key_down[@]
${0}:bind-menu  down-history        $_autocomplete__key_down[@]

${0}:rebind emacs   up-line-or-{history,search} '^P'
${0}:bind-menu      up-history                  '^P'
${0}:rebind emacs down-line-or-{history,select} '^N'
${0}:bind-menu    down-history                  '^N'

${0}:rebind vicmd   up-line-or-{history,search} 'k'
${0}:rebind vicmd down-line-or-{history,select} 'j'

local -Pa menukeys=(
                '^@' accept-and-hold
                '^_' .undo
    "$terminfo[kpp]" backward-word
    "$terminfo[knp]"  forward-word
               '^[v' backward-word
                '^V'  forward-word
)
builtin bindkey -M isearch    "$menukeys[@]"
builtin bindkey -M menuselect "$menukeys[@]"

${0}:precmd() {
  emulate -L zsh
  setopt $_autocomplete__func_opts[@]

  0=${0%:*}

  ${0}:rebind main  expand-or-complete        complete-word '\t'
  ${0}:rebind main  expand-or-complete-prefix complete-word '\t'
  ${0}:rebind main  menu-expand-or-complete   complete-word '\t'

  local backtab=$terminfo[kcbt]
  ${0}:rebind main undefined-key insert-unambiguous-or-complete "$backtab"

  ${0}:bound main complete-word '\t' &&
      ${0}:bind-menu accept-line '\t'

  ${0}:bind main  history-search-backward $_autocomplete__key_alt_up[@]
  ${0}:bind-menu  vi-backward-blank-word  $_autocomplete__key_alt_up[@]
  ${0}:bind main  menu-select             $_autocomplete__key_alt_down[@]
  ${0}:bind-menu  vi-forward-blank-word   $_autocomplete__key_alt_down[@]

#  ${0}:rebind emacs history-search-backward history-search-backward '\ep'
#  ${0}:bind-menu                            vi-backward-blank-word  '\ep'
#  ${0}:rebind emacs history-search-forward  menu-select             '\en'
#  ${0}:bind-menu                            vi-forward-blank-word   '\en'

#  ${0}:rebind vicmd vi-rev-repeat-search    history-search-backward 'N'
#  ${0}:rebind vicmd vi-repeat-search        menu-select             'n'

#  ${0}:rebind emacs history-incremental-search-backward{,}         '^R'
#  ${0}:bind-menu    history-incremental-search-backward            '^R'
#  ${0}:rebind emacs history-incremental-search-forward menu-search '^S'
#  ${0}:bind-menu    history-incremental-search-forward             '^S'

#  ${0}:rebind vicmd {vi-history,history-incremental}-search-backward '/'
#  ${0}:rebind vicmd  vi-history-search-forward         menu-search   '?'

  unset -m '_autocomplete__key_*'
  unfunction ${0}:{{,re,un}bind,bound}
}

@Numenorean
Copy link

Numenorean commented Mar 17, 2024

it took me almost two days to find out that there is apparently no way to rebind builtin shortcuts because of that shitty precmd trick to bind the keys just before the prompt appears. Works only if I manually execute bindkey in prompt.

What I tried:

source $XDG_DATA_HOME/zinit/plugins/marlonrichert---zsh-autocomplete/zsh-autocomplete.plugin.zsh

testfunc() {
    echo "test"
    zle       reset-prompt
}
zle -N testfunc
bindkey '\E[1;3A' testfunc # Alt-Up

Zsh log:

+/home/emon/.config/zsh/.zshrc:169> bindkey '\E[1;3A' testfunc
+.autocomplete__key-bindings:precmd:16> .autocomplete__key-bindings:bind main history-search-backward '\e\e[A' '\e\eOA' '\e[1;3A'
+.autocomplete__key-bindings:bind:3> bindkey -M main '\e\e[A' history-search-backward '\e\eOA' history-search-backward '\e[1;3A' history-search-backward

IMHO that should be added to README to not to misguide the other people

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants