Share on Facebook Share on Twitter Email
Answers.com

Command line completion

 
Wikipedia: Command line completion
Example of command line completion in Bash.

Command line completion (also tab completion) is a common feature of command 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 in the first few characters of a command, program, or filename, and press a completion key (normally TAB) in order 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 animated image accompanying this article. Commonly-accessed commands, especially if they have long names, require fewer keystrokes to activate. Commands with long or difficult to spell filenames can be entered by simply entering the first few characters and pressing a completion key, in which the program fills in the rest of the command or filename. A user may have several similarly-named files, and some command-line interpreters, especially UNIX shells, will allow the user to type in a few characters, and upon pressing TAB, will provide a list all filenames beginning with those few characters. The user can then type more characters and press TAB and the program will supply a new, narrowed-down list if the typed characters are still ambiguous. When the string of typed characters is unambiguous, pressing the TAB key will complete the name of the file. The user then presses enter to open the file or run the program.

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.

Contents

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] From there it was borrowed by Unix.

Example

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

To open file introduction-to-command-line-completion.html in web browser one would have to type the command:

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

This is quite long command to type. Instead we can make use of command line completion and only type the first three letters of our command:

 fir

Then we can press Tab ⇆ and because the only command in our system that starts with "fir" is "firefox", it will be completed to:

 firefox

We also need to type the name of the file. Again we make use of command line completion and just type the first letter of the filename:

 firefox i

But this time introduction-to-command-line-completion.html is not the only file in 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 just type letter "c":

 firefox introduction-to-c

After pressing TAB it will be completed to the whole filename:

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

So in short we just typed:

 fir<TAB>i<TAB>c<TAB>

This is just 8 keystrokes, that is considerably less than 52 keystrokes we would have needed to type without using command line completion.

Completion in different command line interfaces

  • Unix shells, including bash (the default shell in Linux and Mac OS X) and ksh among many others, have a long-standing tradition of advanced and customizable completion capabilities (see the external links section below for some examples).
    • For Korn shell users, file name completion depends on the value of the EDITOR variable. If EDITOR is set to vi, you type part of name, and then click Escape followed by a backslash (\) character. If EDITOR is set to emacs, you type part of the name, and then click the Escape key twice to complete the file name.
    • The Z shell (zsh) pioneered the support for fully programmable completion, allowing users to have the shell automatically complete the parameters of various commands unrelated to the shell itself, which is accomplished by priming the shell with definitions of all known switches as well as appropriate parameter types. This allows the user to e.g. type tar xzf Tab ⇆ and have the shell complete only tarred gzip archives from the actual filesystem, skipping other files which are incompatible with the input parameters. A modern zsh installation comes with completion definitions for over five hundred commands.
  • Windows PowerShell, the new extensible command shell from Microsoft, which is based on object-oriented programming and the Microsoft .NET framework provides powerful and customizable completion capabilities similar to those of traditional Unix shells.[2][3][citation needed]
  • The cmd.exe command processor of Windows NT-based systems supports basic completion. It is possible to use a separate key-binding for matching directory names only.
  • The MS-DOS command processor COMMAND.COM did not have command line completion: pressing the tab key would just advance the cursor. Before the release of Windows however various enhanced shells for MS-DOS, such as 4DOS, or the FreeDOS version of command.com, would feature Unix-style tab completion.

See also

References

External links

Unix shells

Windows command interpreters

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


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Command line completion" Read more