Bash script auto-complete filename by file type/extension

Suppose I wrote a bash script with usage script.sh 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 firtpart.whatever.is.next.tex. 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 Sweave.sh complete -f -X ‘!.@(eps|ps)’ eps2pdf.sh

About Vinh Nguyen

Statistician

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>