Command-line completion (alsotab completion) is a common feature ofcommand-line interpreters, in which the program automatically fills in partially typed commands.
Command line interpreters are programs that allow a user to interact with the underlying operating system by typing commands at a command prompt using a command line interface (CLI), in contrast to pointing and clicking a mouse in a graphical user interface (GUI). Command-line completion allows the user to type the first few characters of a command, program, or filename, and press a completion key (normallyTab ↹) to fill in the rest of the item. The user then pressesReturn or↵ Enter to run the command or open the file.
Command-line completion is useful in several ways, as illustrated by the animation accompanying this article. Commonly accessed commands, especially ones with long names, require fewer keystrokes to reach. Commands with long or difficult to spell filenames can be entered by typing the first few characters and pressing a completion key, which completes the command or filename. In the case of multiple possible completions, some command-line interpreters, especially Unix shells, will list all possible completions beginning with those few characters. The user can type more characters and pressTab ↹ again to see a new, narrowed-down list if the typed characters are still ambiguous, or else complete the command/filename with a trailing space. An alternate form of completion rotates through all matching results when the input is ambiguous.
Completable elements may include commands, arguments, file names and other entities, depending on the specific interpreter and its configuration. Command-line completion generally only works ininteractive mode. That is, it cannot be invoked to complete partially typed commands inscripts orbatch files, even if the completion is unambiguous. The nametab completion comes from the fact that command-line completion is often invoked by pressing thetab key.
Tab completion showed up early in computing history; one of the first examples appeared in theBerkeley Timesharing System for theSDS 940, where if a typed string were ambiguous, the interpreter would do nothing, but if the string wasnot ambiguous, it would automatically complete it without any command from the user. This feature did not work well with the all too frequenttypos, and so was a mixed blessing. This feature was imitated byTenex's developers who made an important change: Tenex used "escape recognition", in which the interpreter would not attempt to autocomplete unless theescape key was struck (thus the name) by the user. The domain was also expanded from only program names on the Berkeley system to both program names and files on Tenex.[1] The Tenex descendantTOPS-20 moved command line completion from command interpreter to the operating system via the COMND JSYS system call, to make it available to other user applications.[2] From there it was borrowed by Unix.
To open the fileintroduction-to-command-line-completion.html withFirefox one would type:
firefox introduction-to-command-line-completion.html
This is a long command to type. Instead we can use command-line completion.
The following example shows how command-line completion works inBash. Other command line shells may perform slightly differently.
First we type the first three letters of our command:
fir
Then we pressTab ↹ and because the only command in our system that starts with "fir" is "firefox", it will be completed to:
firefox
Then we start typing the file name:
firefox i
But this timeintroduction-to-command-line-completion.html is not the only file in the current directory that starts with "i". The directory also contains filesintroduction-to-bash.html andintroduction-to-firefox.html. The system can't decide which of these filenames we wanted to type, but it does know that the file must begin with "introduction-to-", so the command will be completed to:
firefox introduction-to-
Now we type "c":
firefox introduction-to-c
After pressingTab ↹ it will be completed to the whole filename:
firefox introduction-to-command-line-completion.html
In short we typed:
firTab ↹iTab ↹cTab ↹
This is just eight keystrokes, which is considerably less than 52 keystrokes we would have needed to type without using command-line completion.
The following example shows how command-line completion works with rotating completion, such as Windows'sCMD uses.
We follow the same procedure as for prompting completion until we have:
firefox i
We pressTab ↹ once, with the result:
firefox introduction-to-bash.html
We pressTab ↹ again, getting:
firefox introduction-to-command-line-completion.html
In short we typed:
firTab ↹iTab ↹Tab ↹
This is just seven keystrokes, comparable to prompting-style completion. This works best if we know what possibilities the interpreter will rotate through.
complete
andcompgen
commands[4] have been available since the beta version of 2.04[3] in 2000[5] and offers at least Pathname and filename completion.(Be sure to check the "Applies to" section in each article)