In Emacs shell-mode, this auto-completion doesn't work, even after I explicitly source /etc/bash_completion. The above example sticks as in or auto-completes with a filename in the current directory rather than a valid apt-get command option. Presumably, this is because Emacs is intercepting the Tab key-press. How do I enable smart auto-completion in shell-mode?

7 Answers
7

I know this question is three years old, but it's something that I've also been interested in solving. A Web search directed me to a piece of elisp that makes Emacs use bash for completion in shell mode. It works for me, in any case.

+1 for having the courage to answer a three-year-old, but still totally relevant, question. Thanks!
–
djhaskin987May 22 '12 at 14:23

1

This is already in melpa melpa.org/#/bash-completion, installation is straightforward using M-x package-list-packages, but in the end it didn't work for me, I don't know why, it may be my emacs (24.3.1) or bash (4.3.30) version. The documentation says "bash-completion.el is quite sensitive to the OS and BASH version..." then is a list where my versions are absent.
–
boclodoaOct 30 '14 at 17:34

In the emacs shell, it's actually emacs doing the auto-completion, not bash. If the shell and emacs are out of sync (e.g. by using pushd, popd or some bash user function that changes the shell's current directory), then auto-completion stops working.

To fix this, just type 'dirs' into the shell and things get back in sync.

I don't know the answer to this. But the reason that it doesn't work as you expect is probably because the completion in emacs shells is handled by emacs internally (by the comint-dynamic-complete function), and doesn't have those smart completion functions built-in.

I'm afraid it is not an easy thing to fix.

Edit: njsf's suggestion of using term-mode is probably as good as it gets. Start it with

M-x term

It is included in the standard emacs distribution (and in emacs21-common or emacs22-common on Ubuntu and Debian at least).

Like Matli said, it is not an easy task, since bash is started with --noediting and TAB is bound to comint-dynamic-complete.

One could possibly rebind TAB to self-insert-command in shell-comand-hook with local-set-key
and make shell-mode not start with --noediting by M-x customize-variable RET explicit-bash-args, but I suspect that it will not sit well with all other editing.

You might want to try term-mode, but it has another set of problems, because some of the other regular keybindings are overtaken by term-mode.

EDIT: By other regular keybidings being overtaken by term-mode, I mean all but C-c which becomes the escape to be able to switch buffers. So instead of C-x k to kill the buffer you'd have to C-c C-x k. Or to switch to another buffer 'C-c C-x o' or 'C-c C-x 2'

Please, consider another mode M-x term, like I did this when hit problem in 2011. I try to gather all efforts over Inet at that time to make shell work with Bash completion, including this question. But since discovering alternative in face of term-mode I ever don't want to try eshell.

It is full terminal emulator, so you can run interactive program inside, like Midnight commander. Or switch to zsh completion so you don't lose time spent on Emacs configuration - make once use all life.

With this characteristic - TAB completion in bash you get for free. But more important you get full Readline power, like incremental or prefixed command search. To make this setup more convenient to use check my .inputrc, .bashrc, .emacs.

Essential part of .inputrc:

# I like this!
set editing-mode emacs
# Don't strip characters to 7 bits when reading.
set input-meta on
# Allow iso-latin1 characters to be inserted rather than converted to
# prefix-meta sequences.
set convert-meta off
# Display characters with the eighth bit set directly rather than as
# meta-prefixed characters.
set output-meta on
# Ignore hidden files.
set match-hidden-files off
# Ignore case (on/off).
set completion-ignore-case on
set completion-query-items 100
# First tab suggests ambiguous variants.
set show-all-if-ambiguous on
# Replace common prefix with ...
set completion-prefix-display-length 1
set skip-completed-text off
# If set to 'on', completed directory names have a slash appended. The default is 'on'.
set mark-directories on
set mark-symlinked-directories on
# If set to 'on', a character denoting a file's type is appended to the
# filename when listing possible completions. The default is 'off'.
set visible-stats on
set horizontal-scroll-mode off
$if Bash
"\C-x\C-e": edit-and-execute-command
$endif
# Define my favorite Emacs key bindings.
"\C-@": set-mark
"\C-w": kill-region
"\M-w": copy-region-as-kill
# Ctrl+Left/Right to move by whole words.
"\e[1;5C": forward-word
"\e[1;5D": backward-word
# Same with Shift pressed.
"\e[1;6C": forward-word
"\e[1;6D": backward-word
# Ctrl+Backspace/Delete to delete whole words.
"\e[3;5~": kill-word
"\C-_": backward-kill-word
# UP/DOWN filter history by typed string as prefix.
"\e[A": history-search-backward
"\C-p": history-search-backward
"\eOA": history-search-backward
"\e[B": history-search-forward
"\C-n": history-search-forward
"\eOB": history-search-forward
# Bind 'Shift+TAB' to complete as in Python TAB was need for another purpose.
"\e[Z": complete
# Cycling possible completion forward and backward in place.
"\e[1;3C": menu-complete # M-Right
"\e[1;3D": menu-complete-backward # M-Left
"\e[1;5I": menu-complete # C-TAB

.bashrc (YEA! There is dabbrev in Bash from any word in ~/.bash_history):

No, this doesn't solve the problem. It only works on shell-command, not in shell-mode. And besides, it doesn't enable the smart completion that was requested in the question (it will only complete commands and filenames).
–
matliOct 2 '08 at 19:44

1

shell-command-completion-mode does filename completion and is enabled by default. I want Bash's completion feature (which is extensible to include, e.g., sub-commands for apt-get and svn).
–
Chris ConwayOct 2 '08 at 19:47