bash: put aliases und functions in extra files

This commit is contained in:
Michael Grote 2024-05-06 22:55:03 +02:00
parent eb76869379
commit cc7c6d158a
3 changed files with 217 additions and 216 deletions

35
.bash_aliases Normal file
View file

@ -0,0 +1,35 @@
# tmux
alias tmuxa="tmux a -t "
alias tmuxn="tmux new -s "
alias tmuxl="tmux ls"
alias tmuxk="tmux kill-session -t"
# abkuerzungen
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 ru="rip url"
alias mkdir="mkdir -p"
alias last="last -w"
alias ll="ls -lAh"
alias tree="tree -a -I **.git**"
# git
alias gs="git status"
alias gf="git fetch"
alias gdel="git reset --hard ; git clean -f"
alias glast="git log -1 HEAD --stat"
alias gd="git difftool"
alias gsp="git stage -p"
# docker
alias dcu="docker-compose up"
alias dcd="docker-compose down"
alias dcr="docker-compose down && docker-compose up -d"
# safeguard
alias mv="mv -i "
alias cp="cp -i "
alias rm="rm -i "

View file

@ -1,4 +1,8 @@
#!/bin/bash
source .bash_aliases
source .bash_functions
### tmux auto attach
# Check if the user ID is not 0 (root)
@ -69,219 +73,3 @@ 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 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 --ignore-case --files --hidden --glob=!.git/ *"$1"*
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'find'"
find . -type f -not -path '*/\.git/*' -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 --ignore-case --hidden --glob=!.git/ "$1"
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'grep'"
grep --recursive --line-number --ignore-case --exclude-dir=".git" "$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 -s -m "changed Files: $(git diff --name-only --staged)"
else
# übergibt alle argumente an git commit -m als commit nachricht
git commit -s -m "$*"
fi
}
function gc {
case "$1" in
--help | -h)
cat <<EOF
Description:
"git checkout" on steroids
Usage:
gc [--help|--new-branch] [branch]
Options:
-h, --help Print 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... ')
;;
*)
if [ ! -z $1 ] ; then
(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!")
else
(git checkout --quiet master > /dev/null 2>&1 && echo '>>> Try checking out master') ||
(git checkout --quiet main > /dev/null 2>&1 && echo '>>> Try checking out main') ||
(git checkout --quiet release > /dev/null 2>&1 && echo '>>> Try checking out release')
fi
esac
}
function glogs {
# https://git-scm.com/docs/pretty-formats
# https://stackoverflow.com/questions/3631005/git-log-tabular-formatting
# https://stackoverflow.com/questions/7736781/how-to-make-git-log-not-prompt-to-continue
if [ -z "$1" ]; then
ncommits=30;
else
ncommits="$1";
fi;
git log --pretty=format:'%C(auto) %<|(16,trunc)%h %Cred %G?%Creset %<|(60,trunc)%s %<(20,trunc)%Cgreen%cr%Creset %<(15,trunc)%Cblue%an%Creset %Cred%D' --graph --all -$ncommits
}
function gp {
git pull "$@"
}
function mdtoc {
grep --recursive --include="*.md" ^# | sort --version-sort --field-separator=":" | awk 'BEGIN {FS=":"}{print $2 "&&" $1}' | column --table --separator "&&" --table-truncate 1,2
}
# "scrolle" durch die commit-history
ghist() {
local total_commits=$(git rev-list HEAD --count)
local current_commit=$total_commits
show_commit_diff() {
if [ $current_commit -le $total_commits ] && [ $current_commit -ge 1 ]; then
local commit_hash=$(git rev-list --reverse HEAD | sed -n "${current_commit}p")
git --no-pager show $commit_hash
fi
}
while true; do
clear # Clear the terminal
show_commit_diff
# Display navigation options
echo "-----------------------------------------------------------"
echo "Navigation: N - Next Commit, P - Previous Commit, Q - Quit"
echo "-----------------------------------------------------------"
read -n1 -s option
case $option in
p|P)
if [ $current_commit -gt 1 ]; then
((current_commit--))
fi
;;
n|N)
if [ $current_commit -lt $total_commits ]; then
((current_commit++))
fi
;;
q|Q)
break
;;
*)
echo "Invalid option"
;;
esac
done
}
function sar {
case "$1" in
--help | -h)
cat <<EOF
Description:
Search and replace recursively.
Usage:
sar [--help] <search> <replace>
Options:
-h, --help Print this help.
Author:
michael.grote@posteo.de - git.mgrote.net
EOF
;;
esac
if [ "$1" != "-h" ] && [ "$1" != "--help" ] ; then
if [ $# = 2 ] ; then
find . -name '*' -type f -not -path '*/\.git/*' -exec sed -i "s/$1/$2/" {} \;
elif [ $# = 1 ] ; then
echo ">>> Not enough arguments..."
elif [ $# -gt 2 ] ; then
echo ">>> Too many arguments..."
fi
fi
}
### Aliase
# tmux
alias tmuxa="tmux a -t "
alias tmuxn="tmux new -s "
alias tmuxl="tmux ls"
alias tmuxk="tmux kill-session -t"
# abkuerzungen
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 ru="rip url"
alias mkdir="mkdir -p"
alias last="last -w"
alias ll="ls -lAh"
alias tree="tree -a -I **.git**"
# git
alias gs="git status"
alias gf="git fetch"
alias gdel="git reset --hard ; git clean -f"
alias glast="git log -1 HEAD --stat"
alias gd="git difftool"
alias gsp="git stage -p"
# docker
alias dcu="docker-compose up"
alias dcd="docker-compose down"
alias dcr="docker-compose down && docker-compose up -d"
# safeguard
alias mv="mv -i "
alias cp="cp -i "
alias rm="rm -i "

178
.bash_functions Normal file
View file

@ -0,0 +1,178 @@
### Funktionen
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 --ignore-case --files --hidden --glob=!.git/ *"$1"*
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'find'"
find . -type f -not -path '*/\.git/*' -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 --ignore-case --hidden --glob=!.git/ "$1"
else
echo "[INFO] ripgrep ist nicht installiert... suche mit 'grep'"
grep --recursive --line-number --ignore-case --exclude-dir=".git" "$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 -s -m "changed Files: $(git diff --name-only --staged)"
else
# übergibt alle argumente an git commit -m als commit nachricht
git commit -s -m "$*"
fi
}
function gc {
case "$1" in
--help | -h)
cat <<EOF
Description:
"git checkout" on steroids
Usage:
gc [--help|--new-branch] [branch]
Options:
-h, --help Print 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... ')
;;
*)
if [ ! -z $1 ] ; then
(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!")
else
(git checkout --quiet master > /dev/null 2>&1 && echo '>>> Try checking out master') ||
(git checkout --quiet main > /dev/null 2>&1 && echo '>>> Try checking out main') ||
(git checkout --quiet release > /dev/null 2>&1 && echo '>>> Try checking out release')
fi
esac
}
function glogs {
# https://git-scm.com/docs/pretty-formats
# https://stackoverflow.com/questions/3631005/git-log-tabular-formatting
# https://stackoverflow.com/questions/7736781/how-to-make-git-log-not-prompt-to-continue
if [ -z "$1" ]; then
ncommits=30;
else
ncommits="$1";
fi;
git log --pretty=format:'%C(auto) %<|(16,trunc)%h %Cred %G?%Creset %<|(60,trunc)%s %<(20,trunc)%Cgreen%cr%Creset %<(15,trunc)%Cblue%an%Creset %Cred%D' --graph --all -$ncommits
}
function gp {
git pull "$@"
}
function mdtoc {
grep --recursive --include="*.md" ^# | sort --version-sort --field-separator=":" | awk 'BEGIN {FS=":"}{print $2 "&&" $1}' | column --table --separator "&&" --table-truncate 1,2
}
# "scrolle" durch die commit-history
ghist() {
local total_commits=$(git rev-list HEAD --count)
local current_commit=$total_commits
show_commit_diff() {
if [ $current_commit -le $total_commits ] && [ $current_commit -ge 1 ]; then
local commit_hash=$(git rev-list --reverse HEAD | sed -n "${current_commit}p")
git --no-pager show $commit_hash
fi
}
while true; do
clear # Clear the terminal
show_commit_diff
# Display navigation options
echo "-----------------------------------------------------------"
echo "Navigation: N - Next Commit, P - Previous Commit, Q - Quit"
echo "-----------------------------------------------------------"
read -n1 -s option
case $option in
p|P)
if [ $current_commit -gt 1 ]; then
((current_commit--))
fi
;;
n|N)
if [ $current_commit -lt $total_commits ]; then
((current_commit++))
fi
;;
q|Q)
break
;;
*)
echo "Invalid option"
;;
esac
done
}
function sar {
case "$1" in
--help | -h)
cat <<EOF
Description:
Search and replace recursively.
Usage:
sar [--help] <search> <replace>
Options:
-h, --help Print this help.
Author:
michael.grote@posteo.de - git.mgrote.net
EOF
;;
esac
if [ "$1" != "-h" ] && [ "$1" != "--help" ] ; then
if [ $# = 2 ] ; then
find . -name '*' -type f -not -path '*/\.git/*' -exec sed -i "s/$1/$2/" {} \;
elif [ $# = 1 ] ; then
echo ">>> Not enough arguments..."
elif [ $# -gt 2 ] ; then
echo ">>> Too many arguments..."
fi
fi
}