To install this package, run in Emacs:
M-x package-install RET bash-completion RET
bash-completion.el defines dynamic completion hooks for shell-mode and shell-command prompts that is based on bash completion.
Bash completion for Emacs:
A simpler and more complete alternative to bash-completion.el is to
run a bash shell in a buffer in term mode (
Unfortunately, many Emacs editing features are not available when
running in term mode. Also, term mode is not available in
Copy bash-completion.el into a directory that's on Emacs load-path. You can do that manually, or by installing it from MELPA.
To enable bash completion in shell buffers as well as in command
prompts, such as the prompt started by
compile, add the hook
"BASH completion hook")
or simpler, but forces you to load bash-completion at startup:
After that reload your .emacs (
M-x eval-buffer) or restart.
When called from a bash shell buffer,
bash-completion-dynamic-complete communicates with the current shell
to reproduce, as closely as possible the normal bash auto-completion,
available on full terminals.
When called from non-shell buffers, such as the prompt of M-x compile,
bash-completion-dynamic-complete creates a separate bash process
just for doing completion. Such processes have the environment
EMACS_BASH_COMPLETE set to
t, to help distinguish them
from normal shell processes.
You can also use bash completion as an additional completion function
in any buffer that contains bash commands. To do that, add
bash-completion-capf-nonexclusive to the buffer-local
completion-at-point-functions. For example, you can setup bash
eshell-mode by invoking
'bash-completion-capf-nonexclusive nil t)))
There is also a lower-level function
bash-completion-dynamic-complete-nocomint which allows you to
construct your own
(bash-completion-dynamic-complete-nocomint COMP-START COMP-POS DYNAMIC-TABLE)
COMP-START is where the bash command starts --- it depends on the mode
of the calling buffer. In most cases,
because it uses
COMP-POS is usually the current position of the cursor.
When calling from
completion-at-point, make sure to pass a non-nil
value to the DYNAMIC-TABLE argument so it returns a function instead
of a list of strings. This isn't just an optimization: returning a
function instead of a list tells Emacs it should avoids post-filtering
the results and possibly discarding useful completion from bash.
If completion in a bash shell doesn't behave as you think it should, check the following:
M-x bash-completion-debug and look at the
section. Does it match the expected set of completion? If yes,
it might be a display problem. Are you using a completion engine
other than the default, such as ivy or helm? Try turning it off to
confirm, then file a bug
M-x bash-completion-debug, the command you're trying to use
and the function or package providing completion for that command and
where to download it.
See CONTRIBUTING.md for more details.
bash-completion.el is known to work with Bash 4.2 and later and Bash 5, on Emacs, starting with version 25.3, under Linux and OSX.