Tab completion
How Bash completes commands, paths and variables on Tab, why flags and subcommands only complete with extra scripts, and how to write your own with complete.
History and readline shortcutsArrays and associative arraysFunctions, arguments and scope
Tab completion
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Tab asks Bash to complete the word under the cursor. It picks files, commands or variables from where the word sits. Press it twice, or set show-all-if-ambiguous, to list the choices.
- Out of the box Bash completes only file names, command names, variables and a few more. Completing flags and subcommands (git sta, ls --col) needs completion specs, which the bash-completion package provides.
- A completion spec is attached with complete. complete -W 'a b c' cmd offers a fixed word list and complete -F func cmd calls a function that fills the COMPREPLY array.
- Inside a completion function, COMP_WORDS holds the words typed so far, COMP_CWORD the index of the current one, and compgen -W list -- "$cur" filters candidates by prefix.
- Completion settings that belong to readline, such as completion-ignore-case, go in ~/.inputrc or are set with bind.
What Tab is doing
Typing a long path or an exact flag by memory is slow and error-prone. Tab lets the shell finish the word for you. The shell has to know what could come next, and that depends on where the word sits. After the first word it is a command. Elsewhere it is usually a file. After $ it is a variable name.
Completion is a feature of readline, the line editor from the history lesson, working with Bash. I ran everything below in a real interactive Bash 5.3 on macOS, in a scratch directory with four files and a src/ directory. To list choices on the first Tab instead of the second, I ran bind 'set show-all-if-ambiguous on' first.
What works with no setup
$ cat not<Tab>
notes-old.txt notes.txt
$ cat notes^C
Bash filled in the part all candidates share (notes) and listed the rest. A directory gets a slash, and variables complete after a $:
$ cd s<Tab>
$ cd src/
$ echo $HOM<Tab>
$ echo $HOME/
In command position, Tab completes commands from PATH:
$ gre<Tab>
grep gresource
You can ask for candidates directly with the compgen builtin, which is what completion functions use:
$ compgen -c gre | sort -u | head -5
grep
gresource
$ compgen -W "start stop status" -- st
start
stop
status
Matching is case-sensitive by default. ls RE<Tab> did nothing in my test. After bind 'set completion-ignore-case on', it listed readme.md report.md. A setting like that belongs in ~/.inputrc, since it is a readline option, not a Bash variable.
What does not work without help
Two things I tried got silence:
$ ls --col<Tab> (nothing)
$ git sta<Tab> (nothing)
Bash does not know that ls has a --color flag or that git has a status subcommand. That knowledge lives in completion specs, small rules attached to commands. complete -p lists the ones in force, and in my scratch shell it printed nothing: no specs. On a typical Linux install the bash-completion package loads hundreds of them from your rc file, which is why Tab does more there. I did not have it installed, so I could not run the version you get after installing it. If flag completion works on your machine, that package is why.
Writing your own
You can teach Bash about your own commands in a few lines. Say a tool takes the words start, stop, status and restart. The fixed-list form is complete -W:
complete -W "start stop status restart" ./mytool
When the choices depend on context, write a function and attach it with -F. Bash calls it each time you press Tab, with the typed words in COMP_WORDS and the index of the current one in COMP_CWORD. Its job is to fill the COMPREPLY array:
_mytool() {
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "start stop status restart" -- "$cur") )
}
complete -F _mytool ./mytool
I sourced that file and tried it:
$ ./mytool <Tab>
restart start status stop
$ ./mytool sta<Tab>
start status
$ complete -p
complete -F _mytool ./mytool
$ ./mytool restart
mytool: restart
Unquoted $(compgen ...) splits on whitespace, which is fine for words without spaces. Completing file names with spaces needs more care, and the word-splitting lesson explains why. Completions like this live in an rc file, loaded the way the startup files lesson describes. A completion spec only affects interactive shells, so it never changes how a script behaves.
About the video
The embed is a three-minute clip (1:23:01 to 1:26:08) of “Bash Scripting Deep Dive: From Shortcuts to Production Scripts”, a four-hour live session from the Red Hat Enterprise Linux channel. The description lists chapters, and the clip is the one titled “Tab Completion Power”, which comes after a chapter on command-line tips and before one on shell history shortcuts. That is about using Tab, the first half of this lesson. I could not read a transcript, so I cannot say which of the examples above it shows, and the writing-your-own half is not in the clip. The session is in English and aimed at Red Hat Enterprise Linux, so file locations may differ on a Mac.
Two videos on writing completions are linked under deeper: You Suck at Programming’s 53-minute “How Bash Tab Completion Actually Works”, with chapters on compgen, complete, completion functions and compopt, and evan ugarte’s 18-minute “Add autocompletion to your scripts in bash”, with chapters on the reserved variables and a dev-tool example.
Where this fits
Tab completion, history, aliases and the prompt are all configuration that lives in files. Keeping those files tidy and the same across machines is the last lesson in this group: dotfiles and configuration management.
Check yourself
You type ls --col and press Tab and nothing happens. Is Tab broken?
No. Plain Bash only knows how to complete file names here. Completing a command's flags needs a completion spec for ls, which the bash-completion package installs.
What does compgen -W 'start stop status' -- st print?
It prints the words from the list that start with st: start, stop and status, one per line.
In a function used with complete -F, how do you find the word being completed?
Use COMP_WORDS[COMP_CWORD]. The function then fills the COMPREPLY array with matching candidates.
How do you make completion ignore upper and lower case for the current session?
Run bind 'set completion-ignore-case on'. Put set completion-ignore-case on in ~/.inputrc to make it permanent.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.