Command-line completion

Last updated
Example of command-line completion in Bash. Command-line-completion-example.gif
Example of command-line completion in Bash.
Example of command-line completion in PowerShell with Intellisense. Powershell Intellisense example for the Get-Process cmdlet.gif
Example of command-line completion in PowerShell with Intellisense.

Command-line completion (also tab completion) is a common feature of command-line interpreters, in which the program automatically fills in partially typed commands.

Contents

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 (normally Tab ↹) to fill in the rest of the item. The user then presses Return 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 press Tab ↹ 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 in interactive mode. That is, it cannot be invoked to complete partially typed commands in scripts or batch files, even if the completion is unambiguous. The name tab completion comes from the fact that command-line completion is often invoked by pressing the tab key.

History

Tab completion showed up early in computing history; one of the first examples appeared in the Berkeley Timesharing System for the SDS 940, where if a typed string were ambiguous, the interpreter would do nothing, but if the string was not ambiguous, it would automatically complete it without any command from the user. This feature did not work well with the all too frequent typos, and so was a mixed blessing. This feature was imitated by Tenex's developers who made an important change: Tenex used "escape recognition", in which the interpreter would not attempt to autocomplete unless the escape 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 descendant TOPS-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.

Example

To open the file introduction-to-command-line-completion.html with Firefox one would type:

 firefox introduction-to-command-line-completion.html

This is a long command to type. Instead we can use command-line completion.

Prompting completion

The following example shows how command-line completion works in Bash. Other command line shells may perform slightly differently.

First we type the first three letters of our command:

 fir

Then we press Tab ↹ 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 time introduction-to-command-line-completion.html is not the only file in the current directory that starts with "i". The directory also contains files introduction-to-bash.html and introduction-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 pressing Tab ↹ 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.

Rotating completion

The following example shows how command-line completion works with rotating completion, such as Windows's CMD uses.

We follow the same procedure as for prompting completion until we have:

 firefox i

We press Tab ↹ once, with the result:

 firefox introduction-to-bash.html

We press Tab ↹ 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.

Completion in different command line interfaces

See also

Related Research Articles

<span class="mw-page-title-main">Bash (Unix shell)</span> GNU replacement for the Bourne shell

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions and it was one of the first programs Linus Torvalds ported to Linux, alongside GCC. A version is also available for Windows 10 and Windows 11 via the Windows Subsystem for Linux. It is also the default user shell in Solaris 11. Bash was also the default shell in BeOS, and in versions of Apple macOS from 10.3 to 10.15, which changed the default shell to zsh, although Bash remains available as an alternative shell.

<span class="mw-page-title-main">Shell script</span> Script written for the shell, or command line interpreter, of an operating system

A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.

<span class="mw-page-title-main">Unix shell</span> Command-line interpreter for Unix operating system

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

<span class="mw-page-title-main">C shell</span> Unix shell

The C shell is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed in 1978. Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.

<span class="mw-page-title-main">Z shell</span> Unix shell

The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.

tcsh Unix shell based on and compatible with the C shell

tcsh is a Unix shell based on and backward compatible with the C shell (csh).

A filename extension, file name extension or file extension is a suffix to the name of a computer file. The extension indicates a characteristic of the file contents or its intended use. A filename extension is typically delimited from the rest of the filename with a full stop (period), but in some systems it is separated with spaces.

cd (command) Computer command in various operating systems

The cd command, also known as chdir, is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard and *.txt is a glob pattern. The wildcard * stands for "any string of any length including empty, but excluding the path separator characters ".

cmd.exe Command prompt program

Command Prompt, also known as cmd.exe or cmd, is the default command-line interpreter for the OS/2, eComStation, ArcaOS, Microsoft Windows, and ReactOS operating systems. On Windows CE .NET 4.2, Windows CE 5.0 and Windows Embedded CE 6.0 it is referred to as the Command Processor Shell. Its implementations differ between operating systems, but the behavior and basic set of commands are consistent. cmd.exe is the counterpart of COMMAND.COM in DOS and Windows 9x systems, and analogous to the Unix shells used on Unix-like systems. The initial version of cmd.exe for Windows NT was developed by Therese Stowell. Windows CE 2.11 was the first embedded Windows release to support a console and a Windows CE version of cmd.exe. The ReactOS implementation of cmd.exe is derived from FreeCOM, the FreeDOS command line interpreter.

<span class="mw-page-title-main">Bad command or file name</span>

"Bad command or file name" is a common and ambiguous error message in MS-DOS and some other operating systems.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

<span class="mw-page-title-main">Shell (computing)</span> Computer program that exposes an operating systems services to a human user or other programs

In computing, a shell is a computer program that exposes an operating system's services to a human user or other programs. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. It is named a shell because it is the outermost layer around the operating system.

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.

In computing, pushd and popd are a pair of commands which allow users to quickly switch between the current and previous directory when using the command line. When called, they use a directory stack to sequentially save and retrieve directories visited by the user.

In a Unix shell, the full stop called the dot command (.) is a command that evaluates commands in a computer file in the current execution context. In the C shell, a similar functionality is provided as the source command, and this name is seen in "extended" POSIX shells as well.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as a user-friendly alternative to punched cards.

References

  1. "Origins and Development of TOPS-20". www.opost.com. Archived from the original on 2020-08-01. Retrieved 2006-09-10.
  2. DECSYSTEM-20 Assembly Language Guide
  3. 1 2 "Working more productively with bash 2.x/3.x". www.caliban.org.
  4. "Bash Reference Manual". tiswww.case.edu.
  5. "Index of /gnu/bash". ftp.swin.edu.au.
  6. "The PowerShell Guy". thepowershellguy.com.
  7. "The PowerShell Guy". thepowershellguy.com.
  8. "Simon Peyton Jones at Microsoft Research". Microsoft .

Unix shells

Windows command interpreters

(Be sure to check the "Applies to" section in each article)