bash/.bash_aliases
2025-07-07 21:42:25 +02:00

603 lines
21 KiB
Bash

# INFO:
# ╭──────────╮
# │ defaults │
# ╰──────────╯
alias sudo="sudo -E"
alias please="sudo"
# ─< easier dir up >────────────────────────────────────────────────────────────────────────
alias ..="cd .."
alias ...="cd ../.."
# ─< colored ip >───────────────────────────────────────────────────────────────────
alias ip="ip --color=always"
if command-exists curl; then
# ─< linutil >────────────────────────────────────────────────────────────────────────────
alias linutil="curl -fsSL https://christitus.com/linux | sh"
alias "linutil-dev"="curl -fsSL https://christitus.com/linuxdev | sh"
# ─< weather >──────────────────────────────────────────────────────────────────────────────
alias www="curl wttr.in/Ulm"
else
echo-missing "curl"
fi
# ─< check for rg >─────────────────────────────────────────────────────────────────────────
if command-exists rg; then
alias grep="rg --color=always"
alias hl="rg --passthrough"
else
echo-missing "ripgrep"
alias grep="grep --color=always"
alias hl="grep --passthrough"
fi
if command-exists dog; then
alias dig="dog"
fi
# ─< define copy command >────────────────────────────────────────────────────────────────
if command-exists wl-copy; then
copy() {
local file
file="$1"
if cat "$file"; then
command cat "$file" | wl-copy
else
"$file" | wl-copy
fi
}
elif command-exists xclip; then
copy() {
local file
file="$1"
if cat "$file"; then
command cat "$file" | wl-copy
else
"$file" | xclip -selection clipboard
fi
}
else
echo-missing "wl-clipboard | xclip"
fi
# ─< telnet (starwars) >────────────────────────────────────────────────────────────────────
if command-exists telnet; then
alias starwars="telnet -a telehack.com"
fi
if command-exists hyprpanel; then
alias get_cpu='for i in /sys/class/hwmon/hwmon*/temp*_input; do echo "$(<$(dirname $i)/name): $(cat ${i%_*}_label 2>/dev/null || echo $(basename ${i%_*})) $(readlink -f $i)"; done'
fi
# ─< rsync >────────────────────────────────────────────────────────────────────────────────
if command-exists rsync; then
alias scp="rsync -avP"
alias cp="rsync -avP"
else
echo-missing "rsync"
fi
# ─< cli explorer >───────────────────────────────────────────────────────────────────────
if command-exists yazi; then
echo-info "yazi is the explorer of choice"
# ─< yazi move when exit >────────────────────────────────────────────────────────────────
function y() {
local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
yazi "$@" --cwd-file="$tmp"
if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
builtin cd -- "$cwd"
fi
rm -f -- "$tmp"
}
alias lf="y || ya pack -i"
elif command-exists ranger; then
echo-info "ranger is the explorer of choice"
alias lf="ranger"
elif command-exists lf; then
echo-info "lf is the explorer of choice"
else
echo-missing "yazi"
fi
# ─< colorized ls >─────────────────────────────────────────────────────────────────────────
if command-exists exa; then
alias ls="exa --icons -l --git"
alias l="exa --long --no-filesize --no-permissions --no-time --git --colour-scale --icons"
alias ll="exa --all --long --no-filesize --no-permissions --no-time --git --colour-scale --icons"
alias tree="exa --icons -l --tree"
elif command-exists lsd; then
alias ls="lsd -l -1 -h1 --almost-all --git"
alias l="lsd -1"
alias ll="lsd -1 --almost-all"
alias clearl="command clear && l"
alias tree="lsd --tree"
elif command-exists eza; then
alias ls="eza --icons --long --git"
alias l="eza --icons -l"
alias ll="eza --icons -laa"
alias tree="eza --icons -l --tree"
else
echo-missing "exa | eza | lsd"
alias ls="ls --color=always -lAph"
alias l="ls --color=always -lph -w1"
alias ll="ls --color=always -lph"
fi
# ─< t stands for trash(-cli) >───────────────────────────────────────────────────────────────
if command-exists trash; then
alias rm="trash $@"
alias t="trash"
elif command-exists trash-cli; then
alias rm="trash-cli $@"
alias t="trash-cli"
else
echo-missing "trash-cli"
fi
# ─< wireshark / termshark alias >────────────────────────────────────────────────────────
if command-exists termshark; then
alias ws="$_sudo termshark"
fi
# Function to get the IP address
get_ip() {
ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1
}
# ─< h stands for HUGO >──────────────────────────────────────────────────────────────────
if command-exists hugo; then
alias h='hugo'
alias hs='hugo server -D --noHTTPCache --disableFastRender' # --bind "$(get_ip)"'
fi
# ─< bat alias >────────────────────────────────────────────────────────────────────────────
if command-exists batcat; then
alias cat="batcat --color=always -p --paging=never"
alias less="batcat --paging always --color=always"
# alias gd="batcat --diff"
elif command-exists bat; then
alias cat="bat --color=always -p"
alias less="bat --paging always --color=always"
# alias gd="bat --diff"
else
echo-missing "bat"
fi
# ─< fastfetch >────────────────────────────────────────────────────────────────────────────
if command-exists fastfetch; then
alias ff="fastfetch"
alias clearff="command clear & fastfetch"
alias clearf="command clear & fastfetch"
if fastfetch --config os >/dev/null 2>&1; then
alias f="fastfetch --config os"
else
git clone https://git.k4li.de/mirrors/fastfetch.git "$HOME/.local/share/fastfetch" >/dev/null 2>&1
exec "$SHELL"
fi
else
echo-missing "fastfetch"
fi
# NOTE:
# ───────────────────────────────────< Helper functions >───────────────────────────────────
source-script() {
local url import
url="$1"
import="$(mktemp)"
# ─< if $1 is a local file, source this one instead >─────────────────────────────────────
if [ -f "$url" ]; then
source "$url"
sleep 0.1
return 0
else
echo-info "Sourcing external script:${NC} $url"
# ─< if $1 is a url, grab it and source it, also deletes afterwards >─────────────────────
if command-exists curl; then
curl -fsSL $url -o $import
elif command-exists wget; then
wget -o $import $url
else
echo "curl/wget is required, but missing.."
exit 69
fi
source "$import"
sleep 0.1
rm -f "$import"
fi
}
tmux-autosession() {
if [ -z "$TMUX" ]; then
sleep 0.01
# Attach to existing session if any, otherwise create one named 'main'
if tmux ls &>/dev/null; then
sleep 0.01
tmux attach-session
else
sleep 0.01
tmux new-session
fi
fi
}
# ─< Function to determine which Neovim command to use >──────────────────────────────────
choose_nvim() {
if [ -n "$TMUX" ]; then
# If inside an active tmux session, use nvim
echo "command nvim"
return
elif [ -n "$DISPLAY" ] || [ -n "$WAYLAND_DISPLAY" ]; then
# If in a graphical environment, use Neovide
if command-exists neovide; then
echo "neovide --fork"
return
fi
fi
# Default to nvim
echo "command nvim"
}
setup-alias() {
local alias
alias="$1"
case "$alias" in
podman)
alias up="podman-compose up"
alias down="podman-compose down"
alias pull="podman-compose pull"
alias dr="podman run --rm -it"
alias drs="podman-compose down && podman-compose up -d --remove-orphans --force-recreate"
alias ds="podman ps -a --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dcs="podman-compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dl="podman-compose logs -f"
alias dc="podman-compose"
;;
docker)
alias d="docker"
alias dr="docker run --rm -it"
alias up="docker compose up"
alias down="docker compose down"
alias pull="docker compose pull"
alias drs="docker compose down && docker compose up -d --remove-orphans --force-recreate"
alias ds="docker ps -a --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dc="docker compose"
alias dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dlog="docker compose logs"
if ! command-exists gmd; then
alias gmd='bash -c "$(curl -sLo- https://raw.githubusercontent.com/ajayd-san/gomanagedocker/main/install.sh)" && "$SHELL"'
fi
# ─< install lazydocker >─────────────────────────────────────────────────────────────────
alias inst_lazydocker="curl https://raw.githubusercontent.com/jesseduffield/lazydocker/master/scripts/install_update_linux.sh | bash"
;;
git)
# ───────────────────────────────────────< aliases >─────────────────────────────────────
alias g="git"
alias gs="git status -sb"
alias gsl="git status"
alias gm='git checkout main && git merge'
alias gc="git clone --recurse-submodule"
alias gd="git diff"
# alias ga="gd $1 && git add"
alias gp='echo "${CYAN}Updating submodules..${NC}" && git submodule update --init --recursive && echo "${CYAN}Pulling down submodules..${NC}" && git pull --recurse-submodule'
alias gms='git maintenance start'
alias gcm="git commit -m"
alias gpu="git push --recurse-submodule=on-demand"
# ╭──────────────────────────────────────╮
# │ git add with commit like.. │
# │ `git add "<file>" "<commitmessage>"` │
# │ .. or like `git add "<file>"` │
# ╰──────────────────────────────────────╯
ga() {
local file1 file2
file1=$1
file2=$2
if [ -n "$file2" ]; then
git diff ${file1:-.}
git add ${file1:-.}
git commit -m "$file2"
else
git diff ${file1:-.}
git add ${file1:-.}
fi
}
# ──────────────────────────────────────< functions >────────────────────────────────────
# a neat way to clone github repos with a shotname like
# $> gcl pik4li/ReDeploy.git
gcl() {
local file1 file1
if [ -z "$file2" ]; then
git clone --depth=1 "https://github.com/$file1"
else
git clone --depth=1 "https://github.com/$file1" "$file2"
fi
}
# just a fast way to clone from my own instance with a shotname like
# $> gck dotfiles/bash
gck() {
local uri_path clone_path
repo_path=$1
clone_path=$2
if [ -z "$clone_path" ]; then
git clone --recurse-submodules --depth=1 "https://git.k4li.de/$repo_path"
else
git clone --recurse-submodules --depth=1 "https://git.k4li.de/$repo_path" "$clone_path"
fi
}
# Git Submodule Update - gsu
# updates submodules inside a mother repo to the latest main branch and
# commits the files directly. Perfect for dotfile repository management,
# where you JUST want to fetch all the latest changes from all submodules of
# one `big` repo
gsu() {
echo "${CYAN}Updating submodules recursively with -> ${YELLOW}${BOLD}git submodule update --init --recursive${NC}"
git submodule update --init --recursive &&
echo "${CYAN}${BOLD}-- Updated submodules recursively --${NC}"
echo "${CYAN}${BOLD}-- Checking submodule branches... --${NC}"
git submodule foreach '
echo "${RED}Submodule: $name ${NC}"
branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "detached")
if [ "$branch" = "detached" ]; then
default_branch=$(git config -f $toplevel/.gitmodules submodule.$name.branch || echo "main")
echo "${RED}${BOLD}Submodule $name is detached. Checking out ${YELLOW} $default_branch${RED}...${NC}"
git checkout $default_branch
else
echo "${GREEN}${BOLD}Submodule $name is on branch $branch.${NC}"
fi
'
echo "${CYAN}Pulling down updates recursively with -> ${YELLOW}${BOLD}git submodule foreach git pull --recurse-submodule${NC}"
git submodule foreach git pull --recurse-submodule &&
echo "${GREEN}${BOLD}-- pulled down submodules recursively --${NC}"
gUpdateModules() {
if ! git diff --exit-code .; then
echo "${CYAN}${BOLD}Staging changes...${NC}"
git add . || echo "GIT ADD MISSFUNCTION"
sleep 0.3
if git commit -m " update: submodules"; then
echo "${GREEN}${BOLD}commit message ${RED}' update: submodules'${GREEN} successfully commited${NC}"
else
echo "${RED}${BOLD}Failed to commit changes.${NC}"
return 1
fi
else
echo "${GREEN}${BOLD}No changes to commit.${NC}"
return 1
fi
}
if gUpdateModules; then
if git push; then
echo "${GREEN}${BOLD}Push successful.${NC}"
else
echo "${RED}${BOLD}Failed to push changes.${NC}"
return 1
fi
fi
}
# fast way to commit files and a message within one command, also pushes the
# latest files specifyed, with an optional commit message (defaults to 'wip')
# also checks before push, if remote changes exist, warns and does nothing if so
#
# takes in a space separated list of files, and an optional commit message. like:
# $> gwip file1 path/to/file2 dir1 ./path/to/dir2 "This would be so many commands without the `gwip` function!! <3"
#
# $> gwip 'files and folders' 'commit message'
gwip() {
local BOLD GREEN YELLOW NC
BOLD=$'\e[1m'
GREEN=$'\e[92m'
YELLOW=$'\e[93m'
NC=$'\e[0m'
# Fetch the latest changes from the remote
git fetch
# Get the current branch name
local branch
branch=$(git rev-parse --abbrev-ref HEAD)
# Check if there are any changes on the remote branch
if git diff --quiet "$branch" "origin/$branch"; then
local commit_files=()
local commit_message
if [[ -n "$1" ]]; then
for f in "$@"; do
if [ -f "./${f}" ] || [ -d "./${f}/" ]; then
commit_files+=("./${f}")
else
commit_message+="${f}"
break
fi
done
else
echo "${BOLD}${YELLOW}You have to provide the command like..:"
echo "${GREEN}'gwip <list of files> <commit message>' or like 'gwip .'${NC}"
return
fi
case $commit_files in
'./.') commit_message="wip" ;;
*)
local trimmed_files
for f in "${commit_files[@]}"; do
trimmed_files+=("${f#./}")
done
echo "${GREEN}Committed files/folders: ${BOLD}${trimmed_files[*]}${NC}"
;;
esac
if [ -n "$commit_files" ] || [ -n "$commit_message" ]; then
[[ -z "$commit_files" ]] && commit_files=.
[[ -z "$commit_message" ]] && commit_message=wip
echo "${CYAN}No changes on the remote branch. Adding changes and pushing with ${RED}${BOLD}'$commit_message'${NC}${CYAN} commit.${NC}"
if (("${#commit_files}" <= 1)); then
git add $commit_files
else
for f in "${commit_files[@]}"; do
git add $f
done
fi
git commit -m "${commit_message:-wip}"
git push
else
echo "${RED}${BOLD}There was something wrong with:${NC}"
echo "${YELLOW}- commit-files '$commit_files'"
echo "or.."
echo "${YELLOW}- commit-message '$commit_message'${NC}"
fi
else
echo "${RED}${BOLD}There are changes on the remote branch. Please pull the latest changes first.${NC}"
fi
}
;;
tmux)
if [[ $- == *i* ]]; then
if $autosession; then
tmux-autosession
fi
fi
# function to quickly go into an active session
ta() {
if tmux list-sessions >/dev/null 2>&1; then
echo "-- tmux session active! | Connecting to active session --"
sleep 0.3
tmux attach
else
echo "-- No tmux session found! | Creating one --"
sleep 0.3
tmux
fi
}
;;
neovim)
alias cnvim="command nvim"
alias nvim="$(choose_nvim)"
nv() {
appname="$1"
shift
if [ "$#" -eq 0 ]; then
NVIM_APPNAME="$appname" command nvim
else
NVIM_APPNAME="$appname" command nvim "$@"
fi
}
if [ -d "$HOME/.config/nvdev" ]; then
alias nvdev='NVIM_APPNAME="nvdev" command nvim'
alias neodev='NVIM_APPNAME="nvdev" neovide --fork'
fi
;;
esac
}
# fills in the $missing variable for the main config to get the missing cli
# tools directly inside the "dashboard"/loading page
missing() {
local essentials=() i
essentials=(
"bash"
"zsh"
"fzf"
"duf"
"curl"
"wget"
)
for i in "${essentials[@]}"; do
if ! command-exists $i; then
echo-missing "$i"
fi
done
}
resolve() {
local quiet ip server dig_out line ptr hostname time
quiet=false
if [[ "$1" == "-q" ]]; then
quiet=true
shift
fi
ip="$1"
server="$2"
dig_out=$(command dig +noall +answer -x "$ip" ${server:+@$server} 2>/dev/null)
line=$(grep 'PTR' <<<"$dig_out")
ptr=$(awk '{print $4}' <<<"$line")
time=$(awk '{print $2}' <<<"$dig_out")
hostname=$(awk '{print $5}' <<<"$line" | sed 's/\.$//')
if $quiet; then
[[ -n "$hostname" ]] && echo "$hostname"
return
fi
if [[ -n "$hostname" ]]; then
# ✅ resolved
printf "${BOLD}${GREEN}%s${NC} \033[1;90m%s${NC} \033[32m%s${NC} \033[2m%s ms${NC}\n" \
"$ptr" "$ip" "$hostname" "$time"
else
# ❌ not resolved
printf "${RED}%s${NC} \033[1;90m%s${NC} \033[1;31mNOT FOUND${NC} \033[2m%s ms\033[0m\n" \
"PTR" "$ip" "${time:-0}"
fi
}
main() {
# ─< g stands for GIT >─────────────────────────────────────────────────────────────────────
if command-exists git; then
setup-alias git
else
echo-missing "git"
fi
# Set up Neovim aliases based on environment
if command-exists nvim; then
setup-alias neovim
else
echo-missing "neovim"
fi
# ─< d stands for docker >──────────────────────────────────────────────────────────────────
if command-exists docker; then
setup-alias docker
elif command-exists podman; then
setup-alias podman
else
echo-missing "docker | podman"
fi
# Tmux session manager
if command-exists tmux; then
setup-alias tmux
else
echo-missing "tmux"
fi
}
main