Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have noticed that, when I type unzip in bash and press tab, auto-complete works only on files with .zip extension. How is that done? What if I would like to make program that would accept only image files, how to restrict auto-complete to certain extensions (eg .jpg and .png only).

Is this some kind of system setting or an programming setting that vary for languages (bash, C++, python) ?

share|improve this question
    
As a hint only: I believe shell completion of this kind is programmable. So if you take an image viewer, say feh, you can program the completion for it to only 'react' to jpegs and pngs. –  schaiba Feb 28 '13 at 21:58

2 Answers 2

up vote 4 down vote accepted

The program itself runs whatever file names are passed to it. The restriction to a .zip suffix in completion is unrelated to what the program does. Completion is performed by the shell. When you press Tab, the shell parses the command line to some extent looks up the completion rules for the context. Depending on the shell, the context analysis and completion rules may be more or less complex. For example, when the command line contains echo $P and you press Tab, most shells with completion look for variable names beginning with P. By default, shells complete file names, because it's a very common case.

Bash, tcsh and zsh have ways to make the completion programmable: they parse the command under the cursor, and look up the rules for that particular command in a table. With programmable completion, the entry in the table for the unzip program should say that the first argument must be an existing .zip file, and subsequent arguments must be names of members of the zip archive.

The completion rules are not contained in the program itself, but they may be shipped alongside the program in the same package. For example, a package that contains a command /usr/bin/foo can contain a file /etc/bash_completion/foo that describes to bash how to complete arguments for the foo command, and a similar file /usr/share/zsh/functions/Completion/_foo for zsh.

share|improve this answer

Bash command-line completion is customizable via bash scripts. In Gentoo Linux you can easily manage what completion you would like to have enabled using eselect bashcomp command. When enabled, symlink to system-wide script is created in your ~/.bash_completion.d/ directory. You could use this scripts as a first resource to find out how autocompletion is done with bash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.