A User's view of Hledger

Learning about hledger

Include was the key

My hledger data files are organized one file per year. For example, for the calendar year 2023 I have a file named 2023.hledger. This normally works just fine.

However, I recently needed to to query 9 years of data without having to reference each individual file. In other words, I didn’t want to write something like:

hledger -f 2015.hledger -f 2016.hledger -f 2017.hledger [etc. all the way to] 2023.hledger

I wanted to write something much shorter, such as:

hledger all-years reg expenses:groceries

In the above, all-years would be understood as a reference to all the hledger data files.

What didn’t work

My first thought was to use a bash alias. I don’t believe that I had ever created one, but I was familiar with the concept that an alias stands for something else. Unfortunately, aliases, at least in bash, are for commands only. Since the things being referred to were references to files, this wouldn’t work.

Some more research pointed me to shell variables. Shell variables, unlike aliases, are not limited to comands, so this approach sounded more promising. I next created a shell variable set equal to “-f 2015.hledger -f 2016.hledger” etc. I referenced the shell variable with a $ in front of it. And I got an error message. Perhaps there was a way to make this work, but I didn’t see it.

Back to the hledger documentation

I was quite sure that my problem had been discussed in the hledger documentation, so it was time to read that. Bingo. I found that one way to solve the problem is to create a file with include statements. Mine looked like this:

include 2015.hledger
include 2016.hledger
include 2017.hledger
include 2018.hledger
include 2019.hledger
include 2020.hledger
include 2021.hledger
include 2022.hledger
include 2023.hledger

I named the file all.hledger. Now, to run a command over all the years of data, I type something like:

hledger -f all.hledger reg expenses:groceries

Problem solved. At least until I create my file for 2024, at which point I will need to include it in the all.hledger file.