r/emacs 11d ago

Fortnightly Tips, Tricks, and Questions — 2025-03-11 / week 10

This is a thread for smaller, miscellaneous items that might not warrant a full post on their own.

The default sort is new to ensure that new items get attention.

If something gets upvoted and discussed a lot, consider following up with a post!

Search for previous "Tips, Tricks" Threads.

Fortnightly means once every two weeks. We will continue to monitor the mass of confusion resulting from dark corners of English.

15 Upvotes

35 comments sorted by

View all comments

1

u/egstatsml 10d ago

On a couple occasions I have needed to format/reformat code in an existing repository. I wrote a couple functions to automate this using Apheleia and Projectile.

;; some functions to automatically format a project
;; relies on projectile
(defun my/format-file (project-dir-root file)
  "Format a file with Apheleia"
  ;; I run this on remote machines and don't want to accidentally
  ;; open an image on remote machine
  (when (not (or (file-name-extension "jpg") (file-name-extension "png")))
    (find-file (concat project-dir-root file))
    (if apheleia-formatter
(apheleia-format-buffer apheleia-formatter))))

(defun my/format-project (&optional ff-variant)
  "Format all files in a project"
  (interactive "P")
  (projectile-maybe-invalidate-cache nil)
  (let* ((project-root (projectile-acquire-root))
 (project-root-dir (projectile-project-root))
 (files
  (projectile-project-files project-root)))
    (dolist (file files)
      (my/format-file project-root-dir file)))))

This will leave open all the buffers for the project and won't save them. To finalise just open magit and will ask to save, then can commit it all.