#!/usr/bin/env zsh [[ -o interactive ]] || return ####################################################### # HISTORY CONFIGURATION ####################################################### setopt APPEND_HISTORY SHARE_HISTORY INC_APPEND_HISTORY setopt HIST_IGNORE_SPACE HIST_SAVE_NO_DUPS HIST_VERIFY setopt globdots # Expand the history size and specify history file location. export TERM="xterm-256color" export HISTORY_IGNORE="(ls|cd|pwd|exit|sudo reboot|history|cd -|cd ..)" export HISTFILESIZE=10000 export HISTSIZE=10000 export HISTFILE="${XDG_CACHE_HOME:-$HOME/.cache}/zshhistory" export HISTTIMEFORMAT="%F %T" ####################################################### # XDG BASE DIRECTORY SPECIFICATION ####################################################### export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}" export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" ####################################################### # COLORS AND DISPLAY ####################################################### # Colored man pages export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;31m' export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_so=$'\E[01;44;33m' export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_us=$'\E[01;32m' # General colors export CLICOLOR=1 export LS_COLORS='no=00:fi=00:di=00;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:*.xml=00;31:' ####################################################### # CORE ALIASES ####################################################### # Safety aliases alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' alias mkdir='mkdir -p' # Enhanced commands alias da='date "+%Y-%m-%d %A %T %Z"' if [[ "$OSTYPE" == darwin* ]]; then alias ps='ps aux' else alias ps='ps auxf' fi alias ping='ping -c 10' alias less='less -R' alias cls='clear' alias grep='grep --color=auto' # Navigation alias home='cd ~' alias cd..='cd ..' alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias .....='cd ../../../..' alias bd='cd "$OLDPWD"' ####################################################### # CONDITIONAL TOOL ALIASES ####################################################### # Modern replacements with fallbacks if command -v eza >/dev/null; then alias ls='eza -a --classify --color=always --icons --group-directories-first' alias ll='eza --icons --long --all --group-directories-first --git --header --time-style=relative' alias la='eza -a --color=always --group-directories-first' else # Fallback ls aliases if \ls --color=auto / >/dev/null 2>&1; then alias ls='\ls -aFh --color=auto' alias ll='\ls -alFh --color=auto' alias la='\ls -Ah --color=auto' else # macOS/BSD ls alias ls='\ls -aFhG' alias ll='\ls -alFhG' alias la='\ls -AhG' fi fi # Tree alias: prefer tree command, fall back to eza --tree if command -v tree >/dev/null; then alias tree='tree -CAhF --dirsfirst' alias treed='tree -CAFd' elif command -v eza >/dev/null; then alias tree='eza --tree --color=always --icons' fi # Directory listing variants alias lx='\ls -lXBh 2>/dev/null || \ls -lBh' # sort by extension (GNU ls only) alias lk='\ls -lSrh' # sort by size alias lc='\ls -ltcrh' # sort by change time alias lu='\ls -lturh' # sort by access time alias lr='\ls -lRh' # recursive ls alias lt='\ls -ltrh' # sort by date alias lm='\ls -alh | more' # pipe through 'more' alias lw='\ls -xAh' # wide listing format alias lf="\ls -l | grep -v '^d'" # files only alias ldir="\ls -l | grep '^d'" # directories only # Enhanced cat with fallbacks if command -v bat >/dev/null; then alias cat='bat' elif command -v batcat >/dev/null; then alias cat='batcat' fi # Enhanced grep if command -v rg >/dev/null; then alias rg='rg --color=auto' alias grep='rg' fi # Editor command -v nvim >/dev/null && alias vim='nvim' alias snano='sudo nano' ####################################################### # SYSTEM & UTILITY ALIASES ####################################################### # chmod shortcuts (use with caution) alias mx='chmod a+x' alias 000='chmod -R 000' alias 644='chmod -R 644' alias 666='chmod -R 666' alias 755='chmod -R 755' alias 777='chmod -R 777' # Search shortcuts alias h="history | grep" alias p="ps aux | grep" alias f="find . | grep" alias topcpu="ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10" # System info alias diskspace="du -S | sort -n -r | more" alias folders='du -h --max-depth=1 2>/dev/null || du -h -d 1' alias folderssort='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn' alias mountedinfo='df -hT 2>/dev/null || df -h' # Archive shortcuts alias mktar='tar -cvf' alias mkbz2='tar -cvjf' alias mkgz='tar -cvzf' alias untar='tar -xvf' alias unbz2='tar -xvjf' alias ungz='tar -xvzf' # Network & system if [[ "$OSTYPE" == darwin* ]]; then alias openports='lsof -i -P -n | grep LISTEN' alias rebootforce='sudo shutdown -r now' else alias openports='netstat -nape --inet' alias rebootforce='sudo shutdown -r -n now' fi alias rebootsafe='sudo shutdown -r now' alias shutdownsafe='sudo shutdown -h now' # Docker cleanup (if docker exists) command -v docker >/dev/null && alias docker-clean='docker container prune -f; docker image prune -f; docker network prune -f; docker volume prune -f' # Safer rm alternative alias rmd='command rm -rfv' ####################################################### # FUNCTIONS ####################################################### whatsmyip() { echo -n "Internal IP: " if command -v ip >/dev/null; then ip -4 addr show scope global | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1 elif command -v ifconfig >/dev/null; then ifconfig | awk '/inet / && $2 != "127.0.0.1" {print $2}' | head -n 1 else echo "N/A" fi echo -n "External IPv4: " curl -s --connect-timeout 5 https://api.ipify.org || echo "N/A" echo -n -e "\nExternal IPv6: " curl -s --connect-timeout 5 https://api6.ipify.org || echo "N/A" } alias myip="whatsmyip" # File counting countfiles() { local files=$(find . -type f 2>/dev/null | wc -l) local links=$(find . -type l 2>/dev/null | wc -l) local dirs=$(find . -type d 2>/dev/null | wc -l) echo "$files files, $links links, $dirs directories" } # Command type checker checkcommand() { type -a "$@" 2>/dev/null || echo "Command not found: $*" } # Universal extractor extract() { for archive in "$@"; do if [[ -f $archive ]]; then case ${archive:l} in *.tar.bz2 | *.tbz2) tar xvjf "$archive" ;; *.tar.gz | *.tgz) tar xvzf "$archive" ;; *.tar.xz) tar xvJf "$archive" ;; *.bz2) bunzip2 "$archive" ;; *.rar) command -v unrar >/dev/null && unrar x "$archive" || echo "unrar not installed" ;; *.gz) gunzip "$archive" ;; *.tar) tar xvf "$archive" ;; *.zip) unzip "$archive" ;; *.z) uncompress "$archive" ;; *.7z) command -v 7z >/dev/null && 7z x "$archive" || echo "7z not installed" ;; *.xz) unxz "$archive" ;; *) echo "Unknown archive format: '$archive'" ;; esac else echo "File not found: '$archive'" fi done } # Text search in files ftext() { if command -v rg >/dev/null; then rg -i "$1" . else grep -iIHrn --color=always "$1" . 2>/dev/null | less -r fi } # Copy/move and navigate cpg() { if [[ -d $2 ]]; then cp "$1" "$2" && cd "$2" else cp "$1" "$2" fi } mvg() { if [[ -d $2 ]]; then mv "$1" "$2" && cd "$2" else mv "$1" "$2" fi } # Create directory and navigate mkdirg() { mkdir -p "$1" && cd "$1" } # Navigate up directories up() { local d="" local limit=${1:-1} for ((i = 1; i <= limit; i++)); do d="$d/.." done cd "${d:2:${#d}}" || cd .. } # Distribution detection distribution() { if [[ -r /etc/os-release ]]; then source /etc/os-release case $ID in fedora | rhel | centos | rocky | almalinux) echo "redhat" ;; sles | opensuse*) echo "suse" ;; ubuntu | debian) echo "debian" ;; gentoo) echo "gentoo" ;; arch | manjaro | endeavouros) echo "arch" ;; slackware) echo "slackware" ;; alpine) echo "alpine" ;; *) case ${ID_LIKE:-} in *fedora* | *rhel* | *centos*) echo "redhat" ;; *suse*) echo "suse" ;; *ubuntu* | *debian*) echo "debian" ;; *gentoo*) echo "gentoo" ;; *arch*) echo "arch" ;; *) echo "unknown" ;; esac ;; esac elif [[ -f /etc/redhat-release ]]; then echo "redhat" elif [[ -f /etc/debian_version ]]; then echo "debian" elif [[ -f /etc/arch-release ]]; then echo "arch" else echo "unknown" fi } # Version information ver() { local dtype=$(distribution) case $dtype in "redhat") [[ -r /etc/redhat-release ]] && cat /etc/redhat-release || cat /etc/os-release ;; "suse") cat /etc/os-release 2>/dev/null || cat /etc/SuSE-release 2>/dev/null ;; "debian") command -v lsb_release >/dev/null && lsb_release -a || cat /etc/os-release ;; "gentoo") cat /etc/gentoo-release 2>/dev/null || cat /etc/os-release ;; "arch") cat /etc/os-release ;; "alpine") cat /etc/alpine-release 2>/dev/null || cat /etc/os-release ;; *) cat /etc/os-release 2>/dev/null || echo "Unknown distribution" ;; esac echo -e "\nKernel: $(uname -sr)" } # Package installation helper install_zshrc_support() { local dtype=$(distribution) case "$dtype" in "redhat") if command -v dnf &>/dev/null; then sudo dnf install tree zoxide fzf bash-completion fastfetch eza bat ripgrep alacritty else sudo yum install tree zoxide fzf bash-completion fastfetch eza bat ripgrep alacritty fi ;; "suse") sudo zypper install tree zoxide fzf bash-completion fastfetch eza bat ripgrep alacritty ;; "debian") sudo apt-get update sudo apt-get install tree zoxide fzf bash-completion eza bat ripgrep alacritty local FASTFETCH_URL FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | grep "browser_download_url.*linux-amd64.deb" | cut -d '"' -f 4) if [ -n "$FASTFETCH_URL" ]; then curl -sL "$FASTFETCH_URL" -o /tmp/fastfetch_latest_amd64.deb sudo apt-get install /tmp/fastfetch_latest_amd64.deb rm /tmp/fastfetch_latest_amd64.deb else echo "Warning: Could not find fastfetch .deb URL." >&2 fi ;; "arch") if command -v yay &>/dev/null; then yay -S --needed zoxide fzf fastfetch starship eza bat ripgrep ttf-firacode-nerd trash-cli fd yazi tlrc-bin alacritty elif command -v pacman &>/dev/null; then sudo pacman -S --needed zoxide fzf fastfetch starship eza bat ripgrep ttf-firacode-nerd trash-cli fd yazi alacritty else echo "Neither yay nor pacman found. Cannot install." >&2 fi ;; "slackware") echo "No install support for Slackware" >&2 ;; *) echo "Unknown distribution. Cannot install." >&2 ;; esac } install_i3_support() { local dtype dtype=$(distribution) case "$dtype" in "redhat" | "debian" | "suse") echo "i3 support installation not yet implemented for $dtype-based systems." >&2 ;; "arch") if command -v yay &>/dev/null; then yay -S --needed --noconfirm picom feh i3-auto-tiling rofi flameshot xclip alacritty elif command -v pacman &>/dev/null; then sudo pacman -S --needed --noconfirm picom feh rofi flameshot xclip alacritty echo "⚠️ 'i3-auto-tiling' is not in official repos; skipping." >&2 else echo "❌ Neither yay nor pacman found. Cannot install." >&2 fi ;; "slackware") echo "Slackware is not supported for automated i3 setup." >&2 ;; *) echo "Unknown distribution: $dtype. Cannot install i3 support." >&2 ;; esac } ####################################################### # ZSH COMPLETION SYSTEM ####################################################### autoload -Uz compinit zcompdump="${XDG_CACHE_HOME:-$HOME/.cache}/.zcompdump" [[ -d "${zcompdump%/*}" ]] || mkdir -p "${zcompdump%/*}" if [[ -f $zcompdump && ! -w $zcompdump ]]; then echo "⚠️ Warning: $zcompdump is not writable, skipping compinit caching." compinit -i elif [[ ! -f $zcompdump || $zcompdump -ot ${ZDOTDIR:-$HOME}/.zshrc ]]; then compinit -C else compinit fi # Completion settings zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' zstyle ':completion:*' menu select zstyle ':completion:*:descriptions' format '%B%d%b' zstyle ':completion:*' special-dirs true zstyle ':completion::complete:*' special-dirs false ####################################################### # KEYBINDINGS AND INTERACTIVE FEATURES ####################################################### # History search bindkey '^R' history-incremental-search-backward # Arrow key history search autoload -Uz up-line-or-beginning-search down-line-or-beginning-search zle -N up-line-or-beginning-search zle -N down-line-or-beginning-search bindkey '^[[A' up-line-or-beginning-search bindkey '^[[B' down-line-or-beginning-search bindkey '^[[1;5D' backward-word bindkey '^[[1;5C' forward-word bindkey '^H' backward-kill-word # Disable XON/XOFF flow control (Ctrl+S/Ctrl+Q) stty -ixon 2>/dev/null # Zoxide integration command -v zoxide >/dev/null && bindkey -s '\C-f' 'zi^M' # Auto-ls after cd autoload -Uz add-zsh-hook _auto_ls() { [[ -r . ]] && ls } add-zsh-hook chpwd _auto_ls ####################################################### # EXTERNAL TOOL INITIALIZATION ####################################################### # Initialize tools if available command -v starship >/dev/null && eval "$(starship init zsh)" command -v zoxide >/dev/null && eval "$(zoxide init zsh)" ####################################################### # YAZI ####################################################### if command -v yazi &>/dev/null; then function y() { local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd yazi "$@" --cwd-file="$tmp" IFS= read -r -d '' cwd <"$tmp" [ -n "$cwd" ] && [ "$cwd" != "$PWD" ] && builtin cd -- "$cwd" rm -f -- "$tmp" } fi ####################################################### # ONEFETCH ####################################################### if command -v onefetch >/dev/null 2>&1; then typeset -g last_repository="" _get_git_root() { git rev-parse --show-toplevel 2>/dev/null || true } _show_repo_greeting_if_needed() { local current_repository current_repository="$(_get_git_root)" if [ -n "$current_repository" ] && [ "$current_repository" != "$last_repository" ]; then onefetch -d churn --no-merges --nerd-fonts fi last_repository="$current_repository" } autoload -Uz add-zsh-hook 2>/dev/null || true if typeset -f add-zsh-hook >/dev/null 2>&1; then add-zsh-hook chpwd _show_repo_greeting_if_needed else chpwd() { _show_repo_greeting_if_needed } fi # Optional: run once now (comment out to avoid startup cost) # _show_repo_greeting_if_needed fi ####################################################### # FZF integration ####################################################### # CTRL-T - Fuzzy find files # CTRL-R - Fuzzy reverse search through history # ALT-C - Fuzzy cd into subdirectories if command -v fzf >/dev/null; then if eval "$(fzf --zsh)" 2>/dev/null; then : elif [[ -f ~/.fzf.zsh ]]; then source ~/.fzf.zsh elif [[ -f /usr/share/fzf/key-bindings.zsh ]]; then source /usr/share/fzf/key-bindings.zsh source /usr/share/fzf/completion.zsh 2>/dev/null fi fi ## replace find with fd if available for fzf # fzf and fd integration if command -v fzf &>/dev/null && command -v fd &>/dev/null; then export FZF_DEFAULT_COMMAND="fd --type f --hidden --exclude .git --color=never" export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" export FZF_ALT_C_COMMAND="fd --type d --hidden --exclude .git --color=never" fi ####################################################### # STARTUP ####################################################### command -v fastfetch >/dev/null && fastfetch # Add Rust's cargo bin directory to PATH if it exists [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env" ####################################################### # GO SETUP ####################################################### GO_FOUND="" if command -v go >/dev/null 2>&1; then GO_FOUND="yes" elif [ -x "$HOME/go/bin/go" ]; then export PATH="$HOME/go/bin:$PATH" GO_FOUND="yes" elif [ -x "/usr/local/go/bin/go" ]; then export PATH="/usr/local/go/bin:$PATH" GO_FOUND="yes" fi if [ "$GO_FOUND" = "yes" ]; then export GOPATH="${GOPATH:-$HOME/go}" [ -d "$GOPATH/bin" ] && export PATH="$PATH:$GOPATH/bin" fi # Add NVM and its binaries to Path if available export NVM_DIR="$HOME/.config/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" [ -d "$HOME/flutter/bin" ] && export PATH="$HOME/flutter/bin:$PATH" # Set ANDROID_HOME if the SDK directory exists if [ -d "$HOME/Android/Sdk" ]; then export ANDROID_HOME="$HOME/Android/Sdk" # Add platform-tools and cmdline-tools to PATH if they exist if [ -d "$ANDROID_HOME/platform-tools" ]; then export PATH="$ANDROID_HOME/platform-tools:$PATH" fi if [ -d "$ANDROID_HOME/cmdline-tools/latest/bin" ]; then export PATH="$ANDROID_HOME/cmdline-tools/latest/bin:$PATH" fi fi