zsh
1. .zshrc
zsh
is my login and interactive shell of choice, but given that all
environment variables are defined within the confines of environment.d
, a
component of systemd
that provides a common environment for user-managed
services, there's no use in creating the standard .zshenv
and .zprofile
files.
1.1. Startup
Autoload compinit
, a module that provides completions for zsh
.
autoload -Uz compinit
Speed up compinit
by using zcompdump
, a completion caching
solution.
compinit -d ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompdump
1.2. Common options
setopt append_history setopt extended_history setopt hist_ignore_space setopt hist_ignore_all_dups setopt hist_reduce_blanks setopt hist_no_store setopt complete_aliases setopt share_history setopt ignore_eof setopt list_packed setopt auto_remove_slash setopt no_complete_aliases
See the manual for more information on the above options.
1.3. Completions
zstyle
is a built-in mini configuration module for zsh
, it
concerns itself with how things a lookup is performed relative to an
input string.
For example, for any given string (expressed by the pattern
:completion:*
), the following form instructs the shell to present
completions as a navigable menu from which items are selected.
zstyle ':completion:*' menu select
While this form tells the shell to cache any completions to reduce the computational cost of looking them up each time.
zstyle ':completion:*' use-cache on
This next one instructs files to be sorted by the last time they were accessed, it is super handy but has the downside of being potentially cumbersome in situations where a directory has more than a few dozen entries, in which case alphabetical order (the default) is the most helpful ordering mechanism.
zstyle ':completion:*' file-sort access
Sorting files reduces the cognitive effort associated with parsing unordered data, grouping things by their nature – listing directories first, files second – will help further reduce that mental strain.
zstyle ':completion:*' list-dirs-first true
Use the LS_COLORS
environment variable to dictate the color
specification of completion candidates.
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
Configure the order of appearance of directories when auto-completing; display
local directories before those in CDPATH
.
zstyle ':completion:*:complete:(cd|pushd):*' \ tag-order 'local-directories named-directories'
For more information on these settings, see zshcompsys(1).
PROMPT="%(4~|.../%2~|%~) $ "
Configure the cdpath
built-in variable, an array-based representation of the
shell-agnostic CDPATH
environment variable that helps jump to (the listed,
semicolon-separated) directories regardless of whether or not they exist in the
current directory.
Every shell recognizes the PATH
environment variable and looks for executables
by traversing each directory until it finds the first matchl; CDPATH
is the
equivalent of PATH
but for directories.
cdpath=($HOME $HOME/sites $HOME/programs $HOME/documents)
1.4. Aliases
I prefer to have commands that perform changes to the filesystem to be verbose and to always prompt me for my confirmation (a protective measure).
Bypassing the prompts can be done using the yes
command, e.g. yes |<cp,rm,mv> [OPTIONS]
.
alias cp='cp -iv' alias mv='mv -iv' alias rm='rm -iv'
Listing directories should be colorful, their keybindings should be intuitive.
alias ls='ls --color=always --group-directories-first' alias la='ls -la' alias ll='ls -l'
cd
is aliased to cdls
to automate discovery, making movement
between directories a bit more intuitive.
alias cd='cdls' alias ..='cd ..'
Browse the list of installed packages and their metadata:
alias pkgls='pacman -Qq | fzf --preview-window sharp --preview "pacman -Qi {1}"'
Browse the list of packages available for download (and install the selection):
alias pkgin='pacman -Slq | fzf --multi --preview "pacman -Si {1}" | xargs -ro doas pacman -S'
And everything else…
alias sudo='doas' alias open='xdg-open' alias ctl='systemctl' alias uctl='systemctl --user' alias vim='vim --clean' alias mbsync="mbsync --config ${XDG_CONFIG_HOME:-$HOME/.config}/mbsync/mbsyncrc" alias ducks='du -cks * | sort -rn | head -11' alias bell='tput bel'
1.5. Functions
Define a function that lists the contents of directories upon entering
them, it is aliased to cd
section 1.4.
cdls() { builtin cd "$@" && ls }
Define a function that brings programs to the foreground the same way they were placed in the background, originally authored by Adam Stankiewicz in an article titled "How to boost your Vim productivity".
fancy-ctrl-z() { if [[ $#BUFFER -eq 0 ]]; then BUFFER="fg" zle accept-line -w else zle push-input -w zle clear-screen -w fi } # Load the function zle -N fancy-ctrl-z # Bind it to Ctrl-Z bindkey '^Z' fancy-ctrl-z
If a command's execution time exceeds $COMMAND_DURATION
seconds, ring the bell
after it has finished (no matter the exit code).
COMMAND_DURATION=30 function precmd_bell { if [[ ${TTYIDLE} -gt $COMMAND_DURATION ]] ; then tput bel fi } precmd_functions+=(precmd_bell)
1.6. Keybindings
bindkey -e