/emacs-native-shell-complete

Completion in shell buffers using native mechanisms

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

Native complete

https://melpa.org/packages/native-complete-badge.svg https://github.com/CeleritasCelery/emacs-native-shell-complete/workflows/CI/badge.svg

This package provides the exact same completions in shell-mode as you get in a terminal emulator with the TAB key. This means that completions are always accurate and synchronized with your current directory and shell environment. This also means that subshell completion works correctly. Blog

images/demo.gif

Currently this has been tested in

  • bash
  • csh
  • zsh
  • sqlite

Setup steps

Emacs setup

Install from Melpa with M-x package-install.

This project contains two completion packages, native-complete for basic auto-complete completion and company-native-complete for company-mode. Choose one.

If using default (built-in) completion

For users of the built-in auto-complete package, native-complete provides native-complete-at-point which is a “completion at point function” (capf) and can be enabled by adding it to completion-at-point-functions in a shell buffer.

If using company mode

For users of the add-on company package company-native-complete provides a function of the same name which is an asynchronous backend for the company completion framework. It is enabled by adding it as a company backend. For example, you could add the following to your init file.

(add-to-list 'company-backends 'company-native-complete)

Customize the prompt regexp

You need to set the variable comint-prompt-regexp to a regex that will match your shell prompt. The following is a good default for bash, zsh, and csh.

(add-hook 'shell-mode-hook (lambda () (setq comint-prompt-regexp "^.+[$%>] ")))

shell setup

bash

Run native-complete-setup-bash in Emacs before you load your first shell. Add this to your emacs configuration file.

(with-eval-after-load 'shell
  (native-complete-setup-bash))

If the HISTCONTROL environment variable is not set to ignorespace or ignoreboth you will get a lot of garbage in your shell history. We also need to disable bracketed-paste. Add these to your .bashrc file.

export HISTCONTROL=ignoreboth
bind 'set enable-bracketed-paste off'

csh

Make sure editing is enabled.

set edit

Check the config

Once the above steps are complete, run native-complete-check-config in a shell buffer to check for common problems. If it reports success you should be good to go.

Advanced setup

For most users this package should work out the box after the above steps. However if you are using an unusual shell or are invoking subshells that are a different type then your main shell, you may need add some additional configuration.

Native complete look at the shell prompt or process name to determine which completion style to use. A completion style is the key 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 style (key sequence). There are currently the following completion styles:

bash
M-*
zsh
C-D
tab
<tab>
sqlite
<tab> <tab>

Which completion style to use is determined by the shell-file-name, which is the executable that is used to start your shell. If the style 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 . style) pairs. If the prompt of the current shell matches prompt-regex then the corresponding style 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, call native-complete-check-config from a shell buffer. The default completion style is tab.

You can add your own completion styles by adding to native-complete-style-suffix-alist. This is an alist of (style . completion-suffix) pairs. If you discover a new completion style that would be generally applicable, opening a PR would be appreciated. This will improve the supported shells of native-complete out of the box.

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

prompt components in completion list

if you encouter an issue where parts of your shell prompt are showing up as completion candidates you can add a function to comint-redirect-filter-functions to remove the prompt line from the process output. See the doc string of that variable for more details.