Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-complete command line arguments

In bash, executables such as mplayer and imagemagick's "convert" have a cool auto-complete functionality on their command line arguments. For instance, if I type

mplayer <tab><tab> 

in one of my video folders, then mplayer will list all media files located in that folder, and only the media files.

Similarly, if I type

convert -<tab><tab> 

then I will see all the possible options of the convert script, which is great.

My question is how to achieve a similar functionality, using bash, ruby or python scripts?

like image 275
Régis B. Avatar asked Sep 11 '10 10:09

Régis B.


People also ask

How do I use autocomplete in CMD?

Using autocomplete is as simple as pressing the [TAB] and the active command line options will fill-in. If more than one option is available, you can hit [TAB] twice to display all possible choices and continue typing until there is only one matching choice left.

How do I use autocomplete command in Linux?

When at the MS-DOS, Windows command line or a Linux or Unix shell, typing in long file names or directories can become a burden. Use the Tab to autocomplete the names of directories and files while in the command line.

How do you autocomplete in bash?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

Which keystroke can be used to autocomplete a command?

To do that, press “Win + R,” type cmd and press the Enter button to open the command prompt. Once opened, enter the below command and press the Enter button. From this point onward, you can use the auto-complete feature by pressing the control characters “Ctrl + D” for folder and “Ctrl + F” for files.


1 Answers

This is an example of BASH's smart completion. A basic description is here, a guide to writing your own extensions is here and another (Debian-based) guide is here. And here's a fuller featured introduction to the complete command (the command that facilitates this behaviour).

like image 149
Andy Avatar answered Sep 21 '22 16:09

Andy