Skip to content

Instantly share code, notes, and snippets.

@BlackthornYugen
Last active July 19, 2022 03:16
Show Gist options
  • Save BlackthornYugen/1fb200f5eec2e423b2a49092fb1633d3 to your computer and use it in GitHub Desktop.
Save BlackthornYugen/1fb200f5eec2e423b2a49092fb1633d3 to your computer and use it in GitHub Desktop.
Bash Autocomplete quick reference

Bash Autocomplete quick reference

Directory

Look for direcotries assuming a relative path.

complete -A directory echo

File

Look for files assuming a relative path.

complete -A file echo

Wordlist

Provide your own complete options. Useful for menu items that don't change. Add -o nospace to prevent a trailing space on your completion.

complete -W "hello hola guten\ tag nǐn\ hǎo salve asalaam\ alaikum konnichiwa olá" echo

Worldlist via function

Use a function to generate list of autocomplete values.

_echo_completions() {
    COMPREPLY+=("appeal")
    COMPREPLY+=("appear")
    COMPREPLY+=("append")
    COMPREPLY+=("apple")
    COMPREPLY+=("apply")
    COMPREPLY+=("bend")
}
complete -F _echo_completions echo

Dynamic

Smarter wordlist function that is context aware.

_echo_completions() {
    COMPREPLY=($(compgen -W "appeal appear append apple apply bend" "${COMP_WORDS[1]}"))
}
complete -F _echo_completions echo

Troubleshoot

Write environment variables to a file and tail the file from another terminal.

_echo_completions() {
    env > tmpfile
    COMPREPLY=($(compgen -W "appeal appear append apple apply bend" "${COMP_WORDS[1]}"))
}
complete -F _echo_completions echo

See also

See my Steam Launcher that lets you open an app by ID or game name: BlackthornYugen/autocomplete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment