Command line completion

Command-line completion is a feature in shells (Bash, Fish, Zsh, ...) to automatically complete a command, argument, or option. Usually, the completion is triggered by pressing the tab key once or twice after typing hledger . (The exact behavior may differ in shells other than Bash.)

asciicast

The completions handle hledger's CLI:

  • commands and generic options
  • command-specific options
  • filenames for options that take a filename as argument
  • account names from journal files (but not yet for files named by --file)
  • query filter keywords like status:, tag:, or amt:

Installation for end users

Completions are currently only implemented for the Bash shell.

Please check first if the completions for hledger are already installed on your distribution. Refer to the last paragraph of this section for how to test that.

To install the completions manually, follow this steps:

  • Download or copy the file hledger/shell-completion/hledger-completion.bash and save it as ${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions.

    Note: Prior to version 1.25, the shell-completion directory was at the repository root (not hledger/shell-completion). Update the URLs, taking this into account, if you are trying to download the completion script for an older version of hledger such as 1.21.

  • Then, you have to start a new Bash, e.g. by typing bash on the current shell.

Example installation script:

url='https://raw.githubusercontent.com/simonmichael/hledger/master/hledger/shell-completion/hledger-completion.bash'
curl "$url" > "${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions"
bash  # open a new bash to try it

Now, try it by typing hledger (with a space after the command) and press the tab key twice. You should see a list of appropriate completions for hledger. Then you can type a part of one of the suggestions and press tab again to complete it. If you only see filenames, the completions are not correctly installed.

Completion scripts for other shells

You're welcome to add completion scripts for other shells (e.g. Fish or Zsh)! It should not be too hard. All available hledger options and commands are already there. Only the shell hooks and logic is missing.

Please refer to the README in the shell-completion folder.