Search code examples
zsh

How does this snippet of zsh code keep track of time? Doesn't seem to have date or anything


I came across this snippet of code in the prezto repo, and im really stumped.

It appears to rebuild zsh comp every 20 hours, but I can't figure out how.

(Nm-20) seems to be calling into a subshell, but Nm-20 itself is not a program.

($BLAH/zcompdump) should be a file, but it shouldn't be executable, so why is it surrounded by ()?

Lastly, if (( $#_comp_files )) seems to be that the outer() is a test to see if ( $#_comp_files ) is true or not, but what is $#_comp_files?

# Load and initialize the completion system ignoring insecure directories with a
# cache time of 20 hours, so it should almost always regenerate the first time a
# shell is opened each day.
autoload -Uz compinit
_comp_files=(${ZDOTDIR:-$HOME}/.zcompdump(Nm-20))
if (( $#_comp_files )); then
  compinit -i -C
else
  compinit -i
fi
unset _comp_files


Solution

  • The magic is the use of Glob Qualifiers.
    At the end of a filename, you can add parenthesis with filters.

    .zcompdump(Nm-20)) will select this file only if modified less than 20 days ago.
    The N says to not display an error (NULL_GLOB).

    It's missing a h to have Nmh-20 to get it if modified less than 20 h ago.

    man zshexpn then search for Glob Qualifiers.

    Try it yourself with

    cd /tmp
    touch myfile
    z=(myfile(Nmm-1)) ; echo $z
    

    wait a minute and try again the last line, with or without N