(redirected from Homepage)

Strona domowa

Witam na mojej prywatnej stronie internetowej!

[If this is all Polish to you, click here: English]

Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.

Marcin ‘mbork’ Borkowski

2025-07-07 Mass resetting Org mode checkboxes

I’m a big fan of checklists. There are many cases where I need to follow some procedure containing many steps, and I really do not want to forget about any of them. This is especially challenging when it is something I need to do not very often, say, once a month, since it is very easy to forget about one or more steps, and properly memorizing the procedure would take many months. One example is preparing and sending invoices, which I need to do at the beginning of every month. Another (slightly less typical) example is packing for vacations – it’s very easy to forget to take something, and very important not to. In fact, I prepared a generic list of things to take with me on vacation more than fifteen years ago, and I use it several times a year, sometimes adding some new item, sometimes removing some no longer needed item.

Me being me, I keep these checklists in Org mode, and they consist of one or more headings with a bunch of checkboxed lists.

One thing which was always a nuisance for me was resetting those checkboxes back to “not checked” state. I usually just query-replace​d the string [X] to [ ], but then I had to manually reset the checkbox cookies on each headline. Finally, I decided that there must be a better method – why not write a simple piece of Elisp to reset all checkboxes for me? Of course, I started with doing a bit of research to make sure I don’t reinvent the wheel. Lo and behold, Org mode already has a feature like that! There is a command called org-reset-checkbox-state-subtree, which resets all checkboxes in the current subtree. Now, the only thing left to automate is to call this function for every subtree in the buffer or region.

(defun org-reset-checkbox-state-buffer ()
  "Reset all checkboxes in the (narrowed portion of) the buffer."
  (interactive "*")
  (org-map-entries #'org-reset-checkbox-state-subtree
                   t nil
                   'archive 'comment))

It’s not ideal – for example, it apparently unfolds the headlines in the whole buffer. Also, it calls (org-update-checkbox-count-maybe 'all), which scans the whole buffer after processing every headline – definitely not optimal. However, I’m not going to run this on Org files larger than, say, several kilobytes (and I do have Org files larger than that – in fact, the very file I’m writing this blog in is over 1.5MB!). Also, if the heading structure is nested, it is enough to run org-reset-checkbox-state-subtree on every level one headline, while my code does that on all subheadings, too – but then again, my packing list (currently the only use-case for this command over org-reset-checkbox-state-subtree) does not have any subheadings at all. If your use-case is more complex, feel free to adapt my code, for example calling org-forward-heading-same-level in a while loop.

That’s it for today, see you next time!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

More...