Last active
November 7, 2024 23:20
-
-
Save k4yt3x/3b41a1a65f5d3087133e449793eb8858 to your computer and use it in GitHub Desktop.
K4YT3X Server/Community zshrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# K4YT3X Server/Community Zshrc | |
# Version: 2024.11.07 | |
# Copyright (C) 2017-2024 K4YT3X. | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
# Gist Page: https://gist.github.com/k4yt3x/3b41a1a65f5d3087133e449793eb8858 | |
# Short URL: kt.ax/z | |
# Quick Install: curl -L kt.ax/z -o ~/.zshrc && zsh | |
# Requires: cURL/Wget, Git, Zsh | |
# =========================== Environment Preparation | |
# automatically start tmux | |
#if command -v tmux &> /dev/null && \ | |
# [[ -o interactive ]] && \ | |
# [[ ! "$TERM" =~ screen ]] && \ | |
# [[ ! "$TERM" =~ tmux ]] && \ | |
# [ -z "$TMUX" ] && \ | |
# [ -z "$SSH_CONNECTION" ]; then | |
# exec tmux | |
#fi | |
# enable Powerlevel10k instant prompt | |
# this section should stay close to the top of the ~/.zshrc file | |
instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" | |
[[ -r "$instant_prompt" ]] && source "$instant_prompt" | |
# =========================== Global Variables | |
# set terminal time zone | |
export TZ=UTC | |
# ensure standard PATH locations are all included | |
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" | |
export PATH="$HOME/.local/bin:$PATH" | |
# binaries compiled by Cargo (Rust) | |
export PATH="$HOME/.cargo/bin:$PATH" | |
# binaries compiled by Go | |
export PATH="$HOME/.local/share/go/bin:$PATH" | |
# setting GOPATH for Golang | |
export GOPATH="$HOME/.local/share/go" | |
# specify the SSH agent socket path | |
#export SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh" | |
# disable less history | |
export LESSHISTFILE='-' | |
# custom Python settings | |
export PYTHONDONTWRITEBYTECODE=1 | |
export PYTHONPYCACHEPREFIX="$HOME/.cache/pycache" | |
[ -f "$HOME/.pythonrc" ] && export PYTHONSTARTUP=~/.pythonrc | |
# skip the rest for a non-interactive shell | |
[[ $- != *i* ]] && return | |
# =========================== Interactive Shell Begin | |
# fail-over minimal command prompt | |
export PS1='%n@%m:%~%# ' | |
# set the time zone code according to the military time zone codes standard | |
declare -A MILITARY_TIME_ZONES | |
MILITARY_TIME_ZONES=( | |
['+01']='A' ['+02']='B' ['+03']='C' ['+04']='D' ['+05']='E' ['+06']='F' | |
['+07']='G' ['+08']='H' ['+09']='I' ['+10']='K' ['+11']='L' ['+12']='M' | |
['-01']='N' ['-02']='O' ['-03']='P' ['-04']='Q' ['-05']='R' ['-06']='S' | |
['-07']='T' ['-08']='U' ['-09']='V' ['-10']='W' ['-11']='X' ['-12']='Y' | |
['+00']='Z' | |
) | |
TIMEZONE_CODE=$MILITARY_TIME_ZONES[$(date '+%:::z')] | |
[ "$TIMEZONE_CODE" = '' ] && TIMEZONE_CODE='J' | |
# enable 256 color & true color support | |
# commented to use the terminal-provided TERM value | |
#export TERM='xterm-256color' | |
# if the current directory is a Git repository, return the current branch's name | |
function __format_git_branch() { | |
branch="$(git branch --show-current)" &>/dev/null | |
[ $? -eq 0 ] && echo " %F{blue}(%F{white}%%$branch%F{blue})%F{white}" | |
} | |
# styled command prompt | |
if [[ $UID == 0 || $EUID == 0 ]]; then | |
# blue for root | |
export PS1="%F{240}[%D{%H%M:%S}$TIMEZONE_CODE] \ | |
%F{33}%n%F{white}%#%B%m%b%F{blue}[%~]%F{white}>\$(__format_git_branch) " | |
else | |
# yellow for other users | |
export PS1="%F{240}[%D{%H%M:%S}$TIMEZONE_CODE] \ | |
%F{11}%n%F{white}%#%B%m%b%F{yellow}[%~]%F{white}\$(__format_git_branch)> " | |
fi | |
# automatically set default editor | |
export EDITOR=$(command -v nvim vim vi helix hx nano | head -1) | |
# WSL doesn't work well with BG_NICE | |
[ -d '/mnt/c' ] && [[ "$(uname -a)" == *Microsoft* ]] && unsetopt BG_NICE | |
# =========================== Initialize Antigen and Plugins | |
# partially from Skywind3000's zshrc | |
# original code: https://github.com/skywind3000/vim/blob/master/etc/zshrc.zsh | |
function install_antigen() { | |
# Antigen: https://github.com/zsh-users/antigen | |
ANTIGEN_URL='https://git.io/antigen' | |
ANTIGEN="$HOME/.local/bin/antigen.zsh" | |
ANTIGEN_TEMP='/tmp/antigen.zsh' | |
# install antigen.zsh if not exist | |
if [ ! -f "$ANTIGEN" ]; then | |
echo "installing Antigen to $ANTIGEN" | |
[ ! -d "$HOME/.local" ] && mkdir -p "$HOME/.local" | |
[ ! -d "$HOME/.local/bin" ] && mkdir -p "$HOME/.local/bin" | |
[ ! -f "$HOME/.z" ] && touch "$HOME/.z" | |
# automatically select an available downloader | |
if command -v curl &>/dev/null; then | |
curl -sSL "$ANTIGEN_URL" -o "$ANTIGEN_TEMP" | |
elif command -v wget &>/dev/null; then | |
wget -q "$ANTIGEN_URL" -O "$ANTIGEN_TEMP" | |
else | |
>&2 echo 'ERROR: Neither cURL nor Wget found' | |
return 1 | |
fi | |
# if download has failed | |
if [ ! $? -eq 0 ]; then | |
>&2 echo "\nERROR: Failed to download antigen.zsh ($ANTIGEN_URL)" | |
return 1 | |
else | |
mv "$ANTIGEN_TEMP" "$ANTIGEN" | |
fi | |
fi | |
if [ -f "$ANTIGEN" ] && [ ! -d "$HOME/.antigen" ]; then | |
if ! command -v git &>/dev/null; then | |
>&2 echo 'ERROR: Git not found' | |
return 1 | |
fi | |
fi | |
} | |
install_antigen | |
if [ ! $? -eq 0 ]; then | |
>&2 echo 'unable to install Antigen' | |
fi | |
# initialize antigen | |
source "$ANTIGEN" | |
# disable URL formatting | |
DISABLE_MAGIC_FUNCTIONS=true | |
# initialize oh-my-zsh | |
antigen use oh-my-zsh | |
# load Zsh bundles/plugins with Antigen | |
# visit https://github.com/unixorn/awesome-zsh-plugins | |
#antigen bundle command-not-find | |
#antigen bundle rupa/z z.sh | |
#antigen bundle supercrabtree/k | |
#antigen bundle svn-fast-info | |
#antigen bundle z | |
antigen bundle agkozak/zsh-z | |
antigen bundle colorize | |
antigen bundle extract | |
antigen bundle git | |
antigen bundle github | |
antigen bundle kubectl | |
antigen bundle python | |
antigen bundle rust | |
antigen bundle zsh-users/zsh-autosuggestions | |
antigen bundle zsh-users/zsh-completions | |
antigen bundle zsh-users/zsh-syntax-highlighting | |
# ================= agkozak/zsh-z Configuration | |
# change the default database path | |
ZSHZ_DATA="$HOME/.cache/zshz" | |
# ================= powerlevel10k Configuration | |
# enable instant prompt in quiet mode | |
POWERLEVEL9K_INSTANT_PROMPT=quiet | |
# default icon mode | |
POWERLEVEL9K_MODE='ascii' | |
# military time | |
POWERLEVEL9K_TIME_FORMAT="%D{%H%M}$TIMEZONE_CODE" | |
#POWERLEVEL9K_TIME_FORMAT="%D{%H%M:%S}$TIMEZONE_CODE" | |
#POWERLEVEL9K_TIME_FORMAT="%D{%d%H%M$TIMEZONE_CODE%^b%y}" | |
# status segment color | |
POWERLEVEL9K_STATUS_OK_FOREGROUND=245 | |
POWERLEVEL9K_STATUS_OK_ICON= | |
# time segment color | |
POWERLEVEL9K_TIME_FOREGROUND=52 | |
# user segment color | |
POWERLEVEL9K_USER_FOREGROUND=3 | |
# set special blue color if the user is root | |
if [ "$(id -u)" -eq 0 ]; then | |
POWERLEVEL9K_USER_FOREGROUND=4 | |
fi | |
# host segment color | |
POWERLEVEL9K_HOST_FOREGROUND=229 | |
# directory segment color | |
POWERLEVEL9K_DIR_BACKGROUND=235 | |
POWERLEVEL9K_DIR_FOREGROUND=243 | |
POWERLEVEL9K_DIR_ANCHOR_FOREGROUND=7 | |
# shorten long paths | |
POWERLEVEL9K_DIR_SHORTENED_FOREGROUND=109 | |
POWERLEVEL9K_SHORTEN_DELIMITER= | |
POWERLEVEL9K_SHORTEN_DIR_LENGTH=1 | |
POWERLEVEL9K_SHORTEN_FOLDER_MARKER=first | |
POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique | |
# version control system (git, svn, etc.) color | |
POWERLEVEL9K_VCS_CLEAN_FOREGROUND=4 | |
POWERLEVEL9K_VCS_MODIFIED_FOREGROUND=3 | |
POWERLEVEL9K_VCS_UNTRACKED_FOREGROUND=2 | |
# VCS status icons | |
POWERLEVEL9K_VCS_BOOKMARK_ICON='^' | |
POWERLEVEL9K_VCS_BRANCH_ICON='%% ' | |
POWERLEVEL9K_VCS_COMMIT_ICON='#' | |
POWERLEVEL9K_VCS_DIRTY_ICON='*' | |
POWERLEVEL9K_VCS_INCOMING_CHANGES_ICON='<' | |
POWERLEVEL9K_VCS_OUTGOING_CHANGES_ICON='>' | |
POWERLEVEL9K_VCS_REMOTE_BRANCH_ICON='>%% ' | |
POWERLEVEL9K_VCS_STAGED_ICON='$' | |
POWERLEVEL9K_VCS_STASH_ICON='@' | |
POWERLEVEL9K_VCS_UNSTAGED_ICON='+' | |
POWERLEVEL9K_VCS_UNTRACKED_ICON='?' | |
# python virtualenv | |
POWERLEVEL9K_VIRTUALENV_FOREGROUND=178 | |
POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=false | |
# vi-mode color | |
POWERLEVEL9K_VI_MODE_INSERT_FOREGROUND=234 | |
POWERLEVEL9K_VI_MODE_NORMAL_FOREGROUND=234 | |
# left prompt segments | |
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv background_jobs time user host dir_writable dir vcs status prompt_char) | |
# make background color transparent | |
unset -m 'POWERLEVEL9K_*_BACKGROUND' | |
typeset -g POWERLEVEL9K_BACKGROUND= | |
# remove unwanted segment separators | |
POWERLEVEL9K_LEFT_SEGMENT_END_SEPARATOR=' ' | |
POWERLEVEL9K_LEFT_SEGMENT_SEPARATOR= | |
POWERLEVEL9K_RIGHT_SEGMENT_SEPARATOR= | |
typeset -g POWERLEVEL9K_{LEFT,RIGHT}_{LEFT,RIGHT}_WHITESPACE= | |
typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SUBSEGMENT_SEPARATOR=' ' | |
# right prompt segments | |
POWERLEVEL9K_DISABLE_RPROMPT=true | |
#POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(ram disk_usage kubecontext battery) | |
# activate theme | |
antigen theme romkatv/powerlevel10k powerlevel10k | |
# ================= Zsh Built-in Features Configuration | |
# syntax color definition | |
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern) | |
# Zsh syntax highlighting styling | |
typeset -A ZSH_HIGHLIGHT_STYLES | |
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=009 | |
ZSH_HIGHLIGHT_STYLES[reserved-word]=fg=004,standout | |
ZSH_HIGHLIGHT_STYLES[alias]=fg=cyan,bold | |
ZSH_HIGHLIGHT_STYLES[suffix-alias]=fg=green,underline | |
ZSH_HIGHLIGHT_STYLES[global-alias]=fg=magenta | |
ZSH_HIGHLIGHT_STYLES[builtin]=fg=013,bold | |
ZSH_HIGHLIGHT_STYLES[function]=fg=012,bold | |
ZSH_HIGHLIGHT_STYLES[command]=fg=white,bold | |
ZSH_HIGHLIGHT_STYLES[precommand]=fg=white,underline | |
ZSH_HIGHLIGHT_STYLES[commandseparator]=fg=blue,bold | |
ZSH_HIGHLIGHT_STYLES[hashed-command]=fg=009 | |
ZSH_HIGHLIGHT_STYLES[autodirectory]=fg=green,underline | |
ZSH_HIGHLIGHT_STYLES[path]=fg=214,underline | |
#ZSH_HIGHLIGHT_STYLES[path_pathseparator]= | |
#ZSH_HIGHLIGHT_STYLES[path_prefix]= | |
#ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]= | |
ZSH_HIGHLIGHT_STYLES[globbing]=fg=063 | |
ZSH_HIGHLIGHT_STYLES[history-expansion]=fg=white,underline | |
#ZSH_HIGHLIGHT_STYLES[command-substitution]= | |
#ZSH_HIGHLIGHT_STYLES[command-substitution-unquoted]= | |
#ZSH_HIGHLIGHT_STYLES[command-substitution-quoted]= | |
ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter]=fg=magenta | |
#ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter-unquoted]= | |
#ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter-quoted]= | |
#ZSH_HIGHLIGHT_STYLES[process-substitution]= | |
ZSH_HIGHLIGHT_STYLES[process-substitution-delimiter]=fg=magenta | |
#ZSH_HIGHLIGHT_STYLES[arithmetic-expansion]= | |
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=fg=magenta | |
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=fg=magenta | |
#ZSH_HIGHLIGHT_STYLES[back-quoted-argument]= | |
#ZSH_HIGHLIGHT_STYLES[back-quoted-argument-unclosed]= | |
ZSH_HIGHLIGHT_STYLES[back-quoted-argument-delimiter]=fg=blue,bold | |
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]=fg=blue | |
#ZSH_HIGHLIGHT_STYLES[single-quoted-argument-unclosed]= | |
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]=fg=063 | |
#ZSH_HIGHLIGHT_STYLES[double-quoted-argument-unclosed]= | |
ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]=fg=yellow | |
#ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument-unclosed]= | |
ZSH_HIGHLIGHT_STYLES[rc-quote]=fg=magenta | |
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]=fg=009 | |
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]=fg=009 | |
ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=fg=magenta | |
#ZSH_HIGHLIGHT_STYLES[assign]= | |
ZSH_HIGHLIGHT_STYLES[redirection]=fg=blue,bold | |
ZSH_HIGHLIGHT_STYLES[comment]=fg=black,bold | |
ZSH_HIGHLIGHT_STYLES[comment]=fg=black,bold | |
#ZSH_HIGHLIGHT_STYLES[named-fd]= | |
#ZSH_HIGHLIGHT_STYLES[numeric-fd]= | |
ZSH_HIGHLIGHT_STYLES[arg0]=fg=green | |
#ZSH_HIGHLIGHT_STYLES[default]= | |
# brackets | |
ZSH_HIGHLIGHT_STYLES[bracket-error]=fg=red,bold | |
ZSH_HIGHLIGHT_STYLES[bracket-level-1]=fg=yellow,bold | |
ZSH_HIGHLIGHT_STYLES[bracket-level-2]=fg=blue,bold | |
ZSH_HIGHLIGHT_STYLES[bracket-level-3]=fg=green,bold | |
ZSH_HIGHLIGHT_STYLES[bracket-level-4]=fg=magenta,bold | |
ZSH_HIGHLIGHT_STYLES[bracket-level-5]=fg=cyan,bold | |
ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]=standout | |
# patterns | |
typeset -A ZSH_HIGHLIGHT_PATTERNS | |
ZSH_HIGHLIGHT_PATTERNS+=('mkfs' 'fg=red,standout') | |
ZSH_HIGHLIGHT_PATTERNS+=('rm -rf' 'fg=red,standout') | |
# miscellaneous options | |
COMPLETION_WAITING_DOTS='true' | |
HIST_STAMPS='mm/dd/yyyy' | |
HISTSIZE=2147483647 | |
SAVEHIST=$HISTSIZE | |
# load login shell configs | |
if [[ -o login ]]; then | |
[ -f "$HOME/.local/etc/login.sh" ] && source "$HOME/.local/etc/login.sh" | |
[ -f "$HOME/.local/etc/login.zsh" ] && source "$HOME/.local/etc/login.zsh" | |
fi | |
# load local configs | |
[ -f "$HOME/.local/etc/config.zsh" ] && source "$HOME/.local/etc/config.zsh" | |
[ -f "$HOME/.local/etc/local.zsh" ] && source "$HOME/.local/etc/local.zsh" | |
antigen apply | |
# load Zsh bundles/themes not managed by Antigen | |
[ -f '/usr/share/fzf/key-bindings.zsh' ] && source '/usr/share/fzf/key-bindings.zsh' | |
[ -f '/usr/share/fzf/completion.zsh' ] && source '/usr/share/fzf/completion.zsh' | |
# initialize compinit for compdef | |
autoload -U +X compinit && compinit | |
autoload -U +X bashcompinit && bashcompinit | |
# enable zle vim mode | |
#bindkey -v | |
# enable zle emacs mode | |
bindkey -e | |
# default keymap | |
bindkey '\eh' backward-char | |
bindkey '\el' forward-char | |
bindkey '\ej' down-line-or-search | |
bindkey '\ek' up-line-or-search | |
bindkey '\eH' backward-word | |
bindkey '\eL' forward-word | |
bindkey '\eJ' beginning-of-line | |
bindkey '\eK' end-of-line | |
# bindkey '\eu' undo | |
# command bindings | |
bindkey -s '\e:' 'll\n' | |
bindkey -s '\e;' 'l\n' | |
bindkey -s '\e?' '**\t' | |
bindkey -s '\ee' "$EDITOR\n" | |
bindkey -s '\ei' 'cd -\n' | |
bindkey -s '\eo' 'cd ..\n' | |
bindkey -s '\er' 'exec /usr/bin/zsh\n' | |
# options | |
unsetopt correct_all | |
setopt BANG_HIST # treat the '!' character specially during expansion. | |
setopt HIST_EXPIRE_DUPS_FIRST # expire duplicate entries first when trimming history. | |
setopt HIST_FIND_NO_DUPS # do not display a line previously found. | |
setopt HIST_IGNORE_ALL_DUPS # delete old recorded entry if new entry is a duplicate. | |
setopt HIST_IGNORE_DUPS # don't record an entry that was just recorded again. | |
setopt HIST_IGNORE_SPACE # don't record an entry starting with a space. | |
setopt HIST_REDUCE_BLANKS # remove superfluous blanks before recording entry. | |
setopt HIST_SAVE_NO_DUPS # don't write duplicate entries in the history file. | |
setopt HIST_VERIFY # don't execute immediately upon history expansion. | |
setopt INC_APPEND_HISTORY # write to the history file immediately, not when the shell exits. | |
setopt SHARE_HISTORY # share history between all sessions. | |
#setopt complete_aliases # let zsh-completions complete aliases | |
# ignore completion | |
zstyle ':completion:*:complete:-command-:*:*' ignored-patterns '*.pdf|*.exe|*.dll' | |
zstyle ':completion:*:*sh:*:' tag-order files | |
# Kali Zsh completion styles | |
zstyle ':completion:*:*:*:*:*' menu select | |
zstyle ':completion:*' auto-description 'specify: %d' | |
#zstyle ':completion:*' format 'completing %d' | |
zstyle ':completion:*' group-name '' | |
zstyle ':completion:*' list-colors '' | |
zstyle ':completion:*' list-prompt %SAt %p: hit TAB for more, or the character to insert%s | |
zstyle ':completion:*' rehash true | |
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s | |
zstyle ':completion:*' use-compctl false | |
zstyle ':completion:*' verbose true | |
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' | |
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" | |
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' | |
# =========================== Aliases | |
# enhance and preserve alias | |
alias dw0='watch -n0 -d -c ' | |
alias dw1='watch -n1 -d -c ' | |
alias dw='watch -d -c ' | |
alias sudo='sudo ' | |
alias doas='doas ' | |
# commands with patched envvars | |
alias act='sudo DOCKER_HOST=unix:///run/podman/podman.sock act' | |
alias mk='MINIKUBE_IN_STYLE=0 minikube' | |
# enhanced/shortened built-in commands | |
alias abs='realpath -e' | |
alias c='clear' | |
alias cl='cd && clear' | |
alias g="$(command -v rg grep | head -1)" | |
alias gcco='gcc -Wall -march=native -Ofast -flto' | |
alias gi='g -i' | |
alias nc="$(command -v ncat nc | head -1)" | |
alias o='gio open' | |
alias own='chown -R $USER:' | |
alias psf='ps -ef | grep' | |
alias r2='r2 -AA -e bin.relocs.apply=true' | |
alias reload='exec /usr/bin/zsh' | |
alias rm='rm -I' | |
alias sizes='du -sh * | sort -h' | |
alias wh="$(command -v wormhole-rs wormhole | head -1)" | |
function cat() { command -v bat &>/dev/null && bat -pP $@ || command cat $@ } | |
function hl() { grep --color=always -E "$1|$" } | |
# file listing | |
if command -v lsd &>/dev/null; then | |
alias ls='lsd --icon=never --group-dirs=first --size=short -Fv' | |
alias l='ls -lh --date="+%d%b%y" --blocks permission,user,group,size,date,git,name --no-symlink' | |
alias ll='ls -lhaZ --date="+%d%b%y %H%M:%S" --blocks permission,user,group,size,date,git,name --size=bytes' | |
alias tree='ls --tree' | |
alias t='l --tree' | |
alias tt='ll --tree' | |
else | |
alias l='ls -lh' | |
alias ll='ls -lha' | |
fi | |
# Git | |
alias git="GIT_COMMITTER_DATE=\"\$(date -u --date='00:00:00')\" GIT_AUTHOR_DATE=\"\$(date -u --date='00:00:00')\" git" | |
alias gbp!='git fetch --all -p && git branch -vv | awk "/^[^*]/ && /: gone]/{print \$1}" | xargs git branch -D' | |
alias gbp='git fetch --all -p && git branch -vv | awk "/^[^*]/ && /: gone]/{print \$1}" | xargs git branch -d' | |
alias gc='git commit -S -s --verbose' | |
alias gce='git commit --allow-empty-message -m ""' | |
alias gch='git checkout HEAD --' | |
alias gcln='git clean -ffxd' | |
alias gcm='git commit -S -s -m' | |
alias gcms='git commit -S -m' | |
alias gdd='git difftool' | |
alias gdh='git diff HEAD^ HEAD' | |
alias glgl="git log --graph --abbrev-commit --decorate\ | |
--format=format:'%C(bold blue)%h%C(reset)%C(bold brightblack)(%G?)%C(reset) -\ | |
%C(bold green)(%as)%C(reset) %C(white)%s%C(reset) %C(dim white)-\ | |
%an%C(reset)%C(bold yellow)%d%C(reset)'" | |
alias glgla='glgl --all' | |
alias glgls='glgl --stat' | |
alias glglsa='glgls --all' | |
alias gll='find . -maxdepth 2 -type d -name .git -printf "Pulling: %p\n" -exec\ | |
git --git-dir={} --work-tree=$PWD/{}/.. pull \;' | |
alias gls='git log --show-signature' | |
alias grao='git remote add origin' | |
alias grau='git remote add upstream' | |
alias grr='git remote remove' | |
alias grso='git remote set-url origin' | |
alias grsu='git remote set-url upstream' | |
alias gvd='git difftool --tool=nvimdiff --no-prompt' | |
function gdtg() { git tag -d "$1" && git push --delete origin "$1" } | |
# Rust | |
alias cg='cargo' | |
alias cgb='cargo build' | |
alias cgbr='cgb --release' | |
alias cgbg='cargo build --target x86_64-unknown-linux-gnu' | |
alias cgbrg='cgbg --release' | |
alias cgbm='cargo build --target x86_64-unknown-linux-musl' | |
alias cgbrm='cgbm --release' | |
alias cgr='cargo run' | |
alias cgrr='cgr --release' | |
alias cgrg='cargo run --target x86_64-unknown-linux-gnu' | |
alias cgrrg='cgrg --release' | |
alias cgrm='cargo run --target x86_64-unknown-linux-musl' | |
alias cgrrm='cgrm --release' | |
alias cgk='cargo check' | |
alias cgc='cargo clean' | |
alias cgd='cargo doc' | |
alias cgn='cargo new' | |
alias cgi='cargo init' | |
alias cgt='cargo test' | |
alias cgtn='cargo test -- --nocapture' | |
alias cgbn='cargo bench' | |
alias cgu='cargo update' | |
alias cgs='cargo search' | |
alias cgp='cargo publish' | |
alias cgin='cargo install' | |
alias cgun='cargo uninstall' | |
# Python | |
#alias py="$(command -v bpython btpython python python3 python2 | head -1) -q" | |
#alias py="$(command -v python python3 python2 | head -1) -q" | |
alias py='python -q' | |
alias py2='python2 -q' | |
alias py3='python3 -q' | |
alias pybs='python -qm build -s .' | |
alias pybw='python -qm build -w .' | |
alias pydbg='py -m pdb -c continue' | |
alias pyhttp='py -m http.server' | |
alias pyjson='py -m json.tool' | |
alias pym='python -qm' | |
alias vpy='.venv/bin/python -q' | |
alias vpym='.venv/bin/python -qm' | |
alias pipx='USE_EMOJI=false pipx' | |
# container CLI tools | |
alias ct="sudo $(command -v podman docker nerdctl | head -1)" | |
alias ctc='ct ps -a --external' | |
alias cti='ct images' | |
alias ctprune='ct rm -f $(ct ps -aq --external); ct system prune -af --volumes' | |
alias ctt='ct run -it --rm -h=temp --name=temp' | |
alias cttv='ct run -it --rm -h=temp --name=temp -v $PWD:/host' | |
# kubectl shortcuts | |
alias kak.='kubectl apply -k .' | |
alias kak='kubectl apply -k' | |
alias kdj='kubectl describe jobs' | |
alias kgj='kubectl get jobs' | |
alias kk.='kubectl kustomize .' | |
alias kk='kubectl kustomize' | |
alias kns='kubectl config set-context --current --namespace' | |
# systemctl shortcuts | |
alias sys='systemctl' | |
alias start='sudo systemctl start' | |
alias stop='sudo systemctl stop' | |
alias restart='sudo systemctl restart' | |
alias mask='sudo systemctl mask' | |
alias unmask='sudo systemctl unmask' | |
alias status='systemctl status' | |
alias isenabled='systemctl is-enabled' | |
alias enables='sudo systemctl enable' | |
alias disables='sudo systemctl disable' | |
alias daemonreload='sudo systemctl daemon-reload' | |
alias running='systemctl list-units --type=service --state=running' | |
# systemctl user shortcuts | |
alias startu='systemctl start --user' | |
alias stopu='systemctl stop --user' | |
alias statusu='systemctl status --user' | |
alias restartu='systemctl restart --user' | |
alias enablesu='systemctl enable --user' | |
alias disablesu='systemctl disable --user' | |
# shorter systemctl shortcuts | |
alias sdr='sudo systemctl daemon-reload' | |
alias srn='systemctl list-units --type=service --state=running' | |
# journalctl shortcuts | |
alias jf='sudo journalctl -feu' | |
alias jcl='sudo journalctl --rotate && sudo journalctl --vacuum-time=1s' | |
# colored output | |
alias diff='diff --color=auto' | |
alias dir='dir --color=auto' | |
alias egrep='egrep --color' | |
alias egrep='egrep --color=auto' | |
alias fgrep='fgrep --color=auto' | |
alias grep='egrep --color' | |
alias grep='grep --color=auto' | |
alias ip='ip --color=auto' | |
#alias ls='ls --color=auto' | |
alias vdir='vdir --color=auto' | |
# verbose output | |
#alias chgrp='chgrp -v' | |
#alias chmod='chmod -v' | |
#alias chown='chown -v' | |
#alias cp='cp -v' | |
#alias dhclient='dhclient -v' | |
#alias ln='ln -v' | |
#alias mkdir='mkdir -v' | |
#alias mtr4='mtr -4' | |
#alias mtr6='mtr -6' | |
#alias mv='mv -v' | |
#alias ping4='ping4 -O' | |
#alias ping6='ping6 -O' | |
#alias ping='ping -O' | |
#alias rm='rm -v' | |
#alias scp='scp -v' | |
#alias sftp='sftp -v' | |
#alias ssh='ssh -v' | |
# quiet output | |
alias ffmpeg='ffmpeg -hide_banner' | |
# editor alias | |
[ ! -z "$EDITOR" ] && alias v="$EDITOR" | |
# express edit | |
alias vi3='v ~/.config/i3/config' | |
alias vic='v ~/.config/nvim/coc-settings.json' | |
alias vih='v ~/.zsh_history' | |
alias vio='sudo v /etc/hosts' | |
alias vir='sudo v /etc/resolv.conf' | |
alias vis='v ~/.ssh/config' | |
alias vit='v ~/.tmux.conf' | |
alias viv='v ~/.config/nvim/init.lua' | |
alias viz='v ~/.zshrc' | |
# vim/nvim only | |
alias va='v -p *' | |
alias vd='v -d' | |
alias vp='v -p' | |
alias vs='[ -f Session.vim ] && v -S || (>&2 echo "No Session.vim file found" && false)' | |
alias vx='vp $(find . -type f -regex ".*\.\(py\|go\|rs\)")' | |
function vf() { [ ! -z "$1" ] && "$EDITOR" -p $(find . -type f -iname "*$1*") } | |
# dpkg shortcuts | |
alias dpkg-list-rc='dpkg -l | grep "^rc"' | |
alias dpkg-purge-rc='dpkg -l | grep "^rc" | awk "{print \$2}" | sudo xargs dpkg --purge' | |
# =========================== Community Zshrc Functions | |
# update the Zshrc and reload Zsh | |
function update-zshrc() { | |
curl -LsSf https://kt.ax/z -o ~/.zshrc | |
grep "Version:" ~/.zshrc | head -1 | awk -F: \ | |
'{gsub(/^[ \t]+/, "", $2); print "Zshrc updated to version: " $2}' | |
exec "$SHELL" | |
} | |
# =========================== Networking Functions | |
# ipinfo fetches and parses current or specified IP address information | |
# version 4.0.0 | |
function ipinfo() { | |
python3 - $@ << EOF | |
import json as j, socket as s, subprocess as sp, sys, argparse as a | |
p=a.ArgumentParser();p.add_argument("-p",default="ipinfo");p.add_argument('h',nargs='?',default='') | |
p.add_argument("-4",action="store_true");p.add_argument("-6",action="store_true");ar=p.parse_args() | |
try:ip=ar.h and s.getaddrinfo(ar.h,None,s.AF_INET6 if ar.__dict__['6'] else s.AF_INET if ar.__dict__['4'] else 0)[0][4][0] | |
except:print('Invalid hostname/IP',file=sys.stderr);sys.exit(1) | |
url={"ipinfo":"https://ipinfo.io/{ip}","ifconfig":"https://ifconfig.co/json?ip={ip}"}[ar.p] | |
cmd=['curl','-sSL',"-6"if ar.__dict__["6"]else "-4"if ar.__dict__["4"]else"",url.format(ip=ip if ip else "")] | |
pr=sp.run([e for e in cmd if e],capture_output=True) | |
if pr.returncode:print(f"curl error {pr.returncode}",pr.stderr.decode().strip(),file=sys.stderr);sys.exit(1) | |
try:r=j.loads(pr.stdout.decode()) | |
except:print("unable to decode JSON",file=sys.stderr);sys.exit(1) | |
[print("{0:15}{1}".format(k.upper(),v))for k,v in r.items()if k.upper()!="README"] | |
EOF | |
} | |
# set new default route | |
function defroute() { | |
if [ -z "$1" ]; then | |
ip r s default | |
else | |
sudo ip r del default | |
sudo ip r add default via "$1" | |
fi | |
} | |
# connect to RDP with xfreerdp and pre-defined settings | |
function rdp() { | |
# check that a hostname has been provided | |
if [ -z "$1" ]; then | |
>&2 echo 'no target host specified' | |
return 1 | |
fi | |
# position $2 is username | |
[ ! -z "$2" ] && username="/u:$2" | |
# position $3 is domain | |
[ ! -z "$3" ] && domain="/d:$3" | |
# if ncat cannot connect to the port | |
if command -v ncat &>/dev/null \ | |
&& ! ncat -z "$1" 3389; then | |
>&2 echo 'unable to connect to the remote host' | |
return 1 | |
fi | |
# interactively get password | |
echo -n 'Password (will not be echoed): ' | |
read -s password | |
# launch xfreerdp | |
xfreerdp3 /dynamic-resolution /bpp:32 /sound /d: +auto-reconnect \ | |
/gdi:hw /rfx /rfx-mode:video /v:$1 $username $domain /p:$password &>/dev/null &! | |
} | |
# display the TLS certificate information for a host | |
function tlsinfo() { | |
# check that a hostname has been provided | |
if [ -z "$1" ]; then | |
>&2 echo 'no target host specified' | |
>&2 echo 'usage: tlsinfo [hostname] [port]' | |
return 1 | |
fi | |
# use 443 as the default port | |
if [ -z "$2" ]; then | |
port='443' | |
else | |
port="$2" | |
fi | |
/usr/bin/openssl s_client -showcerts -connect "$1:$port" </dev/null | |
} | |
# =========================== Package Management Functions | |
# receive keys from key server and save the keys to /etc/apt/trusted.gpg.d/ | |
function apt-import-key() { | |
if [ "$#" -ne 2 ] ; then | |
echo 'usage: apt-import-key key_hash [filename]' | |
echo 'e.g., apt-import-key 7EA0A9C3F273FCD8 docker.gpg' | |
return | |
fi | |
#keyserver='hkp://keys.gnupg.net' | |
keyserver='hkp://keyserver.ubuntu.com:80' | |
keyfile="/etc/apt/trusted.gpg.d/$2" | |
if [ -f "$keyfile" ]; then | |
echo "The target key file ($keyfile) already exists" | |
return 1 | |
else | |
# check if public key is already available locally | |
keyexisted='false' | |
gpg --list-public-keys "$1" > /dev/null 2>&1 | |
if [ "$?" -eq 0 ]; then | |
keyexisted='true' | |
echo 'key already exists in the local keyring' | |
echo 'using the key from the local keyring' | |
# if the key doesn't exist, retrieve the key from the keyserver | |
else | |
echo 'key does not exist in the local keyring' | |
echo "retrieving the key from the key server ($keyserver)" | |
gpg --keyserver="$keyserver" --receive-keys "$1" | |
# if the gpg command failed, show a warning and exit | |
if [ "$?" -ne 0 ]; then | |
echo 'failed to import keys' | |
return 1 | |
fi | |
fi | |
# export the key to the keyfile | |
echo "Exporting key to file ($keyfile)" | |
gpg --export "$1" | sudo tee "$keyfile" > /dev/null | |
# if key didn't exist before import, delete it | |
if [ "$keyexisted" = 'false' ]; then | |
echo "Deleting temporarily imported key $1 from the local keyring" | |
gpg --batch --yes --delete-keys "$1" | |
fi | |
fi | |
} | |
# =========================== File Management Functions | |
# get downloads files using available backends | |
function get() { | |
[ -z "$1" ] || [ "$1" = '-h' ] || [ "$1" = '--help' ] \ | |
&& echo 'usage: get target_url [output_filename]' && return 2 | |
# assume the protocol to be HTTPS when unspecified | |
if [[ $1 != *':'* ]] && [ ! -f "$1" ]; then url="https://$1"; else url="$1"; fi | |
# use Transmission for torrent downloads | |
if command -v transmission-cli &>/dev/null \ | |
&& [[ $url =~ ^magnet:\\?xt=urn:btih:.* ]] \ | |
|| [ -f "$url" ]; then | |
[ -z "$2" ] && transmission-cli -u0 "$url" | |
[ ! -z "$2" ] && transmission-cli -u0 "$url" -w "$2" | |
# use Aria2 for downloading if possible since it's faster | |
elif command -v aria2c &>/dev/null; then | |
[ -z "$2" ] && aria2c \ | |
--file-allocation=none --allow-overwrite=true --summary-interval=0 \ | |
--seed-time=0 -ctrue -Rtrue -k5M -x10 -s10 -j10 "$url" | |
[ ! -z "$2" ] && aria2c \ | |
--file-allocation=none --allow-overwrite=true --summary-interval=0 \ | |
--seed-time=0 -ctrue -Rtrue -k5M -x10 -s10 -j10 "$url" -o "$2" | |
# use cURL if Aria2 is not found | |
elif command -v curl &>/dev/null; then | |
[ -z "$2" ] && curl -LO "$url" | |
[ ! -z "$2" ] && curl -L "$url" -o "$2" | |
# use Wget as the last resort | |
elif command -v dpkg &>/dev/null; then | |
[ -z "$2" ] && wget "$url" | |
[ ! -z "$2" ] && wget "$url" -O "$2" | |
else | |
>&2 echo 'none of aria2c, curl, nor wget can be found in PATH' | |
return 1 | |
fi | |
} | |
# async copy | |
function ac() { | |
unset ACOPY | |
unset AMOVE | |
export ACOPY=("${(@f)$(realpath -e $@)}") | |
} | |
# async move | |
function am() { | |
unset ACOPY | |
unset AMOVE | |
export AMOVE=("${(@f)$(realpath -e $@)}") | |
} | |
# async paste | |
function ap() { | |
if [ ! -z "$ACOPY" ]; then | |
cp -vr "${ACOPY[@]}" . | |
elif [ ! -z "$AMOVE" ]; then | |
mv -v "${AMOVE[@]}" . | |
unset AMOVE | |
else | |
>&2 echo 'clipboard empty' | |
return 1 | |
fi | |
} | |
# create and enter a temporary directory that self-destructs | |
function td() { | |
tempdir="$(mktemp -d)" | |
zsh -c "cd $tempdir && exec zsh" | |
rm -vrf "$tempdir" | |
} | |
# archive a file/directory with the highest-possible compression ratio | |
function archive() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no file or directory specified' | |
>&2 echo 'usage: archive [PATH]' | |
return 1 | |
elif [ ! -f "$1" ] && [ ! -d "$1" ]; then | |
>&2 echo 'file or directory does not exist' | |
>&2 echo 'usage: archive [PATH]' | |
return 1 | |
fi | |
if command -v zpaq &>/dev/null; then | |
'creating zpaq archive' | |
zpaq -m5 a "$(basename -- $1).zpaq" "$1" | |
elif command -v 7z &>/dev/null; then | |
'creating 7z archive' | |
#-m0=BCJ2 -m1=LZMA:d25 -m2=LZMA:d19 -m3=LZMA:d19 -mb0:1 \ | |
7z -t7z -mx=9 -mfb=273 -ms -md=31 -myx=9 -mtm=- -mmt -mmtf -md=1536m \ | |
-mmf=bt3 -mmc=10000 -mpb=0 -mlc=0 -m0=LZMA2:27 \ | |
a "$(basename -- $1).7z" "$1" | |
# && rm -rf $1 > /dev/null 2>&1 | |
else | |
>&2 echo 'neither zpaq nor 7z can be found in PATH' | |
fi | |
} | |
# archive a file/directory with the highest-possible compression ratio with 7-Zip | |
function compress() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no file or directory specified' | |
>&2 echo 'usage: compress [PATH]' | |
return 1 | |
elif [ ! -f "$1" ] && [ ! -d "$1" ]; then | |
>&2 echo 'file or directory does not exist' | |
>&2 echo 'usage: compress [PATH]' | |
return 1 | |
fi | |
if command -v 7z &>/dev/null; then | |
'creating 7z archive' | |
#-m0=BCJ2 -m1=LZMA:d25 -m2=LZMA:d19 -m3=LZMA:d19 -mb0:1 \ | |
7z -t7z -mx=9 -mfb=273 -ms -md=31 -myx=9 -mtm=- -mmt -mmtf -md=1536m \ | |
-mmf=bt3 -mmc=10000 -mpb=0 -mlc=0 -m0=LZMA2:27 \ | |
a "$(basename -- $1).7z" "$@" | |
# && rm -rf $1 > /dev/null 2>&1 | |
else | |
>&2 echo '7z cannot be found in PATH' | |
fi | |
} | |
# encrypt a file using AES-256-OFB | |
function encrypt() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no input file specified' | |
>&2 echo 'usage: encrypt [PATH]' | |
return 1 | |
fi | |
openssl enc -aes-256-ofb -salt -pbkdf2 -iter 100000 -in "$1" -out "$1.enc" | |
} | |
# decrypt a file encrypted with AES-256-OFB | |
function decrypt() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no input file specified' | |
>&2 echo 'usage: decrypt [PATH]' | |
return 1 | |
fi | |
local output_path="${1%.enc}" | |
[ "$1" = "$output_path" ] && output_path="${1}.dec" | |
openssl enc -d -aes-256-ofb -salt -pbkdf2 -iter 100000 -in "$1" -out "$output_path" | |
} | |
# rename files or directories | |
function rn() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no file or directory specified' | |
>&2 echo 'usage: rn [PATH]' | |
return 1 | |
fi | |
read '?rename to: ' rename | |
mv -v "$1" "$rename" | |
} | |
# a more intuitive multi-backend find | |
function f() { | |
if command -v fd &> /dev/null; then | |
fd -HIg "*$1*" | |
else | |
find -name "*$1*" | |
fi | |
} | |
# a more intuitive multi-backend find, case-insensitive | |
function F() { | |
if command -v fd &> /dev/null; then | |
fd -iHIg "*$1*" | |
else | |
find -iname "*$1*" | |
fi | |
} | |
# backup a file or directory | |
function bak() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no file or directory specified' | |
>&2 echo 'usage: bak [PATH]' | |
return 1 | |
fi | |
local backup_path="${1}.bak" | |
[ -f "$1" ] && cp -v "$1" "$backup_path" | |
[ -d "$1" ] && cp -rv "$1" "$backup_path" | |
} | |
# =========================== System Administration Functions | |
# allow kubecolor to substitute kubectl while not breaking the completions | |
function kubectl() { | |
# -G (NULL_GLOB) indicates that the function is executed by Zsh's autocomplete | |
# autocomplete has: -G, -P; doesn't have -Z, -m | |
if [[ $- == *G* ]] || ! command -v kubecolor &>/dev/null; then | |
command kubectl "$@" | |
else | |
command kubecolor "$@" | |
fi | |
} | |
# split docs in a k8s YAML file into multiple files | |
function splityaml() { | |
awk -vout=. -F": " '$0~/^# Source: /{file=out"/"$2; print "Creating "file; | |
system ("mkdir -p $(dirname "file"); echo -n "" > "file)}\ | |
$0!~/^#/ && $0!="---"{print $0 >> file}' | |
} | |
# get the name of the first pod in a deployment | |
function kgdp() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no target deployment name specified' | |
>&2 echo 'usage: kgdp [deployment_name]' | |
return 1 | |
fi | |
kubectl get pods -l app.kubernetes.io/name="$1" \ | |
-o jsonpath="{.items[0].metadata.name}" | |
} | |
# follow logs of a deployment | |
function klfd() { | |
if [ -z "$1" ]; then | |
>&2 echo 'no target deployment name specified' | |
>&2 echo 'usage: klfd [deployment_name] [container_name]' | |
return 1 | |
fi | |
kubectl logs -f $(kgdp "$1") -c "$2" | |
} | |
# activate Python venv if it exists; create it if it doesn't | |
# deactivate venv if it's already active | |
function venv() { | |
if [ ! -z "$VIRTUAL_ENV" ]; then | |
echo 'deactivating active venv' | |
deactivate | |
else | |
local directory="." | |
while [[ "$directory" != "/" ]]; do | |
if [ -f "$directory/.venv/bin/activate" ]; then | |
local relative_path=$(realpath --relative-to="." "$directory/.venv") | |
echo "activating existing venv at $relative_path" | |
source "$directory/.venv/bin/activate" | |
return | |
fi | |
directory="$(realpath "$directory/..")" | |
done | |
echo -n 'create new venv at `.venv`? [y/N] ' | |
read -r response | |
if [[ "$response" =~ ^([yY])$ ]]; then | |
echo 'creating new venv at `.venv`' | |
python3 -m venv .venv && source '.venv/bin/activate' | |
else | |
echo 'aborted venv activation' | |
fi | |
fi | |
} | |
# =========================== Miscellaneous Functions | |
# OUI lookup using Wireshark's manufacturer database | |
function ouilookup() { | |
curl -sSL https://gitlab.com/wireshark/wireshark/-/raw/master/manuf | grep -i $1 | |
} | |
# convert between IEEE 754 float and hexadecimal representations | |
function ieee754() { | |
python3 - "$1" << EOF | |
import struct, sys | |
try: print(hex(struct.unpack('<I', struct.pack('<f', float(sys.argv[1])))[0])) | |
except ValueError: | |
try: print(struct.unpack('<f', struct.pack('<I', int(sys.argv[1].replace("0x", ""), 16)))[0]) | |
except ValueError: print("invalid input") | |
EOF | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment