Skip to content

Rename files with Denote using dired-preview

The dired-preview package (by me/Protesilaos) automatically displays a preview of the file at point in Dired. This can be helpful in tandem with Denote when we want to rename multiple files by taking a quick look at their contents.

The command denote-dired-rename-marked-files-with-keywords will generate Denote-style file names based on the keywords it prompts for. Identifiers are derived from each file’s modification date (Rename multiple files at once). There is no need for any custom code in this scenario.

As noted in the section about Image Dired, the user may sometimes not need a fully fledged Denote-style file name but only append Denote-like keywords to each file name (e.g. ‘Original Name__denote_test.jpg’ instead of ‘20230710T195843--original-name__denote_test.jpg’).

Rename files with Denote in the Image Dired thumbnails buffer

In such a workflow, it is unlikely to be dealing with ordinary text files where front matter can be helpful. A custom command does not need to behave like what Denote provides out-of-the-box, but can instead append keywords to file names without conducting any further actions. We thus have:

emacs-lisp
(defun my-denote-dired-rename-marked-files-keywords-only ()
  "Like `denote-dired-rename-marked-files-with-keywords' but only for keywords in file names.

Prompt for keywords and rename all marked files in the Dired
buffer to only have Denote-style keywords, but none of the other
conventions of Denote's file-naming scheme."
  (interactive nil dired-mode)
  (if-let* ((marks (dired-get-marked-files)))
      (let ((keywords (denote-keywords-prompt)))
        (dolist (file marks)
          (let* ((dir (file-name-directory file))
                 (file-type (denote-filetype-heuristics file))
                 (title (denote--retrieve-title-or-filename file file-type))
                 (extension (file-name-extension file t))
                 (kws (denote--keywords-combine keywords))
                 (new-name (concat dir title "__" kws extension)))
            (denote-rename-file-and-buffer file new-name)))
        (revert-buffer))
    (user-error "No marked files; aborting")))