Bash script auto-complete filename by file type/extension


Vinh Nguyen


February 12, 2011

Suppose I wrote a bash script with usage filename.type. That is, the script takes in filenames as arguments. Bash's tab completion works with all files. However, I've seen commands that will complete the filename even faster because it will select the file with the "right" extension. For example, pdflatex firstpart[Tab] would automatically select I'd like this feature for the scripts I write as well. Thanks to this post and this, I found out it is implemented through the complete function in bash. For installed programs like pdflatex, the complete commands are added to /etc/bash_completion through the installer script (I think). For me, I can add them to my ~/.bashrc:

##complete -G "*.Rnw" pgfSweave
complete -f -X '!*.@(Rnw)' pgfSweave Sweave
complete -f -X '!*.@(eps|ps)'