If I start typing a command or filename, I can use the tab key to auto-complete my string. Is there a way to change the auto-complete behavior for the current bash session?
For example
On pressing Tab Tab in the current session, only files that contain *.txt
will be displayed.
How does the autocomplete function work? Is there a set of environment variables or something that can be adjusted to change the autocomplete behavior?
Best Answer
In the
bash_completion
mechanism it's the bash function_filedir_xspec()
that is responsible for filename and dirname completions. This funtion can be found in the script/etc/bash_completion
.You can edit that function and add a line that contains your regex. For example use this function (it's a copy of the original function):
Notice the new part at line 32:
This statement is later used in
compgen
to remove all entries, that do not match the regular expression. I wouldn't recommend to edit the file/etc/bash_completion
. Instead make a new file with the content above andsource
the file if needed:Now, all commands/applications in the shell, that where completed by the function
_filedir_xspec()
, complete now only with*.txt
filenames.