A User's view of Hledger

Learning about hledger

Remember the future! — repost

This summer, while I am on vacation, I am reposting some previous columns, albeit with minor edits. These are columns that are largely timeless. Enjoy!

Originally posted June 30, 2022 — Remember the future!
Just because something works now doesn’t mean that it will work in the future. This happened to me with the categories I used in hledger. My naming convention worked for a while, but after some changes to my banking, some problems emerged.

Before looking specifically at accounting categories, let’s consider labels or signs in general. For example, there is a school near me that several years ago changed their parking restrictions. They replaced the sign with the old rules and added above the new rules a second large sign that read “NEW.” That worked for a while. Anyone coming into the parking lot couldn’t miss the large bright orange “NEW” sign that called attention to the new rules on the sign below it. However, fast forward several years to today, and the “NEW” sign is still there.

What purpose does the large orange “NEW” sign continue to serve after multiple years have passed? It’s now an old “NEW” sign. What would happen if the school needed to change the parking restrictions again. Would it put up a new sign with the rules and change the “NEW” sign to “NEW NEW”? In retrospect, it would have been better if they had added a sign that said “Effective Sep. 1, 2014.”

Note: I have seen signs that say something like “Effective Sep. 1,” but the problem is, how do you understand the sign when the date is 9 months later? Which Sep. 1st are they referring to, the one from last year or the one in 3 months? In short, it’s easy find something that works for the moment, but the real trick is finding something that works both now and in the future.

What does this have to do with hledger categories? They are like signs for you and for others who will see your hledger reports. So when thinking about categories for hledger, ask yourself:

  • Does this work now?

  • Will it work in the future?

  • For example, if you have one checking account, the following categorization can work:

    Assets:Checking

    However, will you ever open an additional checking account in the future? If so, what will you call it? For example, assume that the above account is with Acme, and the new account is with BigBank. You can name the new account:

    Assets:BigBank:Checking

    Notice, though, that the first account, Assets:Checking, is now ambiguous since you have two checking accounts. You can still successfully run your hledger reports, but you will always have the mental step of remembering that Assets:Checking is your first checking account, the one with Acme, not BigBank.

    There is still more to consider, though. What if you are using hledger to track the expenses for a group of people, and two people have checking accounts with Acme?

    One solution is to add the name of the person, as in the following example:

    Assets:Acme:Mary Checking

    The above is good if you think of the correct category names ahead of time. However, what do you do if you realize you need to change your hledger account names after the fact? This exact problem happened to me. One of my hledger categories, one that I thought would never be duplicated, went from one to three. (Remember, one of the purposes of this website is for you to learn from my mistakes!)

    As I was thinking of possible solutions, I remembered that the sed tool could be very helpful for this. After a few false starts, I found a specific command that would work. First, let’s assume that I need to change two account names in the file myfile.hledger:

    Assets:Checking to Assets:Acme:Mary Checking

    Assets:BigBank:Checking to Assets:BigBank:Fred Checking

    If you need the above changes, here is a sed command that will work:

    sed ‘s/Assets:Checking/Assets:Acme:Mary Checking/; s/Assets:BigBank:Checking/Assets:BigBank:Fred Checking/’ myfile.hledger > myfile.new

    Notice that I output the sed command to a new file (myfile.new). This way, I can check the new file to confirm everything works. If there is a problem, my original file is still intact. On the other hand, if there are no problems, I rename the original file myfile.hledger to something like myfile.bak (in case I ever need the original file) and then rename myfile.new to myfile.hledger.

    The moral of the story: Use some careful thinking about the future when you create your accounting categories, but if you make a mistake, sed can get you back on track.

    [2025 update: I recently wrote a polite email to the principal of the school where the old “new” sign was. I explained how the sign had been there several years. The school principal responded saying they would see if they could remove it, and shortly afterwards the well-worn “new” sign was gone.]