# On slow systems, checking the cached .zcompdump file to see if it must be | |
# regenerated adds a noticable delay to zsh startup. This little hack restricts | |
# it to once a day. It should be pasted into your own completion file. | |
# | |
# The globbing is a little complicated here: | |
# - '#q' is an explicit glob qualifier that makes globbing work within zsh's [[ ]] construct. | |
# - 'N' makes the glob pattern evaluate to nothing when it doesn't match (rather than throw a globbing error) | |
# - '.' matches "regular files" | |
# - 'mh+24' matches files (or directories or whatever) that are older than 24 hours. | |
autoload -Uz compinit | |
if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+24) ]]; then | |
compinit; | |
else | |
compinit -C; | |
fi; | |
@medwatt put the snippet somewhere in your .zshrc file, or put it in an external script file and call it from your .zshrc
@vincentbernat I solved the multiple shell problem by using a lockfile and a trap
to remove it. This ensures that only one process is responsible for updating the file. Even though there's still a race condition, it's now almost impossibly small 1
() {
setopt local_options
setopt extendedglob
local zcd=${1}
local zcomp_hours=${2:-24} # how often to regenerate the file
local lock_timeout=${2:-1} # change this if compinit normally takes longer to run
local lockfile=${zcd}.lock
if [ -f ${lockfile} ]; then
if [[ -f ${lockfile}(#qN.mm+${lock_timeout}) ]]; then
(
echo "${lockfile} has been held by $(< ${lockfile}) for longer than ${lock_timeout} minute(s)."
echo "This may indicate a problem with compinit"
) >&2
fi
# Exit if there's a lockfile; another process is handling things
return
else
# Create the lockfile with this shell's PID for debugging
echo $$ > ${lockfile}
# Ensure the lockfile is removed
trap "rm -f ${lockfile}" EXIT
fi
autoload -Uz compinit
if [[ -n ${zcd}(#qN.mh+${zcomp_hours}) ]]; then
# The file is old and needs to be regenerated
compinit
else
# The file is either new or does not exist. Either way, -C will handle it correctly
compinit -C
fi
} ${ZDOTDIR:-$HOME}/.zcompdump
For those who are new to some of this stuff, check out
- https://zsh.sourceforge.io/Doc/Release/Completion-System.html
- For how
compinit
works
- For how
- https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation
- For the
(#q.N.....)
syntax, search for "file access qualifier"
- For the
Footnotes
-
A second process would have to check for the file in between 2 (effectively) subsequent commands.
if [ -f ${lockfile} ]
andecho $$ > ${lockfile}
↩
@thefotios my solution is already handling this case (but my message wasn't quite clear on that, so I understand you thought this was not the case). This is the purpose of the ln command (which is atomic, so no race condition).
I commented out these lines in my .zshrc
which sped it up a lot:
if type brew &>/dev/null; then
FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
autoload -Uz compinit
compinit
if
As this comment above pointed out, oh-my-zsh already runs compinit.
@aztack Thank you for your snippet, it helps alot :)
Fellas, if you're of that kind that checks their .zsh files and scripts with shellcheck (like I am), here's a more POSIX-compliant (as much as zsh allows) statement:
if [ "$(find ~/.zcompdump -mtime 1)" ] ; then
compinit
fi
compinit -C
or oneliner, if you prefer that:
# negation, so that at least one exits on 0
[ ! "$(find ~/.zcompdump -mtime 1)" ] || compinit
compinit -C
find's manpage is not clear on this, but I believe you want -mtime +1
to catch a file at least 24h old, rather than exactly 24h old.
On OSX, my .zcomdump was several days old, and the above would not trigger until I added the '+'.
@aztack It helped. 🙇
Where exactly do we put this code: