config/shell: update & move aliases & helpers

Among others to remove excessive sudo prompts
This commit is contained in:
xeruf 2021-09-16 14:18:26 +02:00
parent 27b93aed3e
commit 1e399f122b
10 changed files with 68 additions and 50 deletions

View File

@ -1,3 +1,3 @@
[termname]*vbell: True
*.font: mono:pixelsize=28:antialias=true:autohint=true;
#*.font: mono:pixelsize=28:antialias=true:autohint=true;
*.background: #222

View File

@ -45,25 +45,14 @@ ulimit -c unlimited # Enable core dumps
which lsb_release >/dev/null && export DIST=$(lsb_release --id | cut -d' ' -f2) || true
unalias rd 2>/dev/null
rd() {
for f in "$@"
do
if test -d "$f"; then
if test -d "$f/.git"
then echo "Force delete git project?" && read answer && test "$answer" = "y" && rm -rf "$f"
else find "$f" -maxdepth 1 -type d -empty -printf "Removing empty %p\n" -delete
fi
else rm -i "$f"
fi
shift
done
}
# like "which", but shows contents if it resolves to a file
# and resolves recursive aliases
wh() {
res=$(which "$@")
if expr "$res" : "${@:$#}: aliased to" >/dev/null
then echo "$res" | bat --style=plain --language=sh && wh $(expr "$res" : "${@:$#}: aliased to ${@:$#} " >/dev/null && echo "-p") $(echo "$res" | cut -d' ' -f$(expr 5 '&' "$res" : ".*\(sudo\|noglob\)" '|' 4))
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh &&
wh $(expr "$res" : "${@:$#}: aliased to ${@:$#} " >/dev/null && echo "-p") $(echo "$res" | cut -d' ' -f$(expr 5 '&' "$res" : ".*\(sudo\|noglob\)" '|' 4) | cut -d'(' -f2)
# use command which for other shells
else test -r "$res" && b --style=header "$res" || echo "$res" | bat --style=plain --language=sh
fi
@ -74,6 +63,8 @@ alias calc='rlwrap -a bc -l'
alias logoff="loginctl terminate-user $USER"
blues() { bluedevil-sendfile $(echo "$@" | xargs -n 1 realpath | xargs -n 1 echo -n " --files") }
alias u='topgrade'
alias j='jrnl'
jn() { jrnl -to today "$@" | bat --style=plain --pager="less +G" }
compdef jn=jrnl
@ -84,9 +75,6 @@ alias edgrub='sudoedit /etc/default/grub && sudo update-grub'
alias edenv='sudoedit /etc/environment'
alias edpart='sudoedit /etc/fstab && sudo findmnt --verify'
# Underline arg
highlight() { echo "$1" }
alias dedup='awk '"'"'!a[$0]++'"'"
listconf() {
{ cat "$conf_cache"; test -f "$conf_extra" && cat "$conf_extra";
@ -102,7 +90,7 @@ edconf() {
mkdir -p "$conf_cache_dir"
touch "$conf_cache"
sel=$(listconf | fzf -1 -0 --tiebreak=end,length --preview '$(test -r "{}" || echo "sudo") bat --color=always --style=numbers --line-range :200 {}' --query="$1" --history "$conf_cache_dir/searches")
test "$sel" && ((echo "$sel" | cat - "$conf_cache" | head -30 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache") & edit "$sel")
test "$sel" && ((echo "$sel" | cat - "$conf_cache" | head -20 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache") & edit "$sel")
}
alias hist='print -z $(history | tac | fzf --tiebreak=index --bind="'"del:execute(echo \"'/;{4..}$/d'\" && sed '/;{4..}$/d' -i.bak $HISTFILE)"'" | sed "s|^ \+||" | cut -d" " -f5-)'
@ -130,7 +118,7 @@ edshell() {
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
cd() {
test ! -d "$1" && test $# -eq 1 && dir=$(f --glob "$1*" "$DATA" --maxdepth 2 --type d --max-results 1) &&
test "$1" != "-" -a ! -d "$1" -a $# -eq 1 && dir=$(f --glob "$1*" "$DATA" --maxdepth 2 --type d --max-results 1) &&
test -n "$dir" && cd "$dir" && return
builtin cd $1 &&
command ls --file-type --group-directories-first --color=always --format=vertical -w $COLUMNS | head -3
@ -157,9 +145,8 @@ t() {
fi
fi
}
alias tw='timew'
twsm() { timew summary $(date --date="Monday $1 week ago" -I) to tomorrow :ids ${@:2} }
# Create a temporary timewarrior database for testing
alias twtest='( cp -r "$TIMEWARRIORDB" /tmp/tw-bak && TIMEWARRIORDB=/tmp/timewarriordb-test/$(date +%s) && mkdir -p "$TIMEWARRIORDB"/data && :> "$TIMEWARRIORDB"/timewarrior.cfg && $SHELL )'
alias expr='noglob expr'
@ -222,7 +209,7 @@ unv() { strings $1 | sed 's/5$//' | dedup }
hex() { hexyl "$@" | "${PAGER:-less}" }
# Applications
alias dict="(builtin cd $XDG_DATA_HOME/dictcc && rlwrap perl dictcc-helper.pl)"
alias dict="rdictcc -d $XDG_DATA_HOME/dictcc" #"(builtin cd $XDG_DATA_HOME/dictcc && rlwrap perl dictcc-helper.pl)"
alias startMinecraftServer='curl https://ipinfo.io/ip | xclip -sel clip && cd ~/daten/games/sharedgames/minecraft/server && java -jar forge-1.12.2-14.23.5.2768-universal.jar -mx 8G'
test -d $HOME/.dropbox && dropbox_path="$(cat $HOME/.dropbox/info.json | grep -Po '"'"path"'"\s*:\s*"\K([^"]*)')"
@ -377,7 +364,7 @@ resetdocker() {
typeset -A _clean_map
if test "$BASH_VERSION"
then _clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp)
else _clean_map=(h $XDG_CACHE_HOME t /var/tmp)
else _clean_map=(h $XDG_CACHE_HOME t /var/tmp v /var/cache)
fi
_clean_folders=()
clean() {
@ -391,21 +378,9 @@ clean() {
find $projects_dir -name .gradle -print -exec rm -r {} +
fi
highlight "m to clean mac-files"
if [[ $1 =~ "m" ]]; then
find -iname '.spotlight*' -exec rm -rI {} +
find -name '*.DS_Store' -delete
fi
highlight "e to clean electron caches"
[[ $1 =~ "e" ]] && find "$XDG_CONFIG_HOME" -type d -name "*Cache" -exec rm -r {} + -prune
highlight "c to clean local caches"
if [[ $1 =~ "c" ]]; then
find -name ".gradle" -o -name "generated" -o -name "dist_newstyle" -o -name "cache" -o -name "node_modules" -print -exec rm -r {} + -prune
find -name "*build" -print -exec rm -rI {} + -prune
fi
if test "$BASH_VERSION"; then
for k in "${!_clean_map[@]}"; do
highlight "$k to delete $_clean_map[$k]"
@ -414,8 +389,9 @@ clean() {
done
else
for k in "${(@k)_clean_map}"; do
highlight "$k to delete $_clean_map[$k]"
[[ $1 =~ "$k" ]] && rm -rfv "$_clean_map[$k]"
folder=$_clean_map[$k]
highlight "$k to delete $folder"
[[ $1 =~ "$k" ]] && sudo rm -rf "$folder" && echo "Deleted $folder"
done
fi
@ -425,7 +401,19 @@ clean() {
export aptclean_last=$aptclean_cur
fi
highlight "f to delete empty folders"
highlight "c to recursively remove development caches"
if [[ $1 =~ "c" ]]; then
find -name ".gradle" -o -name "generated" -o -name "dist_newstyle" -o -name "cache" -o -name "node_modules" -print -exec rm -r {} + -prune
find -name "*build" -print -exec rm -rI {} + -prune
fi
highlight "m to recursively remove mac-files"
if [[ $1 =~ "m" ]]; then
find -iname '.spotlight*' -exec rm -rI {} +
find -name '*.DS_Store' -delete
fi
highlight "f to recursively remove empty folders"
[[ $1 =~ "f" ]] && find -empty -type d -delete -print
highlight "o to optimize extensively"

View File

@ -1,7 +1,5 @@
test "$DISPLAY" || return 0
which dragon >/dev/null && alias drag='dragon -x -a' || alias drag='dragon-drag-and-drop -x -a'
mozedit() (
set -eo pipefail
file="$1"

View File

@ -7,9 +7,11 @@ set -eo pipefail
for last; do true; done
last=${last:-.}
tput setaf 4 && $(test -r "$last" || echo "sudo") file -E "$last" | ( grep -v --color=never 'directory$' || true )
# Elevate permissions if the file exists but is not readable
# TODO: find the first visible directory up the tree and check if that is accessible
test -e "$last" || test -e "$(dirname $last)" && { test -r "$last" || elevate=sudo; }
tput setaf 4 && $elevate file -E "$last" | ( grep -v --color=never 'directory$' || true )
elevate="$(test -r "$last" || echo "sudo")"
case "$($elevate file --dereference --mime "$last")" in
*inode/directory*) tput sgr0 && $(test -x "$last" || echo "sudo") ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@" | less -XF;;
*binary) ;;

3
.local/bin/scripts/drag Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
# Start single all-in-one dragon-drag-and-drop with args or pwd
$(command which dragon 2>/dev/null || echo dragon-drag-and-drop) -x -a ${@:-.}

View File

@ -1,9 +1,13 @@
#!/bin/sh
# open info-page, man-page or buffered help flag
set -o pipefail
paginate="${PAGER:-less} +Gg"
case $1 in
(vlc) unbuffer vlc --full-help "${@:2}" | ${PAGER:-less} --quit-if-one-screen;;
(zsh|zmv) # https://unix.stackexchange.com/questions/19290/zmv-for-zsh-dry-runs-and-man-pages
# need to install zsh-doc for info
info --vi-keys --index-search="${@:$#}" zsh || LESS="$LESS +/^ *zmv *\\[" man zshcontrib;;
(*) info "$1" -w | grep -v "manpages" && pinfo "$@" || man "$@" || "$@" --help | ${PAGER:-less} --quit-if-one-screen;;
search="${@:$#}"
# need to install zsh-doc package for info pages
info --vi-keys $(test $search != zsh && echo --index-search=$search) zsh || LESS="$LESS +/^ *${@:$#} *\\[" man zshall;;
(vlc) unbuffer vlc --full-help "${@:2}" | $paginate;;
(gh|chordpro) $@ --help | $paginate;;
(*) info "$1" -w | grep -v "manpages" && pinfo "$@" || man "$@" || ( "$@" --help || "$@" -help ) 2>&1 | $paginate;;
esac

3
.local/bin/scripts/highlight Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
# Underline arg
echo "$1"

16
.local/bin/scripts/rd Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
# Remove recursively safely
for f in "$@"
do
if test -d "$f"; then
if test -e "$f/.git" || test -e "$f/packed-refs"
then echo -n "Force delete git project $f?"
read answer
test "$answer" = "y" && rm -rf "$f"
else find "$f" -maxdepth 1 -type d -empty -printf "Removing empty %p\n" -delete
test -e "$f" && echo -n "$f " >&2 && rm -rI "$f"
fi
else rm -i "$f"
fi
shift
done

View File

@ -2,5 +2,5 @@
# shows size statistics for subfolders
# max depth is equal to the first argument if it is a number or 1
case "$1" in [0-9]) depth=$1; shift;; esac
(sudo du --max-depth "${depth:-1}" -xhat 50M "$@" | sort -h | grep -v "^0") ||
(sudo du --max-depth "${depth:-1}" -xha "$@" | sort -h | tail)
(du --max-depth "${depth:-1}" -xhat 50M "$@" | sort -h | grep -v "^0") ||
(du --max-depth "${depth:-1}" -xha "$@" | sort -h | tail)

4
.local/bin/scripts/twsm Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
# Timewarrior summary of the last n weeks
timew summary $(date --date="Monday $1 week ago" -I) to tomorrow :ids ${@:2} |
less --quit-if-one-screen +G