This layer provides auto-completion to Spacemacs.
The following completion engines are supported:
Snippets are supported via yasnippet and auto-yasnippet.
This layer also configures hippie-expand
.
To use this configuration layer, add it to your ~/.spacemacs
. You will need to
add auto-completion
to the existing dotspacemacs-configuration-layers
list in this
file.
You can customize the user experience of auto-completion with the following layer variables:
auto-completion-return-key-behavior
set the action to perform when theRET
key is pressed, the possible values are:complete
completes with the current selectionnil
does nothing
auto-completion-tab-key-behavior
set the action to perform when theTAB
key is pressed, the possible values are:complete
completes with the current selectioncycle
completes the common prefix and cycle between candidatesnil
does nothing
auto-completion-complete-with-key-sequence
is a string of two characters denoting a key sequence that will perform acomplete
action if the sequence as been entered quickly enough. If its value isnil
then the feature is disabled.
The default configuration of the layer is:
(setq-default dotspacemacs-configuration-layers '(
(auto-completion :variables
auto-completion-return-key-behavior 'complete
auto-completion-tab-key-behavior 'cycle
auto-completion-complete-with-key-sequence nil
auto-completion-complete-with-key-sequence-delay 0.1
auto-completion-private-snippets-directory nil)
))
jk
is a good candidate for auto-completion-complete-with-key-sequence
if
you don’t use it already.
The following directories are added by default:
- ~/.emacs.d/elpa/yasnippet-xxxxx/snippets
- ~/.emacs.d/layers/auto-completion/snippets
- ~/.emacs.d/private/snippets (conditional to the value of
auto-completion-private-snippets-directory
) - ~/.spacemacs.d/snippets (conditional to the existence of
~/.spacemacs.d
directory)
You can provide additional directories by setting the variable
auto-completion-private-snippets-directory
which can take a string in case of
a single path or a list of paths.
If its value is nil
then the path ~/.emacs.d/private/snippets
is used.
By default, snippets are not shown in the auto-completion popup. To show them in
the popup, set the variable auto-completion-enable-snippets-in-popup
to t
.
(setq-default dotspacemacs-configuration-layers
'((auto-completion :variables
auto-completion-enable-snippets-in-popup t))
To enable docstring tooltips set auto-completion-enable-help-tooltip
to t
(setq-default dotspacemacs-configuration-layers
'(auto-completion :variables
auto-completion-enable-help-tooltip t))
To enable sorting auto-completion results by their usage frequency set
auto-completion-enable-sort-by-usage
to t
.
This feature is provided by the company-statistics package when company
is used.
The variable has no effect when auto-complete
is used.
(setq-default dotspacemacs-configuration-layers
'(auto-completion :variables
auto-completion-enable-sort-by-usage t))
By default Spacemacs enables auto-completion explicitly for each supported
major-mode, it means that company
and auto-complete
are not enabled
globally, it allows more flexibility to choose an auto-completion engine
for a given mode.
You may want to enable company globally to get auto-completion
everywhere even in the modes which are not configured by Spacemacs. To do
so, you just have to add (global-company-mode)
in the
dotspacemacs/user -config
function of your dotfile.
Note that if you want to enable auto-complete
globally you will have to
disable company
first, see the next section to do so.
You can disable company
by adding it to the dotspacemacs-excluded-packages
variable, then you are free to enable auto-complete
globally.
Here is an example to add company
auto-completion to python buffer:
;; Define the buffer local company backend variable
(spacemacs|defvar-company-backends python-mode)
;; Add the relevant packages to the layer
(setq python-packages
'(...
company
company-anaconda
...))
;; Configure the packages
(when (configuration-layer/layer-usedp 'auto-completion)
;; Hook company to python-mode
(defun python/post-init-company ()
(spacemacs|add-company-hook python-mode))
;; Add the backend to the major-mode specific backend list
(defun python/init-company-anaconda ()
(use-package company-anaconda
:if (configuration-layer/package-usedp 'company)
:defer t
:init (push 'company-anaconda company-backends-python-mode))))
Key Binding | Description |
---|---|
C-j | go down in company dropdown menu |
C-k | go up in company dropdown menu |
C-/ | show candidates in Helm (for fuzzy searching) |
C-M-/ | filter the company dropdown menu |
C-d | open minibuffer with documentation of thing at point in company dropdown |
Key Binding | Description |
---|---|
C-j | select next candidate |
C-k | select previous candidate |
TAB | expand selection or select next candidate |
S-TAB | select previous candidate |
return | complete word, if word is already completed insert a carriage return |
Key Binding | Description |
---|---|
M-/ | Expand a snippet if text before point is a prefix of a snippet |
SPC i s | List all current yasnippets for inserting |
Key Binding | Description |
---|---|
SPC i S c | create a snippet from an active region |
SPC i S e | Expand the snippet just created with SPC i y |
SPC i S w | Write the snippet inside private/snippets directory for future sessions |