Felix Crux

Technology & Miscellanea

Tags: ,

Instead of typing out the same half-dozen options every time you want a ledger report, you can define them once up front in a .ledgerrc file. This does make it annoying when you don't want those specific options, but there are ways around that.

(This post is part of a series describing how I use the Ledger accounting system. For an introduction to ledger and this series, or to see all the entries, have a look at the first post).

There are a few ledger options that you'll almost always want to set whenever you're looking at any kind of report. In my case, these include --exchange $, to show dollar values of investments instead of number of shares; and --sort date to have transactions listed chronologically (I don't know why this isn't the default).

Fortunately, I don't have to remember to type them each time I use ledger, because by default ledger will look for a file named .ledgerrc in your home directory (You can choose a different one with the --init-file FILE option). This file can contain the same options you'd specify on the command-line, except written out one per line.

My .ledgerrc ends up looking a bit like this:

--exchange $
--sort date
--file ~/path/to/your/data.journal

The downside is that this makes it a pain whenever you don't want to use these options, e.g. because you do want to see the number of shares instead of their monetary value. The simple but cumbersome solution is to edit the file every time, temporarily removing that flag.

Another option that I tried (but ran into problems with) is to do a little bit of shell scripting to selectively remove the flags you don't want. I did this by creating a wrapper for the ledger command that sits in my personal ~/bin directory, and thus takes precedence over the real program. This wrapper pre-processes my .ledgerrc to temporarily remove any options I don't want, before handing everything over to the real ledger program. It looks like this:


set -eu


for arg in "$@"; do
  case "$arg" in
      tempfiles="$tempfiles $temporary_ledgerrc"
      sed "s/--$key_to_unset.*//" "$current_ledgerrc" > "$temporary_ledgerrc"
    *) filtered_args="$filtered_args $arg";;

/usr/bin/ledger --init-file "$current_ledgerrc" $filtered_args

[ -z "$tempfiles" ] || rm $tempfiles

However, when I try actually using this, ledger seems to be reading my main journal file twice (?) and all my transactions get duplicated! I have no idea why this is the case or where the bug is, so at present I don't actually use this. Plus, I'm not very fond of this hack, so I'd like to hear about better ways. The best way would probably be to file a feature request for ledger to support something like this interally.

blog comments powered by Disqus