Add configuration for zsh

- Add ~/.zshrc
  + Configure on how the `history` works
  + Enable nicer `cd` and globbing behavior
  + Enable tab completion, and make them feel nicer
  + Enable VI mode and add some VI-like key bindings
- Add ~/.zlogout clearing the screen
- Add ~/.config/zsh/.zshrc and make it dispatch to ~/.zshrc
  to make `zsh` use the same configuration even on more
  recent Debian/Ubuntu machines that do not look for
  ~/.zshrc any more
- Ensure ~/.local/state/zsh/ exists
This commit is contained in:
Alexander Hess 2025-08-30 11:19:30 +02:00
parent 1f3fe88216
commit fa98b22431
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
6 changed files with 93 additions and 0 deletions

10
.config/zsh/.zshrc Normal file
View file

@ -0,0 +1,10 @@
#!/bin/zsh
# Load the real `zsh` config file in ~/
#
# Recent Debian/Ubuntu versions look for .zshrc
# in .config/zsh and no longer in ~/ which
# is still the main location in many other distributions
. "$HOME/.zshrc"

3
.config/zsh/README.md Normal file
View file

@ -0,0 +1,3 @@
# `zsh`-related Configurations
This folder contains files that are sourced by `zsh`.

View file

View file

@ -51,6 +51,10 @@ if [ -n "$BASH_VERSION" ] && [ -f "$HOME/.bashrc" ]; then
fi
# `zsh`-specific configurations are automatically sourced from ~/.zshrc,
# which then also ensures that this file is sourced
# Put local executables on the `$PATH`
_prepend_to_path "$HOME/.local/bin"

3
.zlogout Normal file
View file

@ -0,0 +1,3 @@
#!/bin/zsh
. "$XDG_CONFIG_HOME/shell/logout"

73
.zshrc
View file

@ -3,5 +3,78 @@
# `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` behave like `bash` for prompts
PS1='%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 INC_APPEND_HISTORY # Write to the `$HISTFILE` immediately
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks from the `history`
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 EXTENDED_GLOB # Advanced globbing patterns
setopt NULL_GLOB # Remove patterns with no matches
setopt CORRECT # Correct spelling of commands
setopt CHECK_JOBS # Show number of running jobs when exiting `zsh`
setopt NO_BEEP # Silence `zsh`
stty -ixon # Prevent Ctrl+S from freezing `zsh`
# Enable (tab) completions
autoload -Uz compinit && compinit
# 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
# 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