hledger/hledger/shell-completion
Simon Michael 64023478b7 imp:accounts: rename --positions to --locations for consistency
with `print`. --positions still works, as a hidden flag.
2025-10-13 10:11:18 -10:00
..
.gitignore imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
BSDmakefile imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
foreach2.m4 imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
hledger-completion.bash imp:accounts: rename --positions to --locations for consistency 2025-10-13 10:11:18 -10:00
hledger-completion.bash.m4 imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
hledger-completion.bash.stub imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
Makefile make: shellcompletions: top level rule to build shell completions 2021-07-20 21:29:52 -10:00
parse-commands.sh ;dev: shell-completion: fix/update commands list parsing 2024-09-14 17:30:29 -07:00
parse-options.sh imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
query-filters.txt imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
quote.m4 imp: pkg: move shell-completions into hledger package 2021-07-20 20:55:14 -10:00
README.md ;doc:completions: readme edits 2025-07-24 20:09:41 +01:00

hledger shell completions

This code generates shell completion scripts for hledgers command line interface. Context-sensitive completions are usually triggered by pressing the tab key once or twice after typing the command hledger (or hledger-ui or hledger-web).

bash completions

An old demo:

asciicast

To use hledgers bash shell completions, these must be installed on your system: - bash - the bash-completion package - hledgers bash completion script - hledger, hledger-ui, and/or hledger-web

For latest user setup instructions and feature list, please see Install > Shell completions.

Generating the bash completions

In the main hledger repo, first build and install an up-to-date version of hledger in PATH, then run make to detect its commands and options and generate the hledger-completion.bash script. GNU make and GNU m4 are required.

stack install hledger
cd hledger/shell-completion
make

This is normally done at release time. The makefile runs hledger, hledger -h, and hledger CMD -h for each command, to detect (built-in) command names, flag names and flag values, and it combines these with a stub script to form the final completion script. You can add -jNUMPROCS to make it run faster.

Data from the journal (account names, payee names etc.) is not gathered here; the script detects those dynamically at completion time.

Installing the bash completions

(If you want a released version of the completion script, use the Install page link above instead.)

To install the dev completion script, also regenerating it if needed:

make install

This will copy hledger-completion.bash to $XDG_DATA_HOME/bash-completion/completions/hledger. (See bash-completions docs for other places you can install the script if needed.) bash will use it when completing command lines beginning with “hledger” or “hledger-ui” or “hledger-web”.

You can also just source the script it in your shell startup; this will load completions eagerly, adding a delay to your shell start up time. Or you can source it in your current bash session for testing.

Packaging the bash completions

The hledger-completion.bash script is available in the hledger package tarball on Hackage, and on Github in the release bindists or at https://github.com/simonmichael/hledger/blob/TAG/hledger/shell-completion/hledger-completion.bash. (Eg: https://github.com/simonmichael/hledger/blob/1.43/hledger/shell-completion/hledger-completion.bash)

After it has been loaded, this script will provide completions for hledger, hledger-ui and hledger-web. Note if it is installed only as hledger, bash-completion will not load it until the user types hledger<SPACE><TAB>. Consider also symbolic-linking it as hledger-ui and hledger-web, so that hledger-ui<SPACE><TAB> and hledger-web<SPACE><TAB> will also load it.

hledger bash completions packaging status as of 1.40:

  • Homebrew: includes slightly stale completions, installed as hledger only
  • Arch Linux AUR: packaging in progress
  • Others: unknown

zsh completions

hledger completions are shipped with zsh: https://github.com/zsh-users/zsh-completions/blob/master/src/_hledger. They may be out of date, please help by updating them.

fish completions

hledger completions are shipped with fish: https://github.com/fish-shell/fish-shell/blob/master/share/completions/hledger.fish They may be out of date, please help by updating them.

Creating/updating completion scripts

A rough guide:

  1. Create an m4 template for your completion script, eg hledger-completion.SHELL.m4 where SHELL is the name of the shell.

  2. Add a rule in Makefile to transform this to hledger-completion.SHELL.

  3. The current hledger commands, options and command options are listed in commands.txt, generic-options.txt, and options-*.txt, generated by the Makefile. Use m4 to include these options and commands into hledger-completion.SHELL.m4. See hledger-completion.bash.m4 as an example.

  4. Use make and then make hledger-completion.SHELL to generate and test the completion script.

  5. Then: add the generated script hledger-completion.SHELL to the upstream shell repo, so that it will be easily available to users. If thats not possible, add it here for now. And if you can, please help to keep it up to date with new hledger releases.