config/shell: consistent configuration editing helpers

This commit is contained in:
xeruf 2022-08-09 11:00:29 +02:00
parent 7f8d124e35
commit 5a19b3e1f3
4 changed files with 19 additions and 11 deletions

View File

@ -55,6 +55,7 @@ h() {
help "$@" help "$@"
# TODO resolve aliases in 'h' alias # TODO resolve aliases in 'h' alias
# TODO call 'wh' on scripts instead # TODO call 'wh' on scripts instead
# TODO integrate tldr
} }
xtrace() { xtrace() {
@ -66,6 +67,7 @@ xtrace() {
# Shows source code for command, resolving nested aliases # Shows source code for command, resolving nested aliases
wh() { wh() {
res=$(which "$@") || return $? res=$(which "$@") || return $?
# only works in zsh, not bash
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh && then echo "$res" | bat --style=plain --language=sh &&
tool="$(echo "$res" | head -1 | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)" tool="$(echo "$res" | head -1 | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)"
@ -90,10 +92,14 @@ pathadd() {
alias rs="reset && source $HOME/.zshenv && exec $SHELL" alias rs="reset && source $HOME/.zshenv && exec $SHELL"
alias hist='print -z $(history | grep -v "killm " | tac | fzf --tiebreak=index --bind='"'"'del:execute(sed "\;$(echo {4..})$d" -i.bak $HISTFILE)'"'"' | sed "s|^ \+||" | cut -d" " -f5-)' alias hist='print -z $(history | grep -v "killm " | tac | fzf --tiebreak=index --bind='"'"'del:execute(sed "\;$(echo {4..})$d" -i.bak $HISTFILE)'"'"' | sed "s|^ \+||" | cut -d" " -f5-)'
# Edit shell config files alias es='edit-shell'
# Exit code 1 - no change, 2 - file not found alias ec='edit-config'
alias eb='edit-bin'
CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}" CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}"
edshell() { # Fuzzy find and edit shell config files
# Exit code: 1 - no change, 2 - file not found
edit-shell() {
case $1 in case $1 in
("") file="$CONFIG_SHELL_FUNCTIONS";; ("") file="$CONFIG_SHELL_FUNCTIONS";;
(zsh) file="$CONFIG_ZSH/.zshrc";; (zsh) file="$CONFIG_ZSH/.zshrc";;
@ -108,15 +114,17 @@ edshell() {
test -f "$file" && checksum="$(md5sum "$file")" test -f "$file" && checksum="$(md5sum "$file")"
$EDITOR "$(test "$line" && case "$EDITOR" in (nvim) echo "+normal! ${line}ggzx";; (*vi*) echo "+$line";; (emacs*|*/emacs*) echo "+${line}";; esac || echo "--")" "${file%:*}" $EDITOR "$(test "$line" && case "$EDITOR" in (nvim) echo "+normal! ${line}ggzx";; (*vi*) echo "+$line";; (emacs*|*/emacs*) echo "+${line}";; esac || echo "--")" "${file%:*}"
test -s "$file" || return 1 test -s "$file" || return 1
# Reload shell config upon change
test "$checksum" != "$(md5sum $file)" && rs test "$checksum" != "$(md5sum $file)" && rs
} }
# Edit a file in the PATH # Edit an executable in the PATH
edbin() { edit-bin() {
# TODO resolve simple alias alias=$(alias "$1" 2>/dev/null | sed "s/.*='\?\([-A-z]*\)'\?\$/\1/")
if f="$(which $1 2>/dev/null)" && test -f "$f" toedit=${alias:-$1}
if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
then edit "$f" then edit "$f"
else edshell -f "$1" else edit-shell -f "$toedit"
test $? != 2 || edit "$HOME/.local/bin/${2:-scripts}/$1" test $? != 2 || edit "$HOME/.local/bin/${2:-scripts}/$1"
fi fi
} }

View File

@ -1,4 +1,4 @@
#compdef edbin #compdef edit-bin
_files -W $HOME/.local/bin/scripts _files -W $HOME/.local/bin/scripts
#compadd $(ls --almost-all --indicator-style=slash $HOME/.local/bin/scripts) #compadd $(ls --almost-all --indicator-style=slash $HOME/.local/bin/scripts)
#$(echo $PATH | cut -d':' -f1 | xargs ls -pA) #$(echo $PATH | cut -d':' -f1 | xargs ls -pA)

View File

@ -1,2 +1,2 @@
#compdef edshell #compdef edit-shell
compadd -V unsorted zsh profile $(ls -pA $CONFIG_SHELLS) compadd -V unsorted zsh profile $(ls -pA $CONFIG_SHELLS)

View File

@ -15,7 +15,7 @@ listconf() {
} | dedup } | dedup
} }
conf_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/edconf" conf_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/$(basename "$0")"
conf_cache="$conf_cache_dir/files" conf_cache="$conf_cache_dir/files"
conf_tmp="${conf_cache}.tmp" conf_tmp="${conf_cache}.tmp"
mkdir -p "$conf_cache_dir" mkdir -p "$conf_cache_dir"