zsh/.zshrc

652 lines
23 KiB
Bash

# ─< Helper functions >─────────────────────────────────────────────────────────────────
# ANSI color codes
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
_STORED_ERRORS=""
_STORED_WARNINGS=""
_STORED_INFOS=""
# Modified echo functions that both store and display messages
function echo_error() {
local message="${RED}$1${NC}\n"
printf "$message" >&2
_STORED_ERRORS="${_STORED_ERRORS}${message}"
}
function echo_warning() {
local message="${YELLOW}$1${NC}\n"
printf "$message"
_STORED_WARNINGS="${_STORED_WARNINGS}${message}"
}
function echo_info() {
local message="${CYAN}$1${NC}\n"
printf "$message"
_STORED_INFOS="${_STORED_INFOS}${message}"
}
# Function to display all stored messages
function error_log() {
local has_messages=0
# First check if we have any messages at all
if [ -z "$_STORED_ERRORS" ] && [ -z "$_STORED_WARNINGS" ]; then
return 0
fi
# Only display sections that have content
if [ -n "$_STORED_ERRORS" ]; then
printf "\n${BOLD}${RED}=== Errors ===${NC}\n"
printf "$_STORED_ERRORS"
has_messages=1
fi
if [ -n "$_STORED_WARNINGS" ]; then
printf "\n${BOLD}${YELLOW}=== Warnings ===${NC}\n"
printf "$_STORED_WARNINGS"
has_messages=1
fi
if [ -n "$_STORED_INFOS" ]; then
[ "$has_messages" -eq 1 ] && printf "\n"
printf "${BOLD}${CYAN}=== Info ===${NC}\n"
printf "$_STORED_INFOS"
has_messages=1
fi
}
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# Check if the user is root and set sudo variable if necessary
check_root() {
if [[ "${EUID}" -ne 0 ]]; then
if command_exists sudo; then
echo_info "User <$(whoami)> is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
# Load completions
autoload -Uz compinit && compinit
bindkey -e
# Use Alt + Backspace to delete the previous word
bindkey '^[^?' backward-kill-word
# Use Alt + Left Arrow to move to the previous word
bindkey '^[[1;3D' backward-word
# Use Alt + Right Arrow to move to the next word
bindkey '^[[1;3C' forward-word
setopt appendhistory
setopt sharehistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups
autoload -Uz select-word-style
select-word-style shell
# Huge history. Doesn't appear to slow things down, so why not?
HISTSIZE=500000
HISTFILESIZE=100000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
# Completion styling
zstyle :compinstall filename '/home/pika/.zshrc'
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath'
setopt autocd notify
# End of lines configured by zsh-newuser-install
# ─< init fzf for zsh >───────────────────────────────────────────────────────────────────
if command_exists fzf; then
source <(fzf --zsh)
fi
_init (){
if command_exists oh-my-posh; then
# eval "$(oh-my-posh init zsh --config 'https://git.k4li.de/dotfiles/oh-my-posh/raw/branch/main/amro.toml')"
# eval "$(oh-my-posh init zsh --config '~/.zsh/themes/sim-web.toml')"
eval "$(oh-my-posh init zsh --config '~/.zsh/themes/amro.toml')"
# eval "$(oh-my-posh init zsh --config '~/.zsh/themes/atomicBit.toml')"
else
curl -s https://ohmyposh.dev/install.sh | sudo bash --norc -s -- -d /usr/bin/
fi
if command_exists zoxide; then
eval "$(zoxide init zsh)"
eval "$(zoxide init zsh --cmd cd)"
fi
# ─< environment variables for zsh >──────────────────────────────────────────────────────
local zconf="$HOME/.zsh"
local zAutosg="$zconf/autosuggestions/zsh-autosuggestions.zsh"
local zSynthl="$zconf/syntax-highlighting/zsh-syntax-highlighting.zsh"
local zTruecolor="$zconf/custom/256color.zsh"
local zAutopairs="$zconf/autopairs/autopair.zsh"
local zFzfCd="$zconf/custom/zsh-interactive-cd.plugin.zsh"
local zAgentManagement="$zconf/custom/agent.zsh"
local zCommandNotFound="$zconf/custom/command-not-found.plugin.zsh"
local zExtraction="$zconf/extract/extract.plugin.zsh"
local _pluginlist=(
"$zAutosg"
"$zSynthl"
"$zTruecolor"
"$zAutopairs"
"$zFzfCd"
"$zAgentManagement"
"$zCommandNotFound"
"$zExtraction"
)
DEBUG_PLUG=""
# ─< init plugis >────────────────────────────────────────────────────────────────────────
for zPlug in "${_pluginlist[@]}"; do
[[ -f "$zPlug" ]] &&
. $zPlug
DEBUG_PLUG="$DEBUG_PLUG
plugin $zPlug loadet."
done
p_has() {
if ! command_exists has; then
inst_has() {
git clone https://github.com/kdabir/has.git /tmp/has && cd /tmp/has && sudo make install && cd $HOME/ && rm -r /tmp/has
}
echo_info "Installing has"
inst_has >/dev/null 2>&1
else
tools() {
local pkgs=("bash" "zsh" "git" "curl" "make" "cmake" "gcc" "g++" "rg" "docker" "composer" "node" "npm" "php" "jre" "python3" "go" "cargo")
has $pkgs
}
fi
}
p_has
}
_alias(){
alias please="sudo"
alias untar="tar -xf"
# ─< easier dir up >────────────────────────────────────────────────────────────────────────
alias ..="cd .."
# ─< weather >──────────────────────────────────────────────────────────────────────────────
alias www="curl wttr.in/Ulm"
# ─< colored ip >───────────────────────────────────────────────────────────────────
alias ip="ip --color=always"
# ─< check for rg >─────────────────────────────────────────────────────────────────────────
if command_exists rg; then
alias grep="rg --color=always"
else
alias grep="grep --color=always"
fi
# ─< linutil >────────────────────────────────────────────────────────────────────────────
alias linutil="curl -fsSL https://christitus.com/linux | sh"
alias "linutil-dev"="curl -fsSL https://christitus.com/linuxdev | sh"
# ─< telnet (starwars) >────────────────────────────────────────────────────────────────────
if command_exists telnet; then
alias starwars="telnet -a telehack.com"
fi
[[ -f "$HOME/go/bin/lazygit" ]] &&
alias lazygit="$HOME/go/bin/lazygit" &&
alias lg="lazygit"
# ─< cli explorer >───────────────────────────────────────────────────────────────────────
if command_exists yazi; then
echo_info "yazi is the explorer of choise"
alias lf="yazi || ya pack -i"
elif command_exists ranger; then
echo_info "ranger is the explorer of choise"
alias lf="ranger"
elif command_exists lf; then
echo_info "lf is the explorer of choise"
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
alias ls="ls --color=always -lAph"
alias l="ls --color=always -lph -w1"
alias ll="ls --color=always -lph"
fi
# ─< t stands for tmux >────────────────────────────────────────────────────────────────────
if command_exists tmux; then
local tmux_y="-- tmux-session active! | connecting to active session --"
local tmux_n="-- no tmux-session found! | creating one --"
ta() {
command tmux list-sessions >/dev/null 2>&1
if [ $? -eq 0 ]; then
if command_exists notify-send; then
notify-send "TMUX" "$tmux_y"
sleep 0.5
tmux attach
else
echo_info "$tmux_y"
sleep 0.5
tmux attach
fi
else
if command_exists notify-send; then
notify-send "TMUX" "$tmux_n"
sleep 0.5
tmux
else
echo_info "$tmux_n"
sleep 0.5
tmux
fi
fi
}
alias ts="tmux source $HOME/.tmux.conf"
fi
# ─< v stands vor virtual-machine (kvm) >─────────────────────────────────────────────────
if command_exists virsh; then
alias vm="virsh"
alias vms="virsh start"
alias vml="virsh list --all"
alias vmsd="virsh shutdown"
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_error "-- You do not have trash or trash-cli installed! Your 'rm' will be permanent! --"
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"
fi
# ─< wireguard alias >────────────────────────────────────────────────────────────────────
if command_exists wg-quick; then
alias wgup="wg-quick up"
alias wgdown="wg-quick down"
fi
# ─< wireshark / termshark alias >────────────────────────────────────────────────────────
if command_exists termshark; then
alias ws="$_sudo termshark"
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
command clear &
fastfetch
fi
# ─< d stands for docker >──────────────────────────────────────────────────────────────────
if command_exists docker; then
alias up="docker compose up"
alias down="docker compose down"
alias pull="docker compose pull"
alias d="docker"
alias dr="docker run --rm -it"
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 dcs="docker compose ps -a --format 'table {{.Name}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'"
alias dl="docker compose logs -f"
alias dc="docker compose"
alias appupdate="docker compose pull && docker compose up -d --force-recreate"
drweb() {
drweb_help() {
echo "Usage: drweb <server_type> [directory] [port]"
echo " server_type: Type of server to use (nginx or php)"
echo " directory: Directory to serve (default: current directory)"
echo " port: Port to use (default: 8080)"
return
}
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
drweb_help
return
fi
local server_type="$1"
local dir="${2:-./}"
local port="${3:-8080}"
if [[ ! -d "$dir" ]]; then
echo "Error: Directory $dir does not exist"
drweb_help
return 1
fi
case "$server_type" in
nginx)
if [[ -f "$dir/index.html" || -f "$dir/index.php" ]]; then
docker run -p "$port:80" -v "$dir:/usr/share/nginx/html:ro" nginx:alpine
echo "Nginx is serving $dir on port $port"
else
echo "Error: No index.html or index.php found in $dir"
drweb_help
return 1
fi
;;
php)
if [[ -f "$dir/index.php" || -f "$dir/index.html" ]]; then
docker run -p "$port:80" -v "$dir:/var/www/html" php:7.4-apache
echo "PHP Apache is serving $dir on port $port"
else
echo "Error: No index.php or index.html found in $dir"
drweb_help
return 1
fi
;;
*)
echo "Error: Unsupported server type '$server_type'"
drweb_help
return 1
;;
esac
}
fi
# ─< g stands for GIT >─────────────────────────────────────────────────────────────────────
if command_exists git; then
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"
gcl() {
if [ -z "$2" ]; then
git clone --depth=1 "https://github.com/$1"
else
git clone --depth=1 "https://github.com/$1" "$2"
fi
}
gck() {
if [ -z "$2" ]; then
git clone --recurse-submodules --depth=1 "https://git.k4li.de/$1"
else
git clone --recurse-submodules --depth=1 "https://git.k4li.de/$1" "$2"
fi
}
gcs() {
if [ -z "$2" ]; then
git clone --recurse-submodules --depth=1 "git@git.k4li.de:$1"
else
git clone --recurse-submodules --depth=1 "git@git.k4li.de:$1" "$2"
fi
}
alias gd="git diff"
alias ga="gd $1 && git add"
alias gp="git pull --recurse-submodule"
alias gms='git maintenance start'
alias gsu="git submodule foreach git pull && git add . && git commit -m ' updated 📌submodules' && echo '-- Committed changes, pushing now..' && sleep 1 && git push"
alias gcm="git commit -m"
alias gpu="git push --recurse-submodule=on-demand"
if command_exists lazygit; then
alias lg="lazygit"
fi
fi
alias inst_lazydocker="curl https://raw.githubusercontent.com/jesseduffield/lazydocker/master/scripts/install_update_linux.sh | bash"
}
_coding_() {
# ─< h stands for HUGO >──────────────────────────────────────────────────────────────────
if command_exists hugo; then
alias h='hugo'
alias hs='hugo server -D --noHTTPCache --disableFastRender --bind "$(get_ip)"'
fi
# ─< c stands for bin/cake >──────────────────────────────────────────────────────────────
# alias cake='bin/cake'
# alias c='cake'
# alias cs='c server -p 1313'
# ─< VSCodium >─────────────────────────────────────────────────────────────────────────────
if command_exists codium; then
alias code="codium"
export EDITOR="codium"
fi
# ─< neovide, the best frontend for any neovim-config >───────────────────────────────────
if command_exists nvim; then
if command_exists neovide; then
if [ -n "$TMUX" ]; then
alias nvim="command nvim"
else
alias nvim="neovide --fork"
fi
fi
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
}
if command_exists composer; then
alias laravel_new="composer create-project laravel/laravel"
fi
# Check if php is available, then create the alias
if command_exists php; then
alias phprun="php artisan serve --host=$(get_ip) --port=8000"
fi
# Check if npm is available, then create the alias
if command_exists npm; then
npmrun(){
npmrun_help() {
echo "Usage: npmrun [environment] [optional:port]"
echo " environment: The npm environment you want to run (e.g. dev)"
echo " port: Port to use (default: 8000)"
return
}
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
npmrun_help
return
fi
local env="$1"
local port="${2:-8080}"
npm run "$env" -- --host="$(get_ip)" --port="$port"
}
fi
}
# ─< rsync >────────────────────────────────────────────────────────────────────────────────
if command_exists rsync; then
alias scp="rsync -avP"
alias cp="scp"
fi
get_packager() {
search=""
install=""
update=""
upgrade=""
refresh=""
remove=""
. /etc/os-release
case "$ID" in
ubuntu | debian | pop | kali)
if command_exists nala; then
search="nala search"
install="nala install --assume-yes"
refresh="nala update"
upgrade="nala upgrade --full"
remove="nala purge"
clean="nala autoremove --assume-yes"
alias update="$_sudo $refresh && $_sudo $upgrade"
alias install="$_sudo $refresh && $_sudo $install"
alias remove="$_sudo $remove"
alias search="$search"
elif command_exists apt-get; then
search="apt-cache search"
install="apt-get install --yes"
refresh="apt-get update"
upgrade="apt-get upgrade"
remove="apt-get purge"
clean="apt-get autoremove"
alias update="$_sudo $refresh && $_sudo $upgrade"
alias install="$_sudo $refresh && $_sudo $install"
alias remove="$_sudo $remove"
alias search="$search"
fi
;;
arch | manjaro | endevouros | garuda)
if command_exists yay; then
alias install="yay -S --noconfirm"
alias update="yay -Syu"
alias remove="yay -R"
alias search="yay -Ss"
elif command_exists paru; then
alias install="paru -S --noconfirm"
alias update="paru -Syu"
alias remove="paru -R"
alias search="paru -Ss"
elif command_exists pacman; then
alias install="$_sudo pacman -S --noconfirm"
alias update="$_sudo pacman -Syu"
alias remove="$_sudo pacman -R"
alias search="$_sudo pacman -Ss"
fi
;;
fedora | centos)
alias install="dnf install --yes"
alias update="dnf update"
alias remove="dnf remove"
alias search="dnf search"
;;
alpine)
install="apk add"
update="apk update"
upgrade="apk upgrade"
remove="apk del"
alias install="$_sudo $install"
alias update="$_sudo $update && $_sudo $upgrade"
alias remove="$_sudo $remove"
;;
esac
}
_tools_(){
if ! command_exists has; then
$(git clone https://github.com/kdabir/has.git /tmp/has && cd /tmp/has && sudo make install && rm -rf /tmp/has)
else
tools() {
local pkgs="bash fish git curl make cmake gcc g++ rg docker composer node npm php jre python3 go cargo"
has $pkgs
}
fi
}
if command_exists bc; then
math() {
echo "$*" | bc -l
}
else
echo_error "No bc, so no math will work"
fi
_environment(){
if command_exists nvim; then
export EDITOR="nvim"
fi
if command_exists ranger; then
export RANGER_LOAD_DEFAULT_RC="FALSE"
fi
# ─< paths >──────────────────────────────────────────────────────────────────────────────
if [ -d "$HOME/.local/bin" ]; then
export PATH="$HOME/.local/bin:$PATH"
if [ -e "$HOME/.local/bin/lazydocker" ]; then
alias ld="$HOME/.local/bin/lazydocker"
fi
fi
if [ -d "$HOME/go/bin" ]; then
export PATH="$HOME/go/bin:$PATH"
fi
}
main(){
get_packager
_init
_alias
_coding_
_environment
error_log
}
if check_root; then
main
fi
# bun completions
[ -s "/home/pika/.bun/_bun" ] && . "$HOME/.bun/_bun"
[ -s "/home/pika/.bun/_bun" ] && export BUN_INSTALL="$HOME/.bun" && export PATH="$BUN_INSTALL/bin:$PATH"