Correcting Org Tags

A couple of days ago, I was adding some item to my journal—which is really my engineering note book—and when my capture template prompted me for a tag and I started to type it in, I noticed that in the list of suggestions (from Ivy) there was a misspelled tag. That happens fairly regularly. Sometimes I make a flat out typo, sometimes I unconsciously capitalize a proper noun (my policy is all tags are lower case), and sometimes I use a slightly different tag from what I usually do.

When I find a bad tag, I fix it by searching my agenda files with the m option from the Agenda menu1 to bring up the headlines with the offending tag and then fix it by editing the tag with Ctrl+c Ctrl+q.

This time, I’d finally had enough and decided to get rid of them all. The question was how to get a list of all the tags? I first tried grep but the result was a mess and too hard to work with. Then I thought that if Ivy was gathering that list for its completing read, there must be an easy way to do it. I asked for help on functions, typed in “org tag”, and looked through the completions until I found org-global-tags-completion-table. That did exactly what I wanted so I fired up ielm, invoked org-global-tags-completion-table, and had a nice list of tags. I didn’t bother sorting it, I just read down the list until a found a bad tag and called the match TAGS query to bring up the affected headlines so I could fix them. It turned out there were a lot of bad tags but it was easy, if boring, to find and fix them all.

The point of this post is twofold. First, Emacs gives you the tools to fix problems in your data even if several files are involved. It also gives you an easy way to discover those tools. Secondly, the exercise reminded me of the power of ielm. Usually, when I need to run a random elisp command I do it from the minibuffer. This time I needed an easily accessible list so I used ielm. I tend to forget about it but it’s often just what you need, even in situations that don’t involve elisp development.

If I had to do this a lot, I’d write some code to handle correcting the bad tags but that didn’t seem worth the trouble in this case. Still, if I ever decide I need to, Emacs has all the tools to make it easy.

Footnotes:

1

I use Ctrl+c a m but if your keybindings differ it’s the “Match a TAGS/PROP/TODO query”.

This entry was posted in General and tagged , . Bookmark the permalink.