And how does it also autocomplete aliases?
Asked
Active
Viewed 1.4k times
3 Answers
22
Depending on the command:
- Someone may have written a function to generate possible completions of arguments, including options. You'll find functions for some commands in
/etc/bash_completion.d/*(or a different location on some systems). These functions are registered with thecompletebuilt-in (e.g.complete -F _find findtells bash to call the_findfunction when you press Tab on afindcommand). They use thecompgenbuilt-in to tell bash “here are the possible completions”. - For some commands, bash will call the command with the argument
--helpand parse the output. Such commands can be registered with thecompletebuilt-in, e.g.complete -F _longopt ls._longoptis in fact a completion generation function, that happens to parse a command's output rather than use a fixed list. (There are other more specialized completion functions that parse a command's output to generate possible completions; look in/etc/bash_completion.d/*for examples.) - For things like aliases, the completion function looks them up in bash's internal tables. The
completebuilt-in has options for that, e.g.-Afor aliases.
Gilles 'SO- stop being evil'
- 807,993
- 194
- 1,674
- 2,175
9
Have a look at the file
/etc/bash_completion
and observe the files from the directory:
/etc/bash_completion.d
You will find the answer.
SHW
- 14,454
- 14
- 63
- 101
2
Support for bash completion is provided in Debian and Ubuntu by the bash completion package. You also usually need to uncomment the following in /etc/bash.bashrc and/or .bashrc to source the bash completion files. The following is from /etc/bash.bashrc:
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
for this to work. I usually uncomment it in both. The handling of interactive/login shells in Debian is a bit of a mess.
Similar comments presumably apply to other distributions, though maybe they enable the sourcing by default.
Faheem Mitha
- 34,649
- 32
- 119
- 183