hoowl

Physicist, Emacser, Digitales Spielkind

Find recently modified files using vertico in Emacs
Published on Oct 03, 2025.

vertico is a powerful completion framework for Emacs that helps, for example, to find files using partial matches.

By default, it sorts all matches alphabetically and even offers some alternative sorting mechanisms; but none matched my unpleasantly frequent use case: opening whatever-that-file-I-just-downloaded-is-called or just-give-me-whatever-that-command-just-produced. So sorting by modification time was desperately needed!

While that seems like a humble enough desire, it turned out to be more complicated then I thought. My searches online yielded few hits and no working solution. But luckily my slowly evolving ELISP skills seem to have just passed the threshold necessary to come up with a working solution – and here it is in all its hacky glory:

(after! vertico
  (defun hanno/vertico-sort-by-mtime (files)
    "Sort FILES by modification time (newest first)."
    (let ((dir nil))
      (when (< (minibuffer-prompt-end) (point))
        (setq dir (buffer-substring (minibuffer-prompt-end) (point-max))))
      (sort files
            (lambda (a b)
              (let* (
                     (fa (expand-file-name a dir))
                     (fb (expand-file-name b dir))
                     (ta (file-attribute-modification-time (file-attributes fa)))
                     (tb (file-attribute-modification-time (file-attributes fb))))
                (time-less-p tb ta))))))

(defun vertico-toggle-sort ()
  (interactive)
  (setq-local vertico-sort-override-function
              (and (not vertico-sort-override-function)
                   (lambda (files)
                     (if (and (eq minibuffer-history-variable 'file-name-history)
                              (not (eq (car-safe minibuffer-completion-table) 'boundaries)))
                         (hanno/vertico-sort-by-mtime files)
                       (vertico-sort-history-length-alpha files))))
              vertico--input t))

(keymap-set vertico-map "M-S" #'vertico-toggle-sort))

After running the above code, open the find-file dialog by pressing C-x C-f and then press M-S s (meta + shift + s) to toggle the sorting.

How this works

This uses the foreseen mechanism in vertico to modify the sorting of matches, vertico-sort-override-function. Unfortunately, the function called by this hook is only provided with a list of file names without any absolute path needed to actually locate them. Retrieving the modification times is therefore not straightforward.

hanno/vertico-sort-by-mtime therefore starts by deriving the current path from the minibuffer prompt which (of course) displays it for the user. Using this, it gets the modification timestamp for each of the candidates displayed and returns a sorted list.

This certainly feels a little hacky even though it works fine and is probably robust enough. Still, as the marginalia annotations displayed besides the files already have the modification times, I am sure there is a more elegant way to get to this information. One drawback is that it is fairly slow when there are lots of files; in that case, using dired and its built-in sorting might be quicker and more comfortable.

If you have a clever idea how to improve this or should you have encounted any issues, get in touch and let me know!

This code is loosely based on the very helpful examples in the vertico wiki and took inspiration from this post addressing the same problem (but missing the issue with the current directory path).

Tags: emacs, lisp