/emacs-native-shell-complete

Completion in shell buffers using native mechanisms

Primary LanguageEmacs LispGNU Lesser General Public License v3.0LGPL-3.0

Native complete

This package interacts with a shell’s native completion functionality to provide the same completions in Emacs that you would get from the shell itself. Currently this has been tested in

  • bash
  • csh
  • various CAD tool shells

Emacs setup

This package contains two completion functions; native-complete-at-point and company-native-complete. native-complete-at-point is a “completion at point function” (capf) and can be enabled by adding it to completion-at-point-functions in a shell buffer. company-native-complete is an asynchronous backend for the company completion framework. It is enabled by adding it to company-backends.

These functions look at the shell prompt or process name to determine which completion style to use. A completion style is the mechanism used to get the candidates from the native shell. Most shells support <tab> , but some shells won’t give you all the candidates consistently without using a different mechanism. There are currently the following completion styles:

bash
M-* and echo
zsh
C-D
tab
<tab>

Which completion mechanism to use is determined by the shell-file-name, which is the executable that is used to start your shell. If the mechanism can’t be determined from that, or if you use subshells that are a different type, you can use native-complete-style-regex-alist. This variable is a alist of (prompt-regex . mechanism) pairs. If the prompt of the current shell matches prompt-regex then the corresponding mechanism will be used. For example, if your prompt is of the form user> and your shell is bash, you could you the following setting to configure that.

(setq native-complete-style-regex-alist '(("[a-z]+> " . bash)))

To test what completion style your shell is using, evaluate M-: (native-complete-get-completion-style) when the point is at an empty prompt. The default completion mechanism is tab.

native-complete-exclude-regex is a regular expression that is used to match potential candidates that should not appear in the final completion list.

shell setup

bash

you will need to execute native-complete-setup-bash before you load your first shell. The following configuration is recommended for the best experience.

export HISTCONTROL=ignoreboth
bind "set page-completions off"
bind "set show-all-if-unmodified on"
bind "set show-all-if-ambiguous on"
bind "set completion-query-items -1"

Also note that completion in bash works best with command echoing turned off. This can be set using TTY.

stty -echo

csh

Set the max candidates to a sufficiently high number for the best experience. Make sure editing is enabled.

set listmax=5000
set listmaxrows=500
set edit