/home/jtanguy/blog

Simple autocomplete with fish

Julien Tanguy, . Tagged

Making your own autocomplete functions is easy with fish. Lately I wanted to have autocompletion of npm projects in a lerna-managed monorepo.

The documentation

Adding completions to an existing command is not as hard as for bash/zsh. The documentation is quite helpful here.

The only tricky bit here is to get the list of possible values for our complete command. Fortunately, lerna provides the ls command to list all subpackages. We need a little bit of trickery to only output what we need with lerna --loglevel silent ls --all 2> /dev/null

The code

All options are provided in their long form, but you can use the short one also. I use home-manager to append the followings to my $XDG_CONFIG_HOME/fish/config.fish

# Auto-complete lerna scope
complete --command lerna --exclusive --long-option=scope --arguments="(lerna --loglevel silent ls --all 2> /dev/null)"
complete --command lerna --exclusive --long-option=ignore --arguments="(lerna --loglevel silent ls --all 2> /dev/null)"