dotfiles/.bash_extra
2022-09-01 12:59:39 +02:00

272 lines
7.3 KiB
Bash

#!/bin/bash
### tmux auto attach
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then
tmux attach -t default || tmux new -s default
fi
### keychain
eval $(keychain -q --eval --timeout 60 --agents ssh)
### PS1
# git-symbole: * unstaged, + staged, $ stashed, % untracked
export GIT_PS1_SHOWUNTRACKEDFILES=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export PS1='\n\[\e[34m\]\u@\[\e[34m\]\h \[\e[94m\]\w\[\e[33m\]$(__git_ps1) \[\e[94m\]\n> \[\e[39m\]'
#\e[34m \u @ \e[34m \h \e[37m \e[94m \w \e[33m $(__git_ps1) \e[94m \n> \e[39m
#Blau User @ Bue Hostname Light Gray LightBlue Verzeichnis Yellow Git-Script LightBlue Zeilenumbruch Standard-Vordergrund-Farbe
# https://unix.stackexchange.com/questions/105958/terminal-prompt-not-wrapping-correctly
# alle Farben sind mit \[<wert>\] escaped, sonst kann die shelle die länge nicht richtig ausrechnen
# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
# https://gist.github.com/justintv/168835
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
### Praktische Dinge
# Deaktiviere FlowControl
# https://unix.stackexchange.com/questions/332791/how-to-permanently-disable-ctrl-s-in-terminal
stty -ixon
# farben fur manpages
# https://opensource.com/article/18/5/bash-tricks
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;33m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;42;30m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;36m'
# bash history bei mehreren Session
shopt -s histappend
# bach History Zeitstempel am Anfang der Zeile
export HISTTIMEFORMAT='[%F_%T] '
# typo bei cd entfernen
shopt -s cdspell
# less mit maus scrollen
export LESS='--mouse --wheel-lines=3 eFRX'
# deaktiviere alias von ll
# weiter unten ist die FUnktion ll definiert die entweder exa oder ls ausführt
unalias ll 2>/dev/null
### Funktionen
function sgc {
case "$1" in
--help)
echo "Suche in der kompletten git-history nach "\$1""
echo ""
echo " Usage: sgc <Suchbegriff>"
;;
"")
echo "[ERROR] Suchbegriff fehlt..."
;;
*)
git grep "$1" $(git rev-list --all)
esac
}
function m2m {
if [[ "$1" == "--help" ]]; then
echo "Sende Datei "\$2" per Mail; Betreff = "\$1""
echo ""
echo " Usage: m2m <Pfad zu Datei> <Betreff>"
else
if [ ! -f "$2" ]; then
echo "[ERROR] Dateipfad fehlt..."
else
if [ -z "$1" ]; then
echo "[ERROR] Betreff fehlt..."
else
echo "$1" | mail -s "$1" -A "$2" michael.grote@posteo.de
fi
fi
fi
}
#function psk {
# if [[ "$1" == "--help" ]]; then
# echo "Beende Prozess "\$1""
# echo ""
# echo " Usage: psk <prozessname>"
# else
# if [ -z "$1" ]; then
# echo "[ERROR] Prozessname fehlt..."
# else
# ps -ef | grep "$1" | grep -v grep | awk '{ print $2 }' | xargs kill -9
# fi
# fi
#}
function pwgen {
if [ -n "$1" ]; then
# Command ist notwendig da die Funktion genau wie das Binary heißt
# https://unix.stackexchange.com/questions/454553/bash-shadow-a-command-function-with-same-name-as-command
command pwgen --capitalize --num-passwords=1 --numerals --secure $1
fi
if [ -z "$1" ]; then
command pwgen
fi
}
function rgf {
if [[ "$1" == "--help" ]]; then
echo "Finde alle Dateien die "\$1" im Namen haben."
echo ""
echo " Usage: rgf <string>"
return 0
fi
if [ -z "$1" ]; then
echo "[ERROR] Suchbegriff fehlt..."
return 1
fi
if command -v rg >/dev/null ; then
rg --files -g *"$1"* --hidden
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'find'"
find . -type f -name *"$1"*
fi
}
function rgi {
if [[ "$1" == "--help" ]]; then
echo "Finde alle Dateien die "\$1" im Inhalt haben."
echo ""
echo " Usage: rgi <string>"
return 0
fi
if [ -z "$1" ]; then
echo "[ERROR] Suchbegriff fehlt..."
return 1
fi
if command -v rg >/dev/null ; then
rg -i "$1"
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'grep'"
grep -r -n -i "$1" .
fi
}
function ssh {
# benennt das aktuelle tmux-windows nach dem ssh-ziel
# baut ssh-verbindung auf
if [ -n "${TMUX}" ]; then
tmux rename-window ""${@: -1}""
fi
command ssh "$@"
if [ -n "${TMUX}" ]; then
tmux rename-window "`hostname -f`"
fi
}
function gcm {
if [ -z "$1" ]; then
echo "Commit-Message fehlt, setze Default-Message..."
git commit -m "changed Files: $(git diff --name-only --staged)"
else
# übergibt alle argumente an git commit -m als commit nachricht
git commit -m "$*"
fi
}
function gc {
case "$1" in
"")
echo "[ERROR] Branch/Option fehlt..."
;;
--help | -h)
cat <<EOF
Description:
"git checkout" on steroids
Usage:
gc [--help|--new-branch] <branch>
Options:
-h, --help Prints this help.
-b, --new-branch Create a new Branch instead changing to an existing branch.
Author:
michael.grote@posteo.de - git.mgrote.net
EOF
;;
--new-branch | -b)
(git checkout --quiet -b "$2" && git push --set-upstream "$(git remote | head -n 1)" "$2" && echo '>>> Create new branch "'"$2"'"') ||
(git checkout --quiet "$2" > /dev/null 2>&1 && echo '>>> Branch already exists, checking out... ')
;;
*)
(echo ">>> Try local checkout" && git checkout --quiet "$1" > /dev/null 2>&1) ||
(echo ">>> Try remote checkout" && git checkout --quiet -b "$1" origin/"$1" > /dev/null 2>&1 || echo ">>> Unkown branch!")
esac
}
function gsp {
case "$1" in
"")
git stage -p
;;
--help | -h)
cat <<EOF
Description:
"git stage -p" + "git commit" combined
without parameter only "gsp" is called,
if '$1' is set; "gcm" is called too
Usage:
gsp [--help] <message>
Options:
-h, --help Prints this help.
Author:
michael.grote@posteo.de - git.mgrote.net
EOF
;;
*)
git stage -p && git commit -m "$*"
;;
esac
}
### Aliase
# tmux
alias tmuxa="tmux a -t "
alias tmuxn="tmux new -s "
alias tmuxl="tmux ls"
alias tmuxk="tmux kill-session -t"
# abkuerzungen
alias se="sudoedit"
alias ip="ip --color=auto"
alias cls="clear"
alias tracert="traceroute"
alias df="df -h"
alias du="du -h"
alias grep="grep -i --color=auto"
alias hostname="hostname -f"
alias untar="tar -xzf"
alias unmount="umount"
alias psk="pkill"
alias ru="rip url"
alias mkdir="mkdir -p"
alias last="last -w"
alias ll="ls -lah"
# git
alias gs="git status"
alias gp="git stash && git pull && git stash pop"
alias gf="git fetch"
alias gdel="git add . && git stash && git stash clear"
alias glast="git log -1 HEAD --stat"
alias gd="git diff"
alias glogs="git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
# vim
alias v="view"
# docker
alias dcu="docker-compose up"
alias dcd="docker-compose down"
alias dcr="docker-compose down && docker-compose up -d"
alias lzd="docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock lazyteam/lazydocker"
# safeguard
alias mv="mv -i "
alias cp="cp -i "
alias rm="rm -i "
# rest
alias suspend="~/dotfiles/scripts/pwr.sh -sd"
alias hibernate="~/dotfiles/scripts/pwr.sh -hb"
alias restart="~/dotfiles/scripts/pwr.sh -r"
alias shutdown="~/dotfiles/scripts/pwr.sh -s"
alias reboot="~/dotfiles/scripts/pwr.sh -r"