Linux – Bash filename tab completion

autocompletebashglobbinglinuxtab completion

I have noticed sometimes that tab completion is helpfully filtered in bash. I am a java programmer and often use the java and javac commands in the terminal for quick or remote tasks.

Say I have the two usual files in a directory: MyProgram.java and MyProgram.class

If I ask bash to tabcomplete from the command prefix java M, it fills straight out to java MyProgram, this is helpful. Clearly either bash is configured to only accept *.class completions for the java command (and knows to strip the file extension), or the java command is telling bash this somehow.

I would like to accomplish the same with a text editor, so I could type in nano M and have it tab complete to nano MyProgram.java rather than pausing at nano MyProgram., is it possible to say, blacklist *.class from nano's tab complete?

Most of all I'd just be interested to know if this is a bash configuration or something program-side.

Best Answer

java has a separate auto-completion script that overrides the default completion mechanism. On my distribution (Arch Linux), it is installed in /usr/share/bash-completion/completions/java.

To override the behaviour of tab-completion, you could create such a script. If you just want to have all .class files ignored for every command, you could set the FIGNORE variable (e.g. in your ~/.bashrc). Example: FIGNORE=.class. From the manual page of bash:

FIGNORE
A colon-separated list of suffixes to ignore when performing filename completion (see READLINE below). A filename whose suffix matches one of the entries in FIGNORE is excluded from the list of matched filenames. A sample value is ".o:~".