Skip to content

Instantly share code, notes, and snippets.

@dinh
Forked from BMPixel/better_defaults.zsh
Created October 1, 2024 07:39
Show Gist options
  • Save dinh/df43fa4fafef6e1720cbb53c2984796b to your computer and use it in GitHub Desktop.
Save dinh/df43fa4fafef6e1720cbb53c2984796b to your computer and use it in GitHub Desktop.
This Zsh configuration is designed to be universally adaptable, ready for any further customization.
# ============================================
# Zsh Configuration with better default
# Zsh configs that anyone should be welcomed to incorporate
# No complex prompts, just the essentials:
# - No aliases and keybindings.
# - No need for third-party plugins.
# ============================================
# ========= Completion & History Setup =========
autoload -Uz compinit
compinit -u
# Ignore unnecessary files such as .pyc
zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/).pyc'
# Enhance process killing commands with menu-style completion
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:kill:*' force-list always
# Load and configure menu-style completion
zmodload -i zsh/complist
bindkey -M menuselect '^M' accept
# ========= Directory Navigation Options =========
unsetopt auto_cd
setopt auto_pushd
setopt pushd_ignore_dups
setopt pushd_silent
# ========= Fine-tuning Completions =========
setopt auto_param_slash
setopt complete_in_word
setopt glob_complete
setopt list_beep
setopt list_packed
setopt list_rows_first
setopt no_beep
# ========= History Configuration =========
setopt append_history
setopt inc_append_history
setopt share_history
unsetopt bang_hist
unsetopt extended_history
setopt hist_ignore_space
setopt hist_reduce_blanks
# Only enable history logging if history file is writable
if [ -w ~/.zsh_history -o -w ~ ]; then
SAVEHIST=100000
HISTSIZE=100000
HISTFILE=~/.zsh_history
fi
# ========= Utility Functions =========
# Check if command is executable or aliased
_has() { return $( whence $1 &>/dev/null ); }
# Safe execution of commands
_try() { return $( eval $* &>/dev/null ); }
# Get version of a command, or "n/a" if not available
_versionof() {
if _has "$1"; then
echo "$1 $($1 --version)"
else
echo "$1 n/a"
fi
}
# ========= Better Editing Defaults =========
# Improve tab completion for redirection
self-insert-redir() {
integer l=$#LBUFFER
zle self-insert
(( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
}
zle -N self-insert-redir
for op in \| \< \> \& ; do
bindkey "$op" self-insert-redir
done
setopt notify
# Automatically quote URLs when pasted
autoload -U url-quote-magic
zle -N self-insert url-quote-magic
# ========= Ergonomic Command Aliases =========
alias mv='mv -i'
alias ln='ln -i'
alias mkdir='mkdir -p -v'
for c in cp rm chmod chown rename; do
alias $c="$c -v"
done
# Use human-readable format for disk space, when available
if _try df -H ~; then
alias df='df -H'
elif _try df -h ~; then
alias df='df -h'
fi
# Colorize output of ls if possible
if _try ls --color; then
alias ls='ls --color'
fi
# Set default pager to less with appropriate options
if _has less; then
export PAGER=less
export LESS='-Ri'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment