diff --git a/.config/shell/functions b/.config/shell/functions index c80af44..71337e3 100644 --- a/.config/shell/functions +++ b/.config/shell/functions @@ -1,14 +1,23 @@ # Shell configuration for Zsh, works almost completely for Bash as well export LESS=-R -alias info='info --vi-keys' export DIST=$(lsb_release --id | cut -d' ' -f2) +alias info='info --vi-keys' # Some aliases -alias conf='{ fd -t f . --full-path $(xdg-user-dir CONFIG) /etc; echo ~/.gradle/gradle.properties } | fzf --tiebreak=end,length --preview "bat --color=always --style=numbers --line-range :500 {}" --bind "enter:execute(test -O {} && $EDITOR {} || sudoedit {}),del:execute(gio trash {}),change:top"' alias editgrub='sudoedit /etc/default/grub && sudo update-grub' alias editenv='sudoedit /etc/environment' +conf() { + conf_cache="$XDG_CACHE_HOME/conf" + conf_tmp="${conf_cache}.tmp" + conf_extra="$XDG_CONFIG_HOME/conf-extra" + touch "$conf_cache" + # | xargs file | grep text | cut -d':' -f1 # this filters out non-text files, but it's ridiculously slow + sel="$({ cat $conf_cache $conf_extra; fd --type file --size -1m --hidden --exact-depth 1 . ~; fd --type file --size -1m --max-depth 3 . --full-path $XDG_CONFIG_HOME /etc } | awk '!a[$0]++' | fzf -1 -0 --tiebreak=end,length --preview 'bat --color=always --style=numbers --line-range :200 {}')" + test "$sel" && ((echo "$sel" | cat - "$conf_cache" | head -9 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache") & (test -O "$sel" && $EDITOR "$sel" || sudoedit "$sel")) +} + CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}" alias r="reset && exec zsh" editshell() { @@ -31,9 +40,17 @@ alias tw='timew' alias c='clear' alias q='exit' alias v='nvim' +alias lar='last | tac' alias grp='grep -iIn' alias grpr='grp -r' -b() { bat "$@" 2>/dev/null || ls -lah "$@" } +b() { + arg=$(test $# -gt 0 && echo "${@: -1}" || echo .) + file $arg | grep -v --color=never directory + case "$(file --dereference $arg)" in + *directory) ls -lAHh "$@";; + *text*) bat --style=numbers "$@";; + esac +} cdd() { cd "$@" 2>/dev/null || cd "$(dirname "$1")" "${@:2}" } tr() { tree -C -a -L 2 "$@" | less } alias sc='sudo systemctl' @@ -69,11 +86,13 @@ mozedit() ( cat "$tmpfile2" | sed "$2" | lz4 -l | dd bs=1 skip=8 seek=12 of="$file" conv=notrunc ) -theme_save="$(xdg-user-dir CACHE)/theme" -theme_default="dark" +theme_save="/tmp/theme" +theme_default="$(d=$(date +%H) && test $d -lt 7 && test $d -gt 17 && echo dark || echo light)" theme() { theme="$1" - if test "$2" = "kde" || test "$theme" != "$THEME" -a "$2" != "q" + if test "$2" = "kde" + then kcmshell5 kcm_lookandfeel + elif test "$theme" != "$THEME" -a "$2" != "q" then kcmshell5 kcm_lookandfeel & fi export THEME="$theme" @@ -83,14 +102,14 @@ theme() { case "$theme" in (light) export BAT_THEME="OneHalfLight" - konsoleprofile colors="Light" - sed -i 's/#\(include.*\/light-256.theme\)/\1/' "$(xdg-user-dir CONFIG)/taskrc" + konsoleprofile colors="Light" + sed -i 's/#\(include.*\/light-256.theme\)/\1/' "$XDG_CONFIG_HOME/taskrc" #mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/' ;; (dark) export BAT_THEME="OneHalfDark" - konsoleprofile colors="Breeze" - sed -i 's/^include.*light-256.theme/#\0/' "$(xdg-user-dir CONFIG)/taskrc" + konsoleprofile colors="Breeze" + sed -i 's/^include.*light-256.theme/#\0/' "$XDG_CONFIG_HOME/taskrc" #mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/' ;; esac @@ -275,6 +294,10 @@ aptrepos() { aptupdate } alias aptinstall='sudo apt-get install' +aptinstalls() { + apt-cache show "$1" | grep --color=never '\(Version\|Description\):' + aptinstall "$@" +} aptrepoinstall() { aptrepo -yu $1 if (( $# > 1 )) @@ -316,12 +339,11 @@ download-debs() { typeset -A _clean_map if test "$BASH_VERSION" -then _clean_map=([m]=~/.mozilla [c]=~/.cache) -else _clean_map=(m ~/.mozilla c ~/.cache) +then _clean_map=([c]=$XDG_CACHE_HOME [t]=/var/tmp) +else _clean_map=(c $XDG_CACHE_HOME t /var/tmp) fi -_clean_folders=(~/.oracle_jre_usage ~/.gradle-kotlin-dsl ~/.thumbnails) +_clean_folders=() clean() { - for f in $_clean_folders do test -d $f && rm -rv $f done @@ -329,7 +351,7 @@ clean() { highlight "g to clean gradle" if [[ $1 =~ "g" ]]; then find ~/.gradle/* -maxdepth 0 -not -name "gradle.properties" -exec rm -r {} \; -print - find ~/daten/projects/ -name .gradle -print -exec rm -r {} \; + find $projects_dir -name .gradle -print -exec rm -r {} \; fi if test "$BASH_VERSION"; then diff --git a/.config/shell/git b/.config/shell/git index 86384c5..081d04d 100644 --- a/.config/shell/git +++ b/.config/shell/git @@ -2,11 +2,16 @@ alias bfg='java -jar ~/daten/dropbox/tools/bfg-1.13.0.jar' alias magit='nvim -c MagitOnly' -# if in home or xdg-config and not within a git directory, replace git by yadm +# if in home or under XDG_CONFIG_HOME and not within a git directory, replace git by yadm git() { - case "$PWD" in - ~|$(xdg-user-dir CONFIG)*) - if ! command git rev-parse --show-toplevel 2&>/dev/null; then + test "$1" = "config" || case "$PWD" in + $HOME|$XDG_CONFIG_HOME|$LAST_YADM) + yadm "$@" + return + ;; + $XDG_CONFIG_HOME*) + if ! command git rev-parse --show-toplevel &>/dev/null; then + export LAST_YADM="$PWD" yadm "$@" return fi;; @@ -40,7 +45,9 @@ glno() { alias gfs='git fetch && git status -s -b' alias glu='git pull upstream $(git curbranch)' alias gluu='git pull upstream $(git curbranch) && git push --no-verify' -alias gcap!='git commit -a --amend --no-edit && git push --force-with-lease' +alias gcap!='git commit --all --amend --no-edit && git push --force-with-lease' +alias gpf='gt push --force-with-lease' +alias grh='git reset HEAD~' alias grh!='git reset --hard' alias grhr='git reset --hard $(git rev-parse --abbrev-ref --symbolic-full-name @{u})' alias gitgc='git gc && git repack -a -d' @@ -73,7 +80,7 @@ gitrebranch() { gitrmbranch() { branch=${1:-$(git curbranch)} git push -d origin $branch - test $1 || git checkout master + test $1 || git checkout master || git checkout $(cat .git/refs/remotes/origin/HEAD | cut -d'/' -f4) git branch -D $branch } @@ -132,7 +139,7 @@ gitclone() { remote=$(gitremote "$@") echo $remote git clone $remote "${@:4}" - cd "$(basename ${remote/.git})" + cd "$(test $4 && echo $4 || basename ${remote/.git})" } gitfork() { @@ -153,12 +160,15 @@ gitorigin() { done } -# set repo as upstream +# set repo as upstream or add a new remote gitupstream() { - local name="${2:-upstream}" - git remote remove $name 2>/dev/null - git remote add $name "$(git remote -v | grep origin | head -1 | cut -f2 | cut -d':' -f1):$1/$(git remote -v | grep origin | head -1 | cut -f2 | cut -d'/' -f2 | cut -d' ' -f1)" - git remote -v && git fetch $name || ( last=$? && echo "git fetch failed, aborting!" && return $last ) + if test "$2" + then local name="$2"; git remote remove "$2" >/dev/null + else git remote get-url upstream &>/dev/null && local name="$1" || local name="upstream" + fi + local origin="$(git remote get-url origin)" + git remote add -f $name "$(echo $origin | cut -d':' -f1):$1/$(echo $origin | cut -d'/' -f2)" + git remote -v } # }}}