Bash script auto-complete filename by file type/extension

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:

 <pre class="src src-sh"><span style="color: #ff4500;">##</span><span style="color: #ff4500;">complete -G "*.Rnw" pgfSweave</span>

complete -f -X ‘!.@(Rnw)’ pgfSweave Sweave complete -f -X ‘!.@(eps|ps)’

About Vinh Nguyen


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>