1. Introduction

In Bash, it’s possible to autocomplete commands or arguments by typing them partially and pressing the tab key. Further, we can use the compgen built-in command to retrieve all possible options for autocompletion in the shell. This includes files, command aliases, builtins, etc.

In this tutorial, we’ll look at how to use compgen to obtain a list of completion options for a given input string. All of the examples we’ll see involve printing output to the shell. However, in a real-life scenario, we’re more likely to use the compgen command inside another program or script.

2. Basic Usage With Options

Let’s start by looking at the help output for the command:

$ help compgen
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
    Display possible completions depending on the options.
    
    Intended to be used from within a shell function generating possible
    completions.  If the optional WORD argument is supplied, matches against
    WORD are generated.
    
    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

The output tells us that we can use the command in the most basic form as compgen -[options] [word], where options can be any of abcdefgjksuv. Obviously, each of the characters has a special meaning:

  • -a: aliases
  • -b: builtins
  • -c: all commands
  • -d: directories
  • -e: exported shell variables
  • -f: files
  • -g: groups
  • -j: jobs
  • -k: shell reserved words
  • -s: services
  • -u: user names
  • -v: variables

We can use one or more of these options as we need.

Further, let’s see some examples below:

$ compgen -v XDG
XDG_CONFIG_DIRS
XDG_CURRENT_DESKTOP
XDG_DATA_DIRS
XDG_GREETER_DATA_DIR
XDG_MENU_PREFIX
XDG_RUNTIME_DIR
...

$ compgen -df Fira
FiraCode75-Regular.otf
Fira_Code
Fira_Code

First, we used the option -v to look for variables starting with XDG. Subsequently, we used the options d and f to look for files or directories starting with Fira in the current directory. Since the f option also includes directories, we see that the directory Fira_Code is displayed twice, once from the d option and once from the f option.

3. Advanced Usage

The compgen command also gives us the ability to specify other arguments such as -A-G-W, -F-C-X-P, and -S. Let’s see what each of these can do:

  • -A: Choose from a list of actions to run the completion.
  • -G: Use a pattern to expand and generate completions.
  • -W: Use a custom word list to match against.
  • -F: Run a shell function, and use the COMPREPLY array to match against after the function returns.
  • -C: Run a command and use its output as the list of words to match against.
  • -X: Use a pattern to filter and remove options, after applying all other options and arguments.
  • -P and -S: Add a prefix or suffix respectively, to each completion, after all other options have been applied.

Next, let’s see what some of these arguments do:

$ compgen -f -G *.otf
Inconsolata for Powerline.otf
FiraCode75-Regular.otf

$ compgen -d -P dir-
dir-Monoid-XtraLarge-Dollar
dir-Roboto_Mono
dir-Fira_Code
dir-Jost
dir-Source_Code_Pro
dir-powerline-fonts

$ compgen -W 'mon tue wed thu fri sat sun' s
sat
sun

In the above three examples, firstly we see that the -G option matches using a pattern to complete. Subsequently, the -P option outputs the options with the given prefix. Lastly, the -W uses the word list supplied for generating the completions.

4. Conclusion

In this article, we looked at different ways to use the compgen command to generate completions for a given word. Thus, we can use the command in other programs and Bash scripts for generating custom completions.

Comments are closed on this article!