dotfiles/.zshrc

152 lines
4.6 KiB
Shell
Raw Normal View History

#!/bin/zsh
# `zsh`-specific configurations
# Remove built-in aliases because we use our own, sourced via ~/.profile
unalias -a
# Load configuration files common to all kinds of shells
[ -f "$HOME/.profile" ] && . "$HOME/.profile"
# Ensure `zsh` is running interactively
[[ -o interactive ]] || return
# Make `zsh` show `chroot`s in the prompt
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# Align `bash` and `zsh` for prompts (see `$PS1` in ~/.bashrc)
PS1="${debian_chroot:+($debian_chroot)}%n@%m:%~%(!.#.\$) "
# Configure the `history`
# Set these environment variables here
# to avoid conflict/overlap with `bash`
export HISTFILE="$XDG_STATE_HOME/zsh/history"
export HISTSIZE=999999 # Number of lines kept in memory
export SAVEHIST=999999 # Number of lines kept in the `$HISTFILE`
setopt APPEND_HISTORY # Do not overwrite the `$HISTFILE`
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks from the `history`
setopt HIST_IGNORE_DUPS # Ignore commands entered identically just before
setopt HIST_IGNORE_SPACE # Ignore commands prefixed with a space
setopt HIST_VERIFY # Show expanded `history` before executing
# Make `zsh` feel even nicer
setopt AUTO_CD # Just type the directory to `cd` into it
setopt CORRECT # Correct spelling of commands
setopt CHECK_JOBS # Show number of running jobs when exiting `zsh`
setopt EXTENDED_GLOB # Advanced globbing patterns
setopt NOMATCH # Fail loudly if patterns have no matches
setopt INTERACTIVE_COMMENTS # Allow `#` comments in the interactive shell
setopt NOBEEP # Silence `zsh`
stty -ixon # Prevent <Control>s from freezing `zsh`
# Enable (tab) completions
# "-u" disables the ownership check on completion dirs, which is safe since only we write to them
autoload -Uz compinit && compinit -u -d "${XDG_STATE_HOME:-$HOME/.local/state}/zsh/zcompdump"
# Complete the `--force` flag of the `dotfiles` utilities in ~/.local/bin
compdef '_arguments "--force[skip safety checks / reinstall]"' dotfiles-install dotfiles-update
# Enable match highlighting and scrolling through long lists,
# and provide a different style of menu completion
zmodload zsh/complist
# Include hidden files in tab completion
_comp_options+=(GLOB_DOTS)
# Make selecting completions nicer with a visual menu
zstyle ':completion:*' menu select
# Make new executables completable right away
zstyle ':completion:*' rehash true
# Match case-insensitively if the exact casing does not match
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'
# Group completion results and label them with bold headers
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*' group-name ''
# Make `git` sub-commands in ~/.local/bin completable
zstyle ':completion:*:*:git:*' user-commands \
${${${(M)${(k)commands}:#git-*}#git-}:#(receive-pack|upload-pack|upload-archive|shell)}
# Jump to frequently used directories with `z <fragment>`
if _command_exists zoxide; then
eval "$(zoxide init zsh)"
fi
# Suggest the `apt` package providing an unknown command
if [ -r /etc/zsh_command_not_found ]; then
. /etc/zsh_command_not_found
fi
# Configure key bindings
# `vi` mode
bindkey -v
# Use `vi` keys to navigate the completions in the menu
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
# Enable Ctrl-R for reverse history search
bindkey "^R" history-incremental-search-backward
# Fuzzy finding for
# - `history`: <Control>r
# - files: <Control>t
# - `cd`: <Alt>c
if [ -r /usr/share/doc/fzf/examples/key-bindings.zsh ]; then
. /usr/share/doc/fzf/examples/key-bindings.zsh
fi
# Fuzzy completion with the `**` trigger
# (e.g., `vi **<Tab>`, `ssh **<Tab>`)
if [ -r /usr/share/doc/fzf/examples/completion.zsh ]; then
. /usr/share/doc/fzf/examples/completion.zsh
fi
# Suggest the most recent matching `history` entry as gray ghost text
if [ -r /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then
. /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
bindkey "^ " autosuggest-accept # <Control>Space + right arrow works by default
fi
# Highlight the command line while typing (valid=green, invalid=red)
# (Important: Must be sourced at the very end of this file)
if [ -r /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]; then
. /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi