Last active
September 18, 2023 02:51
-
-
Save PostgreSqlStan/285437f74fe7acb526d24a9c169004ea to your computer and use it in GitHub Desktop.
zsh function: list key bindings with different display options
This file contains 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
# NEW VERSION: see gist comment for link to refactored version | |
###################################################################################### | |
# started as an alias, see https://postgresqlstan.github.io/zsh/binds/ | |
# my 1st attempt to do anything semi-complicated with shell scripting | |
# don't know what i'm doing, just hacking pieces together until it works | |
bindlist () { | |
local flag_help flag_list flag_pretty | |
local usage=( | |
"Usage:" | |
" bindlist list bindings" | |
" bindlist -h|--help print this message" | |
"Options:" | |
" -a --all include all bindings" | |
" -l --list single column output" | |
" -r --raw bindkey format" | |
) | |
zmodload zsh/zutil | |
zparseopts -D -F -- \ | |
{a,-all}=flag_all \ | |
{h,-help}=flag_help \ | |
{l,-list}=flag_list \ | |
{r,-raw}=flag_raw \ | |
|| return 1 | |
[[ -z "$flag_help" ]] || { print -l $usage && return } | |
local _e_patterns _cmd _list | |
add_pattern() { | |
for item in ${*} | |
_e_patterns=${_e_patterns}$(printf ' -e '\''%s'\' ${item}) | |
} | |
if ! (( $#flag_all )); then | |
# grep -v (exclude) patterns | |
add_pattern "\^\[\[" # match "^[[" | |
add_pattern "\^\[O[A-H]" # match "^[[OA"..."^[[OH" | |
add_pattern "^\"\\\M" # match "\M-^@"-"\M-^?" | |
add_pattern "\"-\"~\"" # match " "-"~" | |
_cmd=$(printf 'grep -v%s\n' "${_e_patterns}") | |
_list=$(bindkey | eval "$(printf '%s' ${_cmd})") | |
else | |
_list=$(bindkey) | |
fi | |
if ! (( $#flag_raw )); then | |
# sed patterns | |
_e_patterns= | |
add_pattern 's/^"//g' # remove 1st quote | |
add_pattern 's/" / /g' # remove 2nd quote | |
add_pattern 's/\^\[/⌥/g' # replace meta chars with option symbol | |
add_pattern 's/\^\?/DEL/g' # replace ^? with DEL | |
_cmd=$(printf 'sed %s\n' "${_e_patterns}") | |
_list=$(echo "${_list}" | eval "$(printf '%s' ${_cmd})") | |
fi | |
if (( $#flag_list )); then | |
print -- "${_list}" | |
else | |
IFS=$'\n' | |
print -c -- "${(f)_list}" | |
unset IFS | |
fi | |
} | |
compdef _gnu_generic bindlist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New version: https://gist.github.com/PostgreSqlStan/b04a27d05710d857cbefb5801997915a