sync/shells/zshrc

635 lines
19 KiB
Bash

# Borg backup repository
export BORG_REPO="kserver:borg"
#zmodload zsh/zprof
echo -ne '[ ]\r'
for config (~/.zsh/*.zsh) source $config
autoload -U compinit
compinit
setopt always_to_end # When completing from the middle of a word, move the cursor to the end of the word
setopt complete_in_word # Allow completion from within a word/phrase
setopt INTERACTIVECOMMENTS
setopt EXTENDED_GLOB
setopt HIST_EXPIRE_DUPS_FIRST
export HIST_STAMPS="dd.mm.yyyy"
export HISTSIZE=12000
export SAVEHIST=10000000
export HISTFILE=$HOME/.zsh_history
setopt APPEND_HISTORY
setopt EXTENDED_HISTORY
setopt APPEND_HISTORY SHARE_HISTORY HISTIGNOREALLDUPS
export DISABLE_AUTO_TITLE=true
export EDITOR='vim'
export FZF_COMPLETION_OPTS='--no-mouse -m -1 -x'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.cache/zsh
# Check if a command exists
compress(){
INFILE="${1:r}.tpxz"
if [ -f "$INFILE" ]; then
echo "Won't replace $1" >&2
return 1
fi
tar --xattrs -I pixz -cpf "$INFILE" $*
}
ex(){
command -v "$1" >/dev/null
return $?
}
# For pasting in commands that start with $
$ () {
$*
}
# A service implementation
sv () {
command="${@: -1}"
while (( $# > 1 )); do
if [[ "$command" == "i" ]]; then
echo "Status of $1"
service "$1" status
shift
continue
fi
sudo true || return 2
if [[ "$1" == "sshd" ]] || [[ "$1" == "ssh" ]]; then
echo "Checking sshd config..."
sudo sshd -t && echo "Config good. Continuing" || (echo "Aborting due to bad ssh config";return 1)
elif [[ "$command" == "r" ]]; then
echo "Restarting $1"
sudo service "$1" restart
elif [[ "$command" == "s" ]]; then
echo "Starting $1"
sudo service "$1" start
elif [[ "$command" == "x" ]]; then
echo "Stopping $1"
sudo service "$1" stop
else
sudo service "$1" "$command"
fi
shift
done
}
compdef _services sv
# Run gui application as root by copying xauth credentials
asroot(){
# Use temporary file for added security
TEMPFILE=$(tempfile)
xauth extract $TEMPFILE $DISPLAY
sudo zsh -c "xauth merge $TEMPFILE;(srm $TEMPFILE||rm $TEMPFILE)&'$@';"
}
# De-symlink
dsl() {
while (( $# > 0 )); do
readlink $1 2>&1 >/dev/null && mv $(readlink $1) $1
shift
done
}
# Remove directory by removing subdirectories up a level
dedir(){
# Try removing the directory if it's empty, fail silently if you can't
=rmdir "$1" 2>/dev/null && return
# Make a uuid to avoid name conflicts
# Ex: dediring sage when sage/sage fails because sage/sage is moved to .
# but since sage/ exists, it can't be moved
name=$(uuidgen)
# Move the directory to the uuid
=mv -n "$1" "$name" || return 1
# Move all files, use (N) in case there is no * or .* matches
=mv -n "$name"/{.,}*(N) . || return 2
=rmdir "$name" || return 3
}
mt(){
if [[ -z "$1" ]]; then
echo "Opts:\nm xe gitea dm syncthing g" >&2
return 1
fi
case $1 in
m) sudo multitail /var/log/messages;;
xe) multitail ~/.xsession-errors;;
ef) sudo multitail /var/log/emerge-fetch.log;;
gitea) sudo multitail /var/log/gitea.{err,log};;
dm) dmesg -w;;
syncthing) sudo multitail /var/log/syncthing.{err,log};;
g) sudo watch -c genlop -tc;;
sync) watch grep -e Dirty: -e Writeback: /proc/meminfo;;
*) multitail "$1"
esac
}
run(){
test -f "${1:r}" && rm "${1:r}"
echo "Building..." >&2
gcc -Wall -g -std=c99 "${1:r}.c" -o "${1:r}" && (echo "Running..." >&2;./"${1:r}")
}
upload() {
tar -cf - "$1" | base64 | curl --data-urlencode text@- -d title="File uploaded at $(date '+%d/%h/%y %H:%M:%S') on $(hostname)" -d name=$USER -d expire="1440" https://austenwares.com/paste/api/create | sed -e 's/view/view\/raw/'
}
download() {
URL="$(printf $1 | perl -pe 's/\/paste\/view(?!\/raw)/\/paste\/view\/raw/')"
curl "$URL" | base64 -d | tar -xf -
}
update-tool() {
if [ "$EUID" -ne 0 ]; then
echo "This function should be run as root"
return 1
fi
local RESP=""
while [[ "$RESP" != "q" ]]; do
CMD=${RESP:0:1}
case "$CMD" in
u) update -q;;
l) layman -S ;;
g) upgrade;;
e) etc-update;;
p) emerge -Av @preserved-rebuild --usepkg=n --keep-going=y;;
m) emerge -Av @module-rebuild --usepkg=n;;
d) emerge -Ava --depclean;;
h) haskell-updater -- --usepkg=n;;
c) perl-cleaner --reallyall;;
y) python-updater;;
r) revdep-rebuild;;
q) return;;
esac
RESP=${RESP:1}
if [[ ! -z "$RESP" ]]; then
continue
fi
echo "Commands:"
echo "u: update"
echo "l: layman -S"
echo "g: upgrade"
echo "e: etc-update"
echo "p: emerge @preserved-rebuild --usepkg=n --keep-going=y"
echo "m: emerge @module-rebuild --usepkg=n"
echo "d: emerge --ask --depclean"
echo "h: haskell-updater -- --usepkg=n"
echo "c: perl-cleaner --reallyall"
echo "y: python-updater"
echo "r: revdep-rebuild"
echo "q: quit"
echo -n '\a> '
read RESP
done
}
alias cleaner="sudo ~/run.sh 'perl-cleaner --reallyall' 'python-updater' 'haskell-updater -- --usepkg=n'"
alias jsonpretty='python -m json.tool'
alias nmrestart='nmcli radio wifi off;nmcli radio wifi on'
if ex mtr; then
alias mtr='mtr -t'
fi
if ex telegram-cli; then
alias tg='telegram-cli -NWA --disable-link-preview'
fi
alias perm='stat -c "%a %n"'
alias afci='git x ./autoformat.sh;git commit -am "Autoformatted"'
if ex eix; then
alias eix="eix -F"
fi
alias mouse1="xmodmap <(echo pointer = 1 2 3)"
alias mouse2="xmodmap <(echo pointer = 3 2 1)"
if ex ocp; then
alias ocp="ocp -vs0 -dcurses"
fi
alias con='git x php bin/console'
alias srm='shred -uzv'
alias disphost='export DISPLAY=:0.0'
alias ll='ls -AlhF'
alias la='ls -A'
alias l='ls -CF'
alias logout='sudo pkill -u $USER'
if ex tmux; then
alias tmux='tmux -2'
fi
alias less='less -R'
if ex tree; then
alias tree='tree -C'
fi
alias resource='. ~/.zshrc'
if [ "$(uname)" != "Darwin" ]; then
# Not Mac
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$HOME/bin"
alias ls='ls --color=always -F'
alias where="readlink -m"
else
# It is a mac
# Prepend macports directories in /opt/local
if ex greadlink; then
alias readlink='greadlink'
alias where="greadlink -m"
fi
export PATH="/opt/local/sbin:/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$HOME/bin"
fi
if test -f /etc/gentoo-release; then
# Gentoo
alias etup='sudo etc-update'
ins(){
# Sudo echo so we have immediate results on weather sudo worked
sudo echo "Args: $*"
# Beep, verbose, ask
sudo emerge --autounmask-write -Ava $*
echo "\a"
}
upgrade(){
# Sudo echo so we have immediate results on weather sudo worked
sudo echo "Args: $*"
sudo emerge --update --newuse --deep --verbose --tree --keep-going=y --verbose-conflicts --alert --ask --binpkg-respect-use=y --binpkg-changed-deps=y --backtrack=30 $* @world
echo "\\a"
}
# For emerging with absolute paths
em(){
# Beep, verbose, ask
EMERGE_ARGS="-Ava"
X=""
for var in "$@"
do
if [[ "$var" =~ ^- ]]; then
EMERGE_ARGS="$EMERGE_ARGS $var"
else
X="$X =$var"
fi
done
echo sudo emerge "$EMERGE_ARGS""$X"
sudo emerge "$EMERGE_ARGS""$X"
echo "\a"
}
update(){
# If this is a server computer
if [ "$(hostname)" = "SGen" ] ; then
echo "You're a server. Exiting"
return
fi
sudo rm -f '/usr/portage/metadata/timestamp.chk'
if ex eix-update; then
sudo zsh -c "emaint sync -a&&eix-update"
else
sudo emaint sync -a
fi
sudo -k
if [ "$1" != "-q" ]; then
echo '\a'
fi
}
elif test -f /etc/lsb-release; then
# Ubuntu
alias install='sudo apt-get --show-progress install'
alias remove='sudo apt-get --show-progress remove'
alias update='sudo apt-get update'
alias upgrade='sudo apt-get --show-progress upgrade'
alias autoremove='sudo apt-get --show-progress autoremove'
alias updateall='sudo sh -c "apt-get update;apt-get upgrade -y;apt-get dist-upgrade -y;apt-get autoremove -y"'
alias search='apt-cache search'
fi
alias ..='k ..'
alias ...='k ../..'
alias ....='k ../../..'
alias .....='k ../../../..'
alias ......='k ../../../../..'
alias .......='k ../../../../../..'
alias ........='k ../../../../../../..'
alias .........='k ../../../../../../../..'
alias ..........='k ../../../../../../../../..'
alias ...........='k ../../../../../../../../../..'
alias ............='k ../../../../../../../../../../..'
alias .............='k ../../../../../../../../../../../..'
alias ..............='k ../../../../../../../../../../../../..'
alias ...............='k ../../../../../../../../../../../../../..'
if ex youtube-dl; then
alias ytdl="youtube-dl $@ -x --audio-format mp3 --audio-quality 0 -c -o '%(title)s.%(ext)s'"
fi
if ex wemux; then
alias tux="wemux"
elif ex tmux; then
alias tux="tmux a || tmux"
fi
if ex git; then
alias g="git"
fi
alias stdns="sudo sh -c 'echo nameserver 8.8.8.8 > /etc/resolv.conf'"
alias fvim="vim -u NONE +\"so ~/.vim/plugged/flappyvird-vim/plugin/flappyvird.vim\" +\"so ~/.vim/plugged/flappyvird-vim/autoload/flappyvird.vim\" +\":FlappyVird\" +\":q\""
alias fk='k `fzf --no-mouse -m -1 -x`'
#alias susp="sudo true&&((slock;sleep 2;nmcli radio wifi off;nmcli radio wifi on)&echo mem | sudo tee /sys/power/state >/dev/null);sudo -k"
alias susp="sudo true && (slock&until echo mem|sudo tee /sys/power/state>/dev/null;do sleep 1;done;sleep 4;nmcli radio wifi off;sleep 3;nmcli radio wifi on)"
if ex sudo; then
alias s='sudo -Hu'
fi
alias -g PA='2>&1 | paste'
if ex rsync; then
alias -g c='rsync --partial -ha --info=progress2 "$@"'
alias -g cfat='rsync --partial -hrlc --info=progress2 "$@"'
alias cp='rsync --partial -ha --info=progress2 "$@"'
fi
alias -g nify=" > /dev/null 2>&1 &"
alias -g nifyd=" > /dev/null 2>&1 & disown"
alias -g L="2>&1|less"
if ex rg; then
alias -g G='|& rg'
# I'm so sorry ggreer
alias ag=rg
elif ex ag; then
alias -g G='|& ag'
elif ex egrep; then
alias -g G='|& egrep'
elif ex grep; then
alias -g G='|& grep'
fi
timer() {
for i in {1.."$1"}; do
sleep 1
echo
done | pv -Sptels "$1" >/dev/null
}
fixsh(){
sed -i -e $1"d" ~/.ssh/known_hosts
}
mv() { /bin/mv -v "$@" }
m() { mv "$@" }
compdef _cp c cfat m mv cp
paste() {
NAME="-d title=Command run at $(date '+%d/%h/%y %H:%M:%S') on $(hostname)"
if [[ ! -z "$1" ]]; then
NAME="-d title=$1"
fi
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" | curl --data-urlencode text@- $NAME -d name=$USER -d expire="40320" https://austenwares.com/paste/api/create
}
grename() {
echo incomplete
return
FILES=($*)
REGEX='s///'
echo -n "Command (frneVVVq): "
while read input; do
case $input in
f)
echo "File list:"
printf '%s\n' $FILES
;;
r)
echo "Regex:"
echo "$REGEX"
;;
n)
echo "Simulated replacement:"
perl-rename -n "$REGEX" $FILES
;;
e)
echo "Editing regex..."
TEMPFILE=$(tempfile)
(cat <(printf '%s\n' $FILES);echo;echo "$REGEX") > $TEMPFILE
vim $TEMPFILE +'normal G$h'
REGEX=$(tac $TEMPFILE|grep -m1 '.')
rm $TEMPFILE
;;
VVV)
echo "Actual results:"
perl-rename -v $REGEX $FILES
return
;;
q)
return
;;
esac
echo -n "Command (frneVVVq): "
done
}
bp(){
echo '\a'
}
apush(){
adb push $* & pv -d $!
}
ltar(){
tar -cf - $@|pv -WcN tar|xz|pv -WcN lzma
#tar -cf - $@|pv -WcN tar -s $(echo $(du -sb $@|awk '{print $1}'|tr '\n' '+')0|bc)|gzip|pv -WcN dest
#tar -cf - $@|pv -WcN tar -s $(echo $(du -sb $@|awk '{print $1}'|tr '\n' '+')0|bc)|gzip|pv -WcN dest
}
ctar(){
tar -cf - $@|pv -WcN tar|gzip|pv -WcN gzip
}
offline(){
if [ ! -z "$@" ] ; then
sudo unshare -n -- sudo -u $USER zsh -c "$@"
else
sudo unshare -n -- sudo -u $USER LP_MARK_PREFIX=" $(tput setaf 1)(offline)$(tput sgr0) " zsh
fi
}
dnstest(){
echo "resolv.conf:"
cat /etc/resolv.conf
echo "Testing DNS:"
dig google.com
echo "Testing ping:"
ping -c 2 8.8.8.8
echo "Testing IP curl:"
curl -L 178.33.250.62
echo
echo "Testing DNS"
curl -L portquiz.net
}
tsh(){
ssh -X $*
if which ponysay >/dev/null; then
ponysay "Connection closed!"
else
echo "\n\n\n\n\n\n\n\n\n\nSESSION CLOSED\n\n\n\n\n\n\n\n\n\n"
fi
}
compdefas () {
local a
a="$1"
shift
compdef "$_comps[$a]" "${(@)*}=$a"
}
compdefas ssh tsh
compdefas mv m mv
mcl(){
mkdir -p $1
cd $1
}
create-repo(){
PRIVATE="true"
while getopts ":p" opt; do
case $opt in
p)
echo "-p was used, making public"
PRIVATE="false"
shift
;;
esac
done
if [ -z "$1" ]; then
echo "No repo name" >&2
return 2
fi
echo -n "Username: "
read username
echo -n "Password: "
read -s password
curl --user "$username:$password" -d name="$1" -d private="$PRIVATE" https://austenwares.com/gitea/api/v1/user/repos
}
hsh(){
FILE="$1"
ARGS=""
while (( $# > 0 )); do
if [[ "md5" = "$1" ]] || [[ "md5sum" = "$1" ]] || [[ "m" = "$1" ]]; then
ARGS="$ARGS "md5sum
elif [[ "sha1" = "$1" ]] || [[ "sha1sum" = "$1" ]] || [[ "s" = "$1" ]]; then
ARGS="$ARGS "sha1sum
elif [[ "sha2" = "$1" ]] || [[ "sha256sum" = "$1" ]]; then
ARGS="$ARGS "sha256sum
elif [[ "sha512" = "$1" ]] || [[ "sha512sum" = "$1" ]]; then
ARGS="$ARGS "sha512sum
fi
shift
done
ARGS=$(echo "$ARGS"|xargs)
echo "Calculating ($ARGS) of \"$FILE\""
echo pv "$FILE" \| pee $ARGS
pv "$FILE" | pee $(echo $ARGS)
}
enc() {
test -f README.md || echo "# enc\n## files" > README.md
while (( $# > 0 )) ; do
fn="$1"
uuid="$(uuidgen)"
shift
echo "Encrypting ($fn)..."
pv "$fn" | gpg -r stonewareslord -o "$uuid" -e - || (echo "Error encrypting $fn" >&2 ; continue)
echo "- \`$uuid\`: \`$fn\`" >> README.md || (echo "Error appending $fn to README.md" >&2 ; continue)
echo "Calculating sum of ($fn) in the background"
pv "$uuid" | sha512sum | perl -pe "s/-/$uuid/" >> SHA512SUMS || (echo "Error calculating sum of $uuid" >&2 ; continue)
rm "$fn" || (echo "Error removing $fn"; continue)
done
}
rc(){
case $1 in
z) vim ~/.zshrc;;
v) vim ~/.vimrc;;
i) vim ~/.i3/config;;
use) sudo vim /etc/portage/package.use/package.use;;
make) sudo vim /etc/portage/make.conf;;
unmask) sudo vim /etc/portage/package.unmask/package.unmask;;
mask) sudo vim /etc/portage/package.mask/package.mask;;
accept_keywords) sudo vim /etc/portage/package.accept_keywords/package.accept_keywords;;
keywords) sudo vim /etc/portage/package.keywords/package.keywords;;
license) sudo vim /etc/portage/package.license/package.license;;
*) echo "Opts:\nz v i\nuse make unmask mask accept_keywords keywords license"
esac
}
pub-git-init(){
# Get the project name
branch=$(git remote -v | ag '^origin\s+' | sed -e 's/^origin\s*//' | cut -d\ -f1 | sed -r -e 's/^.+\///' -e 's/#.+$//' -e 's/\.git$//' | head -n1)
echo "Branch: $branch"
# Get the hashed project name
enc_branch=$(echo "$branch" | sha512sum | cut -d\ -f1)
git remote add local "gcrypt::git@localhost:stonewareslord/79a79b1a6e37813d9226872c6428aca29eec9f6111558109861cb0dcd2f0ffc8198deea4e361f84e170989212452fdfb21e0ef690da2ad399c03ce308d79dcfe#$enc_branch"
git remote add ncsu "gcrypt::git@github.ncsu.edu:agadler/a86a0f8757772be2ec617b395a0716679bfce51c1bfdeb8da6127feebd84facde645e9e30188318344eb458f5834e3d86800.git#$enc_branch"
git remote add gitlab "gcrypt::git@gitlab.com:stonewareslord/e6e03ea006d55de0970a28bcb7fcf65f4c66f98f50830bd69b50c5dc502bdf1a4e4172187cfb5fcef8c32bd7fb316bdc67d7d86713ebfe232f97eb303ac316ae.git#$enc_branch"
git remote add aws "gcrypt::git@aws:stonewareslord/5e0701d5b5b2118127d378c084be9a1dfb33d185d3e2c5da30a3984f5118e3d5136f3f3b038462bccd538e93f9e32cb06136.git#$enc_branch"
git config remote.ncsu.gcrypt-participants "CCDFE3F1"
git config remote.gitlab.gcrypt-participants "CCDFE3F1"
}
pub-git-rm(){
if (( $# == 0 )) ; then
git remote rm local
git remote rm ncsu
git remote rm gitlab
git remote rm aws
fi
while (( $# > 0 )) ; do
git remote rm "$1"
shift
done
}
pub-git-push(){
if (( $# == 0 )) ; then
git push local --all && git push local --tags
git push ncsu --all && git push ncsu --tags
git push gitlab --all && git push gitlab --tags
git push aws --all && git push aws --tags
fi
while (( $# > 0 )) ; do
git push "$1" --all && git push "$1" --tags
shift
done
}
pub-git-clone(){
case $1 in
ncsu) remote="gcrypt::git@github.ncsu.edu:agadler/a86a0f8757772be2ec617b395a0716679bfce51c1bfdeb8da6127feebd84facde645e9e30188318344eb458f5834e3d86800.git#" ;;
gitlab) remote="gcrypt::git@gitlab.com:stonewareslord/e6e03ea006d55de0970a28bcb7fcf65f4c66f98f50830bd69b50c5dc502bdf1a4e4172187cfb5fcef8c32bd7fb316bdc67d7d86713ebfe232f97eb303ac316ae.git#" ;;
local) remote="gcrypt::git@localhost:stonewareslord/79a79b1a6e37813d9226872c6428aca29eec9f6111558109861cb0dcd2f0ffc8198deea4e361f84e170989212452fdfb21e0ef690da2ad399c03ce308d79dcfe#" ;;
aws) remote="gcrypt::git@aws:stonewareslord/5e0701d5b5b2118127d378c084be9a1dfb33d185d3e2c5da30a3984f5118e3d5136f3f3b038462bccd538e93f9e32cb06136.git#" ;;
*) echo "Opts: local ncsu gitlab" ; return 1
esac
shift
if [ -z "$1" ] ; then
echo "No arg2"
return 2
fi
branch=$(echo $1 | sha512sum | cut -d\ -f1)
git clone "$remote$branch" $1
}
k(){
#More than 1 arguement
if [[ $# > 1 ]] ; then
vim $@
elif [ -d "$@" ] ; then
#cd then ls
cd "$@" && ls
elif [[ "$@" == "-" ]] ; then
#Because if this isn't here, k - won't work
cd - && ls
elif [ -f "$@" ] ; then
#Extract if it's extractable
case $1 in
*.tar.xz) pv $1|tar -xJf - ;;
*.txz) pv $1|tar -xJf - ;;
*.tar.bz2) pv $1|tar -xjf - ;;
*.tar.gz) pv $1|tar -xzf - ;;
*.tar) pv $1|tar -xf - ;;
*.tbz) pv $1|tar -xjf - ;;
*.tbz2) pv $1|tar -xjf - ;;
*.tgz) pv $1|tar -xzf - ;;
*.lzma) pv $1|tar --lzma -xf - ;;
*.xz) pv $1|tar -xJf - ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.lrz) lrzuntar $1 ;;
*) vim $1
esac
else
#Edit with vim
echo -n "vim? "
read a
vim "$@"
fi
}
if (( $+TMUX )) || [[ "$TERM" = "screen-256color" ]] || [[ "$TERM" = "screen.xterm-256color" ]]; then
unset zle_bracketed_paste
fi
#(( $+TMUX )) && unset zle_bracketed_paste
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
if ex fasd; then
eval "$(fasd --init auto)"
fi
#Old config options that are no longer in use
#alias pg="pcregrep -M"
#if command -v greadlink 2>&1 >/dev/null; then
# alias readlink='greadlink'
#fi
#alias steamo='sudo kill -9 `pidof steam`;sudo unshare -n -- sh -c "ifconfig lo up;sudo -u $USER steam" > /dev/null 2>&1 & disown'
#alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"'
#alias aoeu='setxkbmap -layout us -option "'
#alias asdf='setxkbmap -layout dvorak -option ""'
#alias sudo='sudo -H'
echo -ne '[# ]\r'
source ~/.zsh-git/lpr/liquidprompt
source ~/.zsh-git/omg/base.sh
source ~/.zsh-git/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source ~/.zsh-git/oh-my-git-themes/af-magic.zsh-theme
echo -e '[##]'
#zprof