dotfiles/.config/shell/functions

501 lines
15 KiB
Plaintext
Raw Normal View History

2020-03-08 19:18:49 +00:00
# Shell configuration for Zsh, works almost completely for Bash as well
2020-03-19 09:52:15 +00:00
2020-03-08 19:18:49 +00:00
export LESS=-R
export DIST=$(lsb_release --id | cut -d' ' -f2)
2020-06-26 21:25:53 +00:00
alias info='info --vi-keys'
2020-03-08 19:18:49 +00:00
2020-03-19 09:52:15 +00:00
# Some aliases
alias editgrub='sudoedit /etc/default/grub && sudo update-grub'
alias editenv='sudoedit /etc/environment'
2020-09-04 10:09:08 +00:00
alias dedup='awk '"'"'!a[$0]++'"'"
edconf() {
conf_cache_dir="$XDG_CACHE_HOME/edconf"
conf_cache="$conf_cache_dir/files"
2020-06-26 21:25:53 +00:00
conf_tmp="${conf_cache}.tmp"
2020-09-04 10:09:08 +00:00
conf_extra="$XDG_CONFIG_HOME/edconf-extra"
mkdir -p "$conf_cache_dir"
2020-06-26 21:25:53 +00:00
touch "$conf_cache"
# | xargs file | grep text | cut -d':' -f1 # this filters out non-text files, but it's ridiculously slow
2020-09-04 10:09:08 +00:00
sel="$({ cat "$conf_cache"; test -f "$conf_extra" && cat "$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 } | dedup | fzf -1 -0 --tiebreak=end,length --preview 'bat --color=always --style=numbers --line-range :200 {}' --query="$1" --history "$conf_cache_dir/searches")"
2020-06-26 21:25:53 +00:00
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"
2020-09-04 10:09:08 +00:00
edshell() {
case $1 in
"") file="$CONFIG_SHELL_FUNCTIONS";;
zsh) file="$CONFIG_ZSH/.zshrc";;
prof*) file="$HOME/.zprofile";;
*) file=$(find $CONFIG_SHELLS -name "$1*" | head -1);;
esac
checksum="$(md5sum $file)"
$EDITOR $file
2020-09-04 10:09:08 +00:00
test "$checksum" != "$(md5sum $file)" && source $HOME/.zprofile && exec $SHELL
}
edbin() {
file="$( (command which $1 || find $HOME/.local/bin -name "$1*" | grep . -m 1 || echo "$HOME/.local/bin/$1") | tail -1 )"
$EDITOR "$file"
yadm add "$file"
2020-06-03 10:39:13 +00:00
}
alias -g ___='"$(eval "$(fc -ln -1)" | tail -n 1)"'
alias -g G="| grp"
2020-09-04 10:09:08 +00:00
alias -g X="| xargs"
2020-03-08 19:18:49 +00:00
2020-09-04 10:09:08 +00:00
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
cd() { builtin cd "$@" && command ls --file-type --group-directories-first --color=always --format=vertical -w $COLUMNS | head -3 }
2020-03-08 19:18:49 +00:00
2020-09-04 10:09:08 +00:00
# Task management & time tracking
2020-06-03 10:39:13 +00:00
alias t='task'
alias tl='tasklite'
alias tw='timew'
2020-09-04 10:09:08 +00:00
twsm() { timew summary $(date --date="Monday $1 week ago" -I) to tomorrow :ids ${@:2} }
alias twtest='( TIMEWARRIORDB=/tmp/timewarriordb && rm -rf ${TIMEWARRIORDB} && mkdir -p ${TIMEWARRIORDB}/data && :> ${TIMEWARRIORDB}/timewarrior.cfg && $SHELL )'
# Quick shortcuts
2020-03-08 19:18:49 +00:00
alias c='clear'
alias q='exit'
alias v='nvim'
2020-09-04 10:09:08 +00:00
alias lst='( last; last -f /var/log/wtmp.1 ) | grep -v "pts/" | tac | less +G'
2020-06-26 21:25:53 +00:00
alias lar='last | tac'
2020-09-04 10:09:08 +00:00
tr() { tree -a -L 2 --du -h -C "$@" | ${PAGER:-less} }
alias xo='xdg-open'
alias sqli='rlwrap sqlite3 -column -header -cmd .tables'
alias loc='noglob locate --basename'
alias uloc='noglob sudo updatedb && locate --basename'
alias syslog='less +F /var/log/syslog'
alias println='printf "\n"'
alias graphics='lspci -vnn | grep VGA --color=never'
alias vlch="vlc -H | ${PAGER:-less}"
alias sc='sudo systemctl'
2020-09-04 10:09:08 +00:00
alias scrw='sudo systemctl restart display-manager'
alias drag='dragon -x -a'
2020-09-04 10:09:08 +00:00
alias grp='grep --line-number --ignore-case --binary-files=without-match'
alias grpr='grp --recursive'
# Use grep and sed to replace $1 with $2 recursively
rpl() {
grep --null --recursive --files-with-matches "$1" |
xargs -0 sed -i "/${1}/{
s//${2}/g
w /dev/stdout
}"
}
# Recover stray swap files from neovim
alias vrec="ls $XDG_DATA_HOME/nvim/swap | sed 's/\%/\//g' | sed 's|\(.*\)\..*|\1|' | head -1 | xargs -r nvim"
alias vrecd="ls $XDG_DATA_HOME/nvim/swap | head -1 | xargs -r rm"
2020-03-08 19:18:49 +00:00
2020-09-04 10:09:08 +00:00
# Applications
alias dict="(builtin cd $(dirname $(locate -n 2 /dict.txt)) && 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'
2020-03-08 19:18:49 +00:00
2020-06-03 10:39:13 +00:00
dropbox_path="$(cat ~/.dropbox/info.json | grep -Po '"'"path"'"\s*:\s*"\K([^"]*)')"
2020-03-08 19:18:49 +00:00
# Add an "alert" alias for long running commands. Use like so: sleep 10; alert
#if [ command -v notify-send >/dev/null 2>&1 ]; then
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(echo "$history[$HISTCMD]" | sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
#fi
# Fix broken mime messages
alias fixmime='sudo rm /usr/share/mime/packages/kde.xml && sudo update-mime-database /usr/share/mime'
# Find zmv in man because I need to look it up all the time ^^
man() {
(test "$1" = "zmv" && LESS="$LESS+/^ *zmv *\\[" /usr/bin/man zshcontrib) || /usr/bin/man "$@"
}
2020-06-03 10:39:13 +00:00
sedcomment() { sed -i 's/$1/#\0/' "${@:2}" }
seduncomment() { sed -i 's/#\($1\)/\0/' "${@:2}" }
mozedit() (
set -eo pipefail
file="$1"
tmpfile="/tmp/$(basename $1).tmp"
unsetopt multios # https://stackoverflow.com/a/58261480
# ( [[ -t 0 ]] && echo 'STDIN is attached to TTY' ) || cat
{ echo -en '\x02\x21\x4C\x18TT\x0\x0' && dd if="$file" bs=1 skip=12 } 2>&1 >"$tmpfile" | head -1 | cut -d+ -f1 | xargs printf "%04x\n" | read len
tmpfile2="${tmpfile}2"
cat "$tmpfile" | sed "s/TT/\\x${len:2:2}\\x${len:0:2}/" | unlz4 >"$tmpfile2"
cat "$tmpfile2" | sed "$2" | lz4 -l | dd bs=1 skip=8 seek=12 of="$file" conv=notrunc
)
theme_save="$XDG_CACHE_HOME/theme"
#theme_default="$(d=$(date +%H) && test $d -gt 7 && test $d -lt 19 && echo light || echo dark)"
theme_default="dark"
2020-06-03 10:39:13 +00:00
theme() {
theme="$1"
2020-06-26 21:25:53 +00:00
if test "$2" = "kde"
then kcmshell5 kcm_lookandfeel
elif test "$theme" != "$THEME" -a "$2" != "q"
2020-06-03 10:39:13 +00:00
then kcmshell5 kcm_lookandfeel &
fi
export THEME="$theme"
echo "$theme">"$theme_save"
#mozfile=$(find $HOME/.mozilla/firefox/ -maxdepth 2 -name addonStartup.json.lz4 | grep ".test")
case "$theme" in
(light)
export BAT_THEME="OneHalfLight"
2020-06-26 21:25:53 +00:00
konsoleprofile colors="Light"
sed -i 's/#\(include.*\/light-256.theme\)/\1/' "$XDG_CONFIG_HOME/taskrc"
2020-06-03 10:39:13 +00:00
#mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/'
;;
(dark)
export BAT_THEME="OneHalfDark"
2020-06-26 21:25:53 +00:00
konsoleprofile colors="Breeze"
sed -i 's/^include.*light-256.theme/#\0/' "$XDG_CONFIG_HOME/taskrc"
2020-06-03 10:39:13 +00:00
#mozedit $mozfile '/light@mozilla/,/"enabled"/ s/\("enabled":.*\)true/\1false/; /dark@mozilla/,/"enabled"/ s/\("enabled":.*\)false/\1true/'
;;
esac
}
export THEME=$(cat "$theme_save" 2> /dev/null)
export THEME=${THEME:-$theme_default}
theme $THEME
2020-03-08 19:18:49 +00:00
updateDeps() {
name="$1"
pattern="$2"
depth="4"
case $name in
gradle-wrapper.properties) depth=6;;
esac
shift 2
oldversion="[0-9.]\+"
while test $# -gt 1; do
case "$1" in
--pattern) oldversion="$2";;
--version) version="$2";;
esac
shift 2
done
echo name $name depth $depth pattern $oldversion version $version
find -maxdepth $depth -type f -name $name | while read f
do echo $f
sed -i "s/\($pattern\)$oldversion/\1$version/gw /dev/stdout" $f
done
}
2020-09-04 10:09:08 +00:00
alias updateKotlin="updateDeps build.gradle.kts 'kotlin(\"jvm\") version ' --version"
2020-03-08 19:18:49 +00:00
alias updateGradle='updateDeps gradle-wrapper.properties "services.gradle.org\/distributions\/gradle-" --version'
alias updateUtils='updateDeps build.gradle.kts '\''\"com.github.Xerus2000.util\", \".*\", \"'\'' --pattern '\''[^\"]\+'\'' --version'
2020-06-03 10:39:13 +00:00
# Kill all shell background processes
2020-03-08 19:18:49 +00:00
alias killbg='kill ${${(v)jobstates##*:*:}%=*}'
2020-06-03 10:39:13 +00:00
# Kil all Java processes except IDEA
# Pass "-9" to force-kill and "-q" to not output what has been killed
2020-03-08 19:18:49 +00:00
killJava() {
pgrep -f 'java' | while read id
do if [[ $(ps --no-headers -o cmd $id) != *"idea"* ]]; then
[[ "$@" == "-q" ]] || echo "killing $(ps -p $id -o pid,cmd --width 350 --no-headers)"
if [[ "$@" == "-9" ]]
then kill -9 $id
else kill $id
fi
fi; done
}
clearNpmCache() {
rm -rf $TMPDIR/react-*
rm -rf node_modules/
npm cache verify
npm install
}
# NAVIGATION & DISK
2020-09-04 10:09:08 +00:00
alias rcd="cd $PWD"
cdd() { cd "$@" 2>/dev/null || cd "$(dirname "$1")" "${@:2}" }
# Switch directory & ls
cl() {
builtin cd $1
ls --almost-all --group-directories-first --file-type
}
# Show type & contents of given files or PWD
b() {
arg=$(test $# -gt 0 && echo "${@: -1}" || echo .)
file $arg | grep -v --color=never directory
case "$(file --dereference $arg)" in
*directory) ls -l --almost-all --human-readable --group-directories-first --file-type --dereference-command-line "$@";;
2020-09-04 10:09:08 +00:00
*text*) bat --style=numbers "$@";;
esac
}
wh() { b $(which "$@"); }
# Go up a number of dirs
2020-03-08 19:18:49 +00:00
up() {
if [[ $# < 1 ]] ; then
cd ..
else
CDSTR=""
for i in {1..$1} ; do
CDSTR="../$CDSTR"
done
cd $CDSTR
fi
}
2020-09-04 10:09:08 +00:00
# shows size statistics for subfolders
2020-03-08 19:18:49 +00:00
s() {
2020-09-04 10:09:08 +00:00
case "$1" in [0-9]) local depth=$1; shift;; esac
sudo du --max-depth "${depth:-1}" -xhat 50M "$@" | sort -h | grep -v "^0"
2020-03-08 19:18:49 +00:00
}
2020-09-04 10:09:08 +00:00
# disk size usage information
alias sd='df -B1M -x tmpfs -x devtmpfs -x squashfs | awk -v a="\033[31m" -v b="\033[33m" -v c="\033[35m" -v n="\033[0m" '"'"'NR==1 {printf "%-20s %6s %7s %9s %s\n",$1,$5,$3,$4,$6} NR>1 {u=strtonum($5); printf (u > 99) ? a : (u > 97) ? b : (u > 94) ? c : ""; printf "%-20s %6s %6.1fG %8.1fG %s\n",$1,$5,$3/1000,$4/1000,$6; printf n}'"'"
2020-03-08 19:18:49 +00:00
alias f='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
alias f1='find -mindepth 1 -maxdepth 1'
# FILES
alias lowercase='rename "y/A-Z/a-z/"'
mkcd() {
mkdir -p $1 && cd $1
}
# Creates directory $2 and moves $1 into it
mvk() {
mkdir -p $(dirname "$2")
mv "$1" "$2"
}
2020-06-03 10:39:13 +00:00
# Moves from $1 to $2 and creates a symlink in place of $2
2020-03-08 19:18:49 +00:00
mvln() {
file=$(test -f "$1" && echo 1 || echo 0)
if test -d $1; then
mkdir -p "$2"
mv -T $1 $2
else
mv -v $1 $2
fi
[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
ln -vsr $2 $1
}
# OTHER
pathadd() {
local IFS=":"
local result="$@"
unset IFS
cat /etc/environment | head -1 | cut -d'"' -f2 | tr ":" "\n" | while read v; do
[[ " $@ " =~ " $v " ]] || result+=":$v"
done
echo PATH=\"${result}\"\\n$(cat /etc/environment | tail -n +2) | sudo tee /etc/environment
}
writecompletion() {
echo "#compdef $1" > "_$1"
2020-09-04 10:09:08 +00:00
$EDITOR "_$1"
2020-03-08 19:18:49 +00:00
}
resetdocker() {
#aptremove docker-ce
kill $(ps -e | grep docker | cut -d' ' -f2)
sudo rm -rf /var/run/docker /var/lib/docker
sudo rm /var/run/docker.*
#aptinstall docker-ce
}
# INSTALL
2020-09-04 10:09:08 +00:00
addalternative() {
sudo update-alternatives --install /usr/bin/$1 $1 "$(which "$1$2")" 1
}
2020-03-08 19:18:49 +00:00
snapinstall() {
sudo snap install "$@" && refreshSetup
2020-03-08 19:18:49 +00:00
}
alias aptupdate='sudo apt-get update'
alias aptremove='sudo apt-get remove'
alias aptpurge='sudo apt-get purge'
2020-09-04 10:09:08 +00:00
alias aptclean='sudo apt-get clean && sudo apt-get autoremove'
2020-03-08 19:18:49 +00:00
upgrade() {
if test $DIST = "neon"
then sudo pkcon update
else sudo apt update && sudo apt upgrade
fi
sudo snap refresh
flatpak update -y
sudo npm update -g
# npm -g outdated --parseable=true | cut -d : -f 4 | xargs -n 1 npm -g install
}
uma() {
if test "$1"
then printf 'y\nj\na' | umake "$1" "$2" "/opt/${3:-$2}"
else
uma ide idea-ultimate idea
uma android android-studio android/android-studio && yes | /opt/android/sdk/tools/bin/sdkmanager --licenses && pathadd /opt/android/sdk/tools/bin /opt/android/sdk/platform-tools && sudo apt install qemu-kvm && sudo adduser janek kvm
test -d "/opt/vscode" && uma ide visual-studio-code vscode
fi
}
alias aptrepo='sudo add-apt-repository'
_aptrepo_n="$(test "$OSTYPE" != "cygwin" && add-apt-repository --help | grep -- "-n")"
aptrepon() {
test $_aptrepo_n && aptrepo -y -n "$@" || aptrepo -y "$@"
}
aptrepos() {
for repo in "$@"
do aptrepon $repo
done
aptupdate
}
alias aptinstall='sudo apt-get install'
2020-06-26 21:25:53 +00:00
aptinstalls() {
apt-cache show "$1" | grep --color=never '\(Version\|Description\):'
aptinstall "$@"
}
2020-03-08 19:18:49 +00:00
aptrepoinstall() {
aptrepo -yu $1
if (( $# > 1 ))
then aptinstall "${@:2}"
else aptinstall "${1##*/}"
fi
}
# Gets the download url for the latest release of a package provided via GitHub Releases
# Usage: ghrelease USER REPO [PATTERN]
ghrelease() {
2020-06-03 10:39:13 +00:00
result=$(curl -s "https://api.github.com/repos/$1/$2/releases/latest" | grep -o "http.*${3:-deb}" | awk '{ print length(), $0}' | sort -n | cut -d' ' -f2-)
echo "$result" | grep amd64 || echo "$result"
2020-03-08 19:18:49 +00:00
}
# Installs a local or remote(http/https) deb package and removes it after installation
installdeb() {
loc="/tmp/install.deb"
2020-06-03 10:39:13 +00:00
case $1 in
2020-09-04 10:09:08 +00:00
http*) wget -O "$loc" $1;;
*) loc="$1";;
2020-03-08 19:18:49 +00:00
esac
2020-09-04 10:09:08 +00:00
sudo dpkg -i "$loc" "${@:2}" && sudo apt -f install && rm "$loc"
2020-03-08 19:18:49 +00:00
}
installgh() {
installdeb $(ghrelease "$@")
}
# Downloads all dependencies of a package into the current directory
download-debs() {
apt-get download $1 && apt-cache depends -i $1 | awk '/Depends:/ {print $2}' | xargs apt-get download
}
# RANDOM
typeset -A _clean_map
if test "$BASH_VERSION"
2020-06-26 21:25:53 +00:00
then _clean_map=([c]=$XDG_CACHE_HOME [t]=/var/tmp)
else _clean_map=(c $XDG_CACHE_HOME t /var/tmp)
2020-03-08 19:18:49 +00:00
fi
2020-06-26 21:25:53 +00:00
_clean_folders=()
2020-03-08 19:18:49 +00:00
clean() {
for f in $_clean_folders
do test -d $f && rm -rv $f
done
2020-06-03 10:39:13 +00:00
2020-03-08 19:18:49 +00:00
highlight "g to clean gradle"
if [[ $1 =~ "g" ]]; then
2020-09-04 10:09:08 +00:00
find ${GRADLE_USER_HOME:-$HOME/.gradle} -mindepth 1 -maxdepth 1 -type d -exec rm -r {} \; -print
2020-06-26 21:25:53 +00:00
find $projects_dir -name .gradle -print -exec rm -r {} \;
2020-03-08 19:18:49 +00:00
fi
2020-06-03 10:39:13 +00:00
2020-09-04 10:09:08 +00:00
highlight "m to clean mac-files"
if [[ $1 =~ "m" ]]; then
find -iname '.spotlight*' -print -exec rm -r {} \;
find -name "*.DS_Store" -delete
fi
2020-03-08 19:18:49 +00:00
if test "$BASH_VERSION"; then
for k in "${!_clean_map[@]}"; do
2020-09-04 10:09:08 +00:00
highlight "$k to delete $_clean_map[$k]"
2020-03-08 19:18:49 +00:00
echo "key : $k"
echo "value: ${_clean_map[$k]}"
done
else
for k in "${(@k)_clean_map}"; do
2020-09-04 10:09:08 +00:00
highlight "$k to delete $_clean_map[$k]"
2020-03-08 19:18:49 +00:00
[[ $1 =~ "$k" ]] && rm -rfv "$_clean_map[$k]"
done
fi
2020-09-04 10:09:08 +00:00
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)
test "$aptclean_cur" -gt "$aptclean_last" && aptclean && echo "Cleaned apt"
export aptclean_last=$aptclean_cur
2020-03-08 19:18:49 +00:00
highlight "e to delete empty folders"
[[ $1 =~ "e" ]] && find -empty -type d -delete -print
}
2020-06-03 10:39:13 +00:00
function zipdiff() {
2020-03-08 19:18:49 +00:00
diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8) #--suppress-common-lines
}
# PROJECTS
2020-06-03 10:39:13 +00:00
pdiff() {
diff -r $1 $2 -x .git -x .idea -x .gradle
}
2020-03-08 19:18:49 +00:00
genssh() {
mkdir -p ~/.ssh
test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -q -N "" -C ${1:-$(hostname)}
if test "$OSTYPE" = "cygwin"
then cat ~/.ssh/id_rsa.pub>/dev/clipboard
2020-06-03 10:39:13 +00:00
else xclip -sel clip < ~/.ssh/id_rsa.pub
fi && echo "SSH Public key copied to clipboard"
2020-03-08 19:18:49 +00:00
}
createproject() {
mkcd $1
2020-06-03 10:39:13 +00:00
2020-03-08 19:18:49 +00:00
echo 'plugins {
kotlin("jvm") version "1.3.41"
application
id("com.github.johnrengelman.shadow") version "5.1.0"
id("com.github.ben-manes.versions") version "0.21.0"
}
2020-06-03 10:39:13 +00:00
2020-03-08 19:18:49 +00:00
sourceSets {
main {
java.srcDir("src/main")
resources.srcDir("src/resources")
}
test {
java.srcDir("src/test")
}
}' > build.gradle.kts
2020-06-03 10:39:13 +00:00
2020-03-08 19:18:49 +00:00
}
# SWAP
2020-09-04 10:09:08 +00:00
alias memstat='free -h | awk '"'"'NR==2 {printf "Free memory:\t %s/%s\t(%d%)\n",$7,$2,$7*100/$2} NR==3 {if($2 != "0B") printf "Used swap:\t%s/%s\t(%d%)\n",$3,$2,$2*100/$3}'"'"
2020-03-08 19:18:49 +00:00
stopswap() {
2020-09-04 10:09:08 +00:00
memstat
swap_used=$(cat /proc/meminfo | grep SwapFree | awk '{print $2}')
mem_available=$(cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
if test $swap_used = 0
2020-03-08 19:18:49 +00:00
then echo "No swap is in use."
2020-09-04 10:09:08 +00:00
elif test $swap_used + 100000 < $mem_available
2020-03-08 19:18:49 +00:00
then echo "Freeing swap..."
sudo swapoff -a
sudo swapon -a
2020-09-04 10:09:08 +00:00
memstat
2020-03-08 19:18:49 +00:00
else
echo "Not enough free memory!"
fi
}