Last active
March 25, 2022 22:30
-
-
Save jeebak/aa8135d7ebfa40c4ab1387fe48aaf8a5 to your computer and use it in GitHub Desktop.
Overload zsh / zle keybindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__dotmatrix::ctrl-key() { | |
# Run: zsh -f -c 'set -o emacs; bindkey' | |
# to get a list of default keybindings. | |
# Some keybindings, like those for cursor movements: ^a, ^e, ^f, ^b, etc. are | |
# meaningful when there is text on the command line. If the command line is | |
# empty, then these keybindings are essentially no-ops. | |
# Using the $CURSOR and $BUFFER zle variables, we can take advantage of this | |
# and assign custom behavior to these bindings when the command line is empty. | |
# Some keybindings, like: ^d, ^n, ^p, ^v, ^y, and ^z perform their actions | |
# whether the command line is empty or not, so these are excluded here. The | |
# ^c binding remainds untouched. | |
# Reserved: | |
# Fzf bindings: ^I: fzf-completion ^R: fzf-history-widget, ^T: fzf-file-widget | |
# Git bindings prefix: ^G | |
# Tmux Navigation: ^{H,J,K,L} | |
local -A widgets | |
if [[ $CURSOR != 0 && -n $BUFFER ]]; then | |
widgets=( | |
# Left Hand | |
$'\x11' push-line # '^Q' | |
$'\x17' backward-kill-word # '^W' | |
$'\x05' end-of-line # '^E' | |
$'\x01' beginning-of-line # '^A' | |
$'\x06' forward-char # '^F' | |
$'\x02' backward-char # '^B' | |
# Right Hand | |
$'\x15' kill-whole-line # '^U' | |
$'\x0f' accept-line-and-down-history # '^O' | |
) | |
[[ -n "${widgets[$KEYS]}" ]] && zle "${widgets[$KEYS]}" | |
return | |
fi | |
local cmd output | |
case $KEYS in | |
$'\x00') # '^@' Ctrl-space | |
:;; | |
# Left Hand | |
$'\x11') # '^Q' | |
:;; | |
$'\x17') # '^W' List all "types" (funcs, aliases, keywords, executables, etc.) | |
output=( | |
$(type -m '*' | | |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS" fzf \ | |
--height='70%' \ | |
--tiebreak='begin,length,end,index' | |
) | |
) | |
[[ -n "$output[1]" ]] && LBUFFER="$output[1] " | |
zle redisplay | |
;; | |
$'\x05') # '^E' [E]xplore | |
if command -v nnn > /dev/null; then cmd="nnn -d -H -n" | |
elif command -v fff > /dev/null; then cmd="fff" | |
elif command -v lf > /dev/null; then cmd="lf" | |
elif command -v ranger > /dev/null; then cmd="ranger" | |
elif command -v vifm > /dev/null; then cmd="vifm" | |
elif command -v mc > /dev/null; then cmd="mc" | |
elif command -v fd > /dev/null; then cmd="fd" | |
else cmd="ls -tral" | |
fi | |
LBUFFER="$cmd" | |
zle accept-line | |
;; | |
$'\x01') # '^A' | |
:;; | |
$'\x13') # '^S' | |
:;; | |
$'\x06') # '^F' [F]iles | |
LBUFFER="ls -tral" | |
zle accept-line | |
;; | |
$'\x02') # '^B' "Go [b]ack" (popd) | |
if [[ "$(dirs -l)" != "$PWD" ]]; then | |
popd | |
zle send-break | |
fi | |
;; | |
# Right Hand | |
$'\x15') # '^U' | |
:;; | |
$'\x0f') # '^O' | |
:;; | |
*) | |
:;; | |
esac | |
} | |
zle -N __dotmatrix::ctrl-key | |
# Left Hand | |
bindkey '^@' __dotmatrix::ctrl-key | |
bindkey '^q' __dotmatrix::ctrl-key | |
bindkey '^w' __dotmatrix::ctrl-key | |
bindkey '^e' __dotmatrix::ctrl-key | |
bindkey '^a' __dotmatrix::ctrl-key | |
bindkey '^s' __dotmatrix::ctrl-key | |
bindkey '^f' __dotmatrix::ctrl-key | |
bindkey '^b' __dotmatrix::ctrl-key | |
# Right Hand | |
bindkey '^u' __dotmatrix::ctrl-key | |
bindkey '^o' __dotmatrix::ctrl-key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment