Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Selectively sorting directory by date in completion
- X-seq: zsh-users 30461
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: André Marçais <andre@xxxxxxxxxxx>
- Cc: zsh-users@xxxxxxx
- Subject: Re: Selectively sorting directory by date in completion
- Date: Sun, 22 Mar 2026 05:14:11 +0100
- Arc-authentication-results: i=1; mx.google.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=I/kP9p1bF0dMHsPlNiWY9wWftKAld7zWqbs14vD1o8E=; fh=ymwQgODUWrCe+fLa0bQxAeCM1Nh+aIkxkGakg7m2aIw=; b=Xwdi4UhFZO1bqebXBVBsQQGcHtu1L00XZY3Fp3Vc4DhNFhLKPISI2oBhz9W1pLgnqD lU80p511OCkS+3NBXy4Sd0Kpl/FdSKadZA4zvRwPXiffj6c+0JK7CGQdfqANqbrPA3OQ TY23fPJ2dIITzks/Wo6UJMsG4w46WyZoxvyv7xFW4trbLxfDSep3Jl6Vki++NJ4vDtek ungulyGuf0ShTr9tEt2JKPHnaGWzvuuMPmwRMFE3fyUKnF6QgBCxNZ+0RSLDKtOVBW1z 7yRbdeiqakr9fQ4Cor9AY20fSUp+RODZm/LhOyROayQ9o4Gs8vKysrK8GXgdLwRFywrk sbeA==; darn=zsh.org
- Arc-seal: i=1; a=rsa-sha256; t=1774152864; cv=none; d=google.com; s=arc-20240605; b=cmsG80snNgXdOQYks+jYqOfZDJrjikQJXmm0d1pr7oeOhfprgnaL6FlyVEJCt1ypwu pGvJGcYQFEFLGRVZSEpjbXUYhbR5q8bmAHmcp71CgQ3Vw8mfv04NZ8jvFvpdeRb8gTnY H66GTq03+RnpYyOvQCyZBkGHEdKcSIEHZEZ7oNrEZ2zaDGTzmS67ESQnfWq/tCF0QL2Y 34syIq5OyWnJ5phf4omwdZRI5BSTQUn0tMKnkRdLZ49qQz/l+ElViUKqG9IQtvTlSiES fIA1UtvWGfmWgSjdnlpIopUOdgkeks2+1X7nfKwT4AOSKz6fchRVnZMWBltwAWZPMJGb JNMw==
- Archived-at: <https://zsh.org/users/30461>
- In-reply-to: <DH8UZQ4YFIC1.3I46LXJG3V9QL@marcais.net>
- List-id: <zsh-users.zsh.org>
- References: <DH8UZQ4YFIC1.3I46LXJG3V9QL@marcais.net>
On Sun, Mar 22, 2026 at 12:45 AM André Marçais <andre@xxxxxxxxxxx> wrote:
>
> Hello,
>
> I'm posting here to share some of my configuration hoping it may be
> useful to others. Also, what I've done feels pretty hacky, so if anyone
> has suggestions, I'm a taker. But first, I explain my problem.
>
> The downloads folder/directory (`~/dl` in my case) often has lots of
> stuff in it but in most cases only the most recently downloaded file is
> relevant. So alphabetical sort when completing file names under `~/dl`
> makes no sense. There is `zstyle ':completion:*' file-sort date` which
> is understood by `_path_files` to sort all file name completion by date.
> I, however, find this to be undesirable for most directories other than
> `~/dl` and a few others since alphabetical sort is better for scanning
> with one's eyes. The global setting is too broad.
>
> So here is the solution I came up with: I redefined `_path_files` by
> symlinking `~/.local/share/zsh/site-functions/_path_files_old` to
> `/usr/share/zsh/functions/Completion/Unix/_path_files` and by creating
> the file `~/.local/share/zsh/site-functions/_path_files` containing
>
> #autoload
>
> case $PREFIX in
> */)
> case ${${~PREFIX:-./}:a} in
> $HOME/dl) curcontext=downloads ;;
> $HOME/tmp) curcontext=downloads ;;
> /tmp) curcontext=downloads ;;
> esac
> ;;
> esac
>
> _path_files_old
>
> and adding the line
>
> zstyle ':completion:downloads:*' file-sort date # see our _path_files
>
> to my `.zshrc`.
>
> Thus far it seems to work, tough I'm uncertain about a few things:
> - I'm not really sure my use of `$curcontext` is correct.
> - The symlink feels kinda wrong.
> - I use `${~spec}` to do filename expansion, but that also does filename
> generation. I'm not sure that's desirable.
>
> Hope this helps someone. And again, hope to get some feedback.
You might be able to make good use of zstyle -e here, where the value
of the style will be evaluated and then the parameter $REPLY will be
used as the style value for that particular lookup. So eg you'd say
zstyle -e :completion:\* file-sort 'some fun code here;
REPLY=something' (The code to be evaled can still be a simple function
call if you don't want to put a big blob of code in your style
definitions). For example I have zstyle -e ':completion:*:urls' urls
_urls_http where _urls_http sets reply appropriately for my particular
use case.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author