Skip to content

Instantly share code, notes, and snippets.

@huaminghuangtw
Created August 31, 2024 05:14
Show Gist options
  • Save huaminghuangtw/ade867b78447cd9808998c7ccc835f1f to your computer and use it in GitHub Desktop.
Save huaminghuangtw/ade867b78447cd9808998c7ccc835f1f to your computer and use it in GitHub Desktop.
My Zsh Configuration. For more powerful aliases please visit: https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/common-aliases/common-aliases.plugin.zsh
# Reload Zsh configuration files
reload() {
local zdotdir=${ZDOTDIR:-$HOME}
source "$zdotdir/.zshenv"
source "$zdotdir/.zprofile"
source "$zdotdir/.zshrc"
source "$zdotdir/.zlogin"
source "$zdotdir/.zlogout"
}
# Update and upgrade system packages
update() {
sudo apt-get update
sudo apt-get dist-upgrade -y
}
# Install a package and update system packages first
install() {
update
sudo apt-get install -y "$1"
}
# Aliases for opening files and directories in WSL
alias open='explorer.exe `wslpath -w "$PWD"`'
alias npp='/mnt/c/Program\ Files\ \(x86\)/Notepad++/Notepad++.exe'
# Command aliases for convenience
alias grep='grep --color=auto'
alias ls='command ls --color=auto'
alias cat='batcat'
# Aliases for various `ls` commands with different options
alias l='exa -lFh' # Size, classified, human-readable
alias la='ls -laFh' # Long list, show almost all, classified, human-readable
alias lr='ls -tRFh' # Sorted by date, recursive, classified, human-readable
alias lt='ls -ltFh' # Long list, sorted by date, classified, human-readable
alias ll='exa -alF' # All files, long list, classified
alias ldot='exa -ld .*' # List dotfiles
alias lS='ls -1FSsh' # Size, single column, human-readable
alias lart='ls -1Fcart' # Sorted by time, newest first, single column
alias lrt='ls -1Fcrt' # Sorted by time, oldest first, single column
# Environment variable management
setenv() {
local env_var=$1
local value=$2
echo "export $env_var=\"$value\"" >> "${ZDOTDIR:-$HOME}/.zshenv"
reload
}
# Path management
alias MyPath='echo -e ${PATH//:/\\n}'
# Quick access to config files
alias zshenv='cd ~ && npp "${ZDOTDIR:-$HOME}/.zshenv" -l'
alias zshrc='cd ~ && npp "${ZDOTDIR:-$HOME}/.zshrc" -l'
alias aliases='cd ~ && npp "${ZDOTDIR:-$HOME}/.zsh_aliases" -l'
alias gitconfig='cd ~ && cat "${ZDOTDIR:-$HOME}/.gitconfig"'
# File packing and unpacking
alias packing='tar -cvf'
alias unpacking='tar -xvf'
# Clipboard management
alias copy='xclip -selection clipboard'
alias c='clip.exe' # For WSL, usage: pwd | c
# IP address retrieval
alias public-ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias private-ip="hostname -I | awk '{print $1}'"
alias ips="ip -o -4 addr list | awk '{print \$4}'"
# File/Directory Management Functions
clear-contents-of-directory() {
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <DIRECTORY>" >&2
else
mkdir -p .tmp_to_remove
cd "$1" || return
mv -- * ../.tmp_to_remove # TODO: Handle dotfiles as well
rm -rf ../.tmp_to_remove &
fi
}
delete-directory() {
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <DIRECTORY>" >&2
else
clear-contents-of-directory "$1"
cd .. || return
rmdir "$1"
fi
}
# Compilation function for C++
compile() {
g++ "$1.cpp" -o "$1" "${@:2}"
}
# File type handling using suffix aliases
autoload -Uz is-at-least
if is-at-least 4.2.0; then
# Open browser on URLs
if [[ -n "$BROWSER" ]]; then
local _browser_fts=(htm html de org net com at cx nl se dk)
for ft in "${_browser_fts[@]}"; do
alias -s $ft='$BROWSER'
done
fi
# Open editor for text files
local _editor_fts=(cpp cxx cc c hh h inl asc txt TXT tex)
for ft in "${_editor_fts[@]}"; do
alias -s $ft='$EDITOR'
done
# Open image viewer for images
if [[ -n "$XIVIEWER" ]]; then
local _image_fts=(jpg jpeg png gif mng tiff tif xpm)
for ft in "${_image_fts[@]}"; do
alias -s $ft='$XIVIEWER'
done
fi
# Play media files
local _media_fts=(ape avi flv m4a mkv mov mp3 mpeg mpg ogg ogm rm wav webm)
for ft in "${_media_fts[@]}"; do
alias -s $ft=mplayer
done
# Read document files
alias -s pdf=acroread
alias -s ps=gv
alias -s dvi=xdvi
alias -s chm=xchm
alias -s djvu=djview
# List contents of archive files
alias -s zip="unzip -l"
alias -s rar="unrar l"
alias -s tar="tar tf"
alias -s tar.gz="tar tzf"
alias -s ace="unace l"
fi
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Prompt initialization
autoload -Uz promptinit
promptinit
prompt adam1
# History configuration
setopt histignorealldups sharehistory
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.zsh_history
# Disable auto-correction
unsetopt correct
unsetopt correct_all
ENABLE_CORRECTION="false"
# Keybindings configuration
bindkey -e # Use Emacs keybindings even if EDITOR is set to vi
# Enable modern completion system
autoload -Uz compinit
compinit
# Completion styles and settings
zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'
# Aliases
source ~/.zsh_aliases
# Plugins configuration
source ~/.zplug/init.zsh
zplug "romkatv/powerlevel10k", as:theme, depth:1
zplug "zsh-users/zsh-completions"
zplug "zsh-users/zsh-autosuggestions"
zplug "zsh-users/zsh-syntax-highlighting"
zplug "MichaelAquilina/zsh-you-should-use"
zplug "junegunn/fzf"
zplug "plugins/command-not-found/", from:oh-my-zsh
zplug "wting/autojump"
# Load plugins and add commands to $PATH
zplug load --verbose
# Prezto initialization
source "${ZDOTDIR:-$HOME}/.zprezto/init.zsh"
# Powerlevel10k configuration
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment