7.3 KiB
ZSH
TL;DR
Startup file sequence:
/etc/zshenvand${ZDOTDIR}/.zshenv- login shells only:
/etc/zprofileand${ZDOTDIR}/.zprofile - interactive shell only:
/etc/zshrcand${ZDOTDIR}/.zshrc - login shells only:
/etc/zloginand${ZDOTDIR}/.zlogin - upon exit:
/etc/zlogoutand${ZDOTDIR}/.zlogout
Aliases are expanded when the function definition is parsed, not when the function is executed. Define aliases before functions to avoid problems.
# create a function
function_name () { … }
function function_name { … }
function function_name () { … }
# regex match
[[ $OSTYPE =~ "darwin" ]]
[[ $OSTYPE -regex-match "darwin" ]]
# find broken symlinks in the current directory
ls **/*(-@)
# print all shell and environment variables
( setopt posixbuiltin; set; )
Alias expansion
When one writes an alias, one can also press ctrl-x followed by a to see the expansion of that alias.
Arrays
# get a slice (negative are backwards)
echo ${ARRAY[2,-1]}
# get all folders up to a non folder, backwards
local COMMAND
local FOLDERS=()
for (( I = $# ; I >= 0 ; I-- )); do
if [[ -d "${@[$I]}" ]]; then
FOLDERS+="${@[$I]}"
else
COMMAND="${@[1,-$((${#FOLDERS}+1))]}"
break
fi
done
# make entries in PATH unique
# see https://til.hashrocket.com/posts/7evpdebn7g-remove-duplicates-in-zsh-path
typeset -aU path
Tests
# regex match
[[ $OSTYPE =~ "darwin" ]]
[[ $OSTYPE -regex-match "darwin" ]]
Find broken symlinks in the current directory
ls **/*(-@)
Configuration
Config files read order
-
/etc/zshenv; this cannot be overridden
subsequent behaviour is modified by theRCSandGLOBAL_RCSoptions:RCSaffects all startup filesGLOBAL_RCSonly affects global startup files (those shown here with an path starting with a /)
If one of the options is unset at any point, any subsequent startup file(s) of the corresponding type will not be read.
It is also possible for a file in$ZDOTDIRto re-enableGLOBAL_RCS.
BothRCSandGLOBAL_RCSare set by default -
$ZDOTDIR/.zshenv -
if the shell is a login shell:
/etc/zprofile$ZDOTDIR/.zprofile
-
if the shell is interactive:
/etc/zshrc$ZDOTDIR/.zshrc
-
if the shell is a login shell:
/etc/zlogin$ZDOTDIR/.zlogin
-
when a login shell exits:
$ZDOTDIR/.zlogout/etc/zlogout
This happens with either an explicit exit via the
exitorlogoutcommands, or an implicit exit by readingend-of-filefrom the terminal.
However, if the shell terminates due to exec’ing another process, the files are not read. These are also affected by theRCSandGLOBAL_RCSoptions.
TheRCSoption affects the saving of history files, i.e. ifRCSis unset when the shell exits, no history file will be saved.
If ZDOTDIR is unset, HOME is used instead. Files listed above as being in /etc may be in another directory, depending on the installation.
/etc/zshenv is run for all instances of zsh.
it is a good idea to put code that does not need to be run for every single shell behind a test of the form if [[ -o rcs ]]; then ... so that it will not be executed when zsh is invoked with the -f option.
When /etc/zprofile is installed it will override PATH and possibly other variables that a user may set in ~/.zshenv. Custom PATH settings and similar overridden variables can be moved to ~/.zprofile or other user startup files that are sourced after the /etc/zprofile.
If PATH must be set in ~/.zshenv to affect things like non-login ssh shells, one method is to use a separate path-setting file that is conditionally sourced in ~/.zshenv and also sourced from ~/.zprofile.
History
# The maximum number of events stored in the internal history list.
# If you use the HIST_EXPIRE_DUPS_FIRST option, setting this value larger than
# the SAVEHIST size will give you the difference as a cushion for saving
# duplicated history events.
HISTSIZE=1000
# The file to save the history in when an interactive shell exits.
# If unset, the history is not saved.
HISTFILE=~/.histfile
# The maximum number of history events to save in the history file.
SAVEHIST=1000
Completion
# Enable completion
autoload -U compinit
compinit
# Enable cache for the completions
zstyle ':completion::complete:*' use-cache true
Prompt management
# Enable prompt management
autoload -U promptinit
promptinit; prompt gentoo
Automatic source of files in a folder
# Configuration modules.
# All files in the configuration folder will be automatically loaded in
# numeric order. The last file setting a value overrides the previous ones.
# Links are only sourced if their reference exists.
ZSH_MODULES_DIR="${ZSH_MODULES_DIR:-$HOME/.zshrc.d}"
if [[ -d "${ZSH_MODULES_DIR}" ]]
then
for ZSH_MODULE in ${ZSH_MODULES_DIR}/*
do
[[ -r ${ZSH_MODULE} ]] && source "${ZSH_MODULE}"
done
unset ZSH_MODULE
fi
Frameworks
Plugins
Troubleshooting
zsh compinit: insecure directories and files, run compaudit for list
compaudit | xargs chmod g-w
Further readings
- Substitutions
- ZSH compinit: insecure directories and files, run compaudit for list
- Pattern matching in a conditional expression
- [Eemove duplicates in ZSH path]
- Completion config example
- What should/shouldn't go in .zshenv, .zshrc, .zlogin, .zprofile, .zlogout?
- The Z Shell Manual
- Gentoo Wiki
- How can I convert an array into a comma separated string?
- How to list all variables names and their current values?