Let's say I have a filename and different extensions to it:
test.tex
test.pdf
It is easier to type just the filename without extension, then the whole filename. This is possible, because corresponding program know the extension which they need and append it automatically. The following examples work perfectly:
tex test
pdf-viewer test
But using bash completion here constantly annoys me by adding the trailing dot. For example:
tex te<TAB>
is turned into
tex test.
But the application will fail with error; I have to hit backspace each time after I hit <TAB>
🙁
How to tell bash not to add trailing dot when doing autocompletion?
Best Answer
There might be an easier way, but this seems to work:
This first sets up a shell function called
texfiles
. It uses the second command line argument ($2
, the word typed by the user on the command line before pressing Tab to complete the word), and populates theCOMPREPLY
array with all names that matches"$2"*.tex
.For each of these names, the suffix
.tex
is removed before adding it to the array.The
complete
command is then used to tell the shell to execute thetexfiles
function for file name completion for thetex
command specifically. You may repeat the samecomplete
command for all commands that should complete.tex
files in the same way. Forpdf-viewer
you may want to create a correspondingpdffiles
function (the names of these functions are arbitrary).This is it in action (
<tab>
signifies me pressing the Tab key):(the
ile
infile
is inserted by the shell)Read the "Programmable Completion" section of the
bash
manual for the gory details.