Bash

Implementing Autocompletion with Bash complete Command

This article introduces how to implement autocompletion with the complete command on the Bash language command line for Linux PCs, servers, and Ubuntu. Also called completion. Tab completion. In English, it's called autocompletion. It's a convenient feature. Some people may be implementing customized autocompletion using fzf, but fzf is fzf, and even though it's written in Go...

Shou Arisaka
2 min read
Sep 29, 2025

This article introduces how to implement autocompletion with the complete command on the Bash language command line for Linux PCs, servers, and Ubuntu.

Also called completion. Tab completion. In English, it’s called autocompletion. It’s a convenient feature.

So, how do you implement such autocompletion?

Some people may be implementing customized autocompletion using fzf, but fzf is fzf, and even though it’s written in Go, it’s heavy, and for a small implementation, it’s too much.

I’ve implemented my own custom function, and when I tab complete with that function, I just want it to tab complete commands or functions like the type command. I want to provide candidates similar to the cd command’s tab completion.

If you’ve implemented a custom function called types() and want to tab complete with commands as candidates similar to type, do the following:

complete -A command types

Image

If you want to provide candidates similar to the cd command’s tab completion:

complete -F _cd hoge

These might be useful:

-A action The action may be one of the following to generate a list of possible completions:

-C command command is executed in a subshell environment, and its output is used as the possible completions.

-F function The shell function function is executed in the current shell environment. When it is executed, $1 is the name of the command whose arguments are being completed, $2 is the word being completed, and $3 is > the word preceding the word being completed, as described above (see Programmable Completion). When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable.

With the -A option, you can easily assign tab completion candidates in a template-like manner. Choose your favorite value from the following: ``` alias arrayvar binding builtin command directory disabled enabled export file function group helptopic hostname job keyword running service setopt shopt signal stopped user variable ``` For a detailed explanation, see the following reference:

Bash Reference Manual bash - What’s the use of complete command? - Ask Ubuntu

If you look at the bash_completion file, you’ll understand, but the code is complex and you don’t feel like reading it. Even type _cd doesn’t make sense what’s written…

I think the method introduced this time is good for simple autocompletion, but for those who want to implement complex autocompletion, I recommend using fzf.

fzfでBashコマンドラインをめっちゃ便利にする

Share this article

Shou Arisaka Sep 29, 2025

🔗 Copy Links