Hledger: Always Strict Mode

Gemma BlackGemma Black
3 min read

For me, Hledger makes double-entry accounting simple. In comparison to other fully-fledged do-it-all applications, being able to type into a plain text file and generate reports, a GUI, balance sheets and income statements is powerful.

However, I've discovered Hledger in strict mode makes things easier.

Catching typos

I can make lots of silly mistakes in plain text. For example, when I finally came to closing the accounts, I had inconsistencies due to typos in my accounts. It was simple enough to fix them, but imagine handling hundreds of transactions with these errors!

Entering correct information the first time is far easier than trying to fix things after the fact. Thankfully with strict mode, we can't have little accidents like this.

By adding -s, we can get Hledger to check our accounts for us. Here's an example of an error message when I made a typo under strict mode doing hledger web -s:

hledger-web: Error: main.journal:15:
   | 2023-09-02 Advertising
15 |     expense:advertising             10 GBP
   |     ^^^^^^^^^^^^^^^^^^^
   |     assets:bank                    -10 GBP

Strict account checking is enabled, and
account "expense:advertising" has not been declared.
Consider adding an account directive. Examples:

account expense:advertising
account expense:advertising    ; type:A  ; (L,E,R,X,C,V)

Changing account order in Web mode

By default in Hledger, accounts in web mode are ordered as:

  1. Assets

  2. Equity

  3. Expense

  4. Income

  5. Liabilities

However, I noticed my expenses account was enormous in comparison to all the other accounts. With strict mode, as we have to declare accounts upfront, Hledger will use whatever order you declare.

So, I changed the order to this:

  1. Liabilities

  2. Income

  3. Equity

  4. Assets

  5. Expenses

How to order accounts in Hledger

You have to declare the top-level account eg. account expenses and not just account expenses:manufacturing before any transaction that uses them. I did it top-level accounts with each of their subaccount types together.

; Liabilities
account liabilities 
account liabilities:loan:director

; Income
account income
account income:bank-coupon

; Equity
account equity
account equity:opening
account equity:share-capital

; Assets
account assets
account assets:bank
account assets:equipment

; Expenses
account expenses
account expenses:manufacturing

; Commodities
commodity 1.00 GBP

But nothing is stopping you from grouping the top-level accounts first, and then all your sub-accounts afterwards.

account liabilities
account income
account equity
account assets
account expenses

; Liabilities 
account liabilities:loan:director

; Income
account income:bank-coupon

...etc

Managing account declarations in a separate file

If all of these accounts are too verbose for the main transactions file, you can move it out into a separate file and include it back. That keeps it nice and organised.

include accounts.journal

Always keeping Strict Mode on

If you have a new journal, adding strict mode will probably be a lot easier than having to retrofit it into an old journal. It might not be worth the effort. But for new journals, I've decided to always have strict mode by creating a bash alias. So that whenever I run Hledger, I catch silly issues upfront. As I use zsh, I added the alias to my zsh profile:

echo 'alias h="hledger -s"' >> ~/.zshrc
0
Subscribe to my newsletter

Read articles from Gemma Black directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gemma Black
Gemma Black

I'm a Senior Software Engineer. With 10+ years working within tech teams, and 20+ years working with code, I develop across the stack, assisting with application design, maintenance, deployment and DevOps within the AWS Cloud.