3

Lately, I'm working on how to configure the best grouped company-backends for different major modes, although having been using company-mode for over one year (already got a configuration long time ago), but recently realized it was not working as I expected, and started looking for the official documentation on how to configure it, but found nothing useful.

For example in emacs-lisp-mode, words in buffers, yasnippet, elisp(company-capf), etc., sorted in this order in the droplist

in c/c++-mode, words in buffers, yasnippet, the name(variable, function, struct etc.), etc,.

in org-mode, words in buffers, yasnippet, etc,

in latex-mode, words in buffers, yasnippet, latex-environment, etc.

If I insert and "/", it should use company-files as the primary backend.

In above examples, there will be a default company-backends for all the major modes when company-mode is enabled, there will be an extra configuration for a specific minor mode to work with the default.

For example:

(setq company-backends
  '(
    (
     company-files
     company-dabbrev
     company-keywords
     company-capf
     company-yasnippet
     )
    (company-abbrev company-dabbrev)
    ))
(add-hook 'emacs-lisp-mode-hook
      (lambda ()
        (set (make-local-variable 'company-backends)
             (append company-backends
                     '((
                        company-elisp
                        ))))))

But I cannot make the company-elisp work, there always are such problems for other major modes, the two backends won't work well for a specific major mode. there always are one or more backends is not working.

No official documentation or examples.

How to create grouped company-backends list to work with an extra one for specific major mode?

5

I faced with this exact same problem a couple of weeks back. company-mode was not behaving as expected for some major modes and for major modes I got no completion at all.

And it turns out the solution was pretty simple. Here's my config for emacs-lisp-mode :-

(use-package emacs-lisp-mode
  :init
  (defun my-elisp-mode-hook ()
    "Hook for `emacs-lisp-mode'"
    (set (make-local-variable 'company-backends)
         '((company-capf company-dabbrev-code company-yasnippet company-files))))

  (add-hook 'emacs-lisp-mode-hook 'my-elisp-mode-hook)
  (add-hook 'emacs-lisp-mode-hook 'company-mode))

Follow the same recipe for other major modes.

As for creating a grouped backend for company to use for all modes, add something like this to your .emacs :-

  (add-to-list 'company-backends 'company-dabbrev-code)
  (add-to-list 'company-backends 'company-yasnippet)
  (add-to-list 'company-backends 'company-files)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.