User-defined bash_completion.d

Oct 02, 2020 Script System

Recently I have been writing quite a few Go applications, and using others such as restic & hugo, some of which have their own shell completion. I would prefer to export these completions locally (within my user directory) without having to do it as root each time for the entire system, specifically because I’m dealing with non-system applications which are upgraded manually.

This tutorial explains how to create a user ~/.bash_completion.d and enable this for bash (Bourne shell).

in your ~/.bashrc, add the following:

for bcfile in ~/.bash_completion.d/* ; do
    [ -f "$bcfile" ] && . "$bcfile"
done

Then create ~/.bash_completion.d

$ mkdir ~/.bash_completion.d

Then simply put your bash completion in there and restart your bash session/terminal.

To add shell completion for the above-mentioned apps (as an example):

$ hugo gen autocomplete --completionfile ~/.bash_completion.d/hugo
Bash completion file for Hugo saved to /home/ralph/.bash_completion.d/hugo

$ restic generate --bash-completion ~/.bash_completion.d/restic
writing bash completion file to /home/ralph/.bash_completion.d/restic

Comments