11

I know this isn't the best type of question for stack... but I can't think of another place where it might get enough exposure.

So I really like company-mode, but sometimes it's annoying to have to manually switch to the next backend (company-try-hard is handy here). I've started playing around with grouped backends, and I can see some awesome potential. Unfortunately, I'm not happy with anything I've tried so far. I think this might be better set with hooks for different modes. Does anyone have some interesting company-backends lists that I could get some inspiration from?

I mainly use python (elpy), javascript (js3-mode), web (web-mode) and shell scripts if that helps.

1 Answer 1

15

You can set a default company-backends, then use hook to append different backends to different mode. Here is a way I config in my emacs.

;; set default `company-backends'
(setq company-backends
      '((company-files          ; files & directory
         company-keywords       ; keywords
         company-capf
         company-yasnippet
         )
        (company-abbrev company-dabbrev)
        ))

(add-hook 'python-mode-hook
          (lambda ()
            (add-to-list (make-local-variable 'company-backends)
                         'company-anaconda)))
(dolist (hook '(js-mode-hook
                js2-mode-hook
                js3-mode-hook
                inferior-js-mode-hook
                ))
  (add-hook hook
            (lambda ()
              (tern-mode t)

              (add-to-list (make-local-variable 'company-backends)
                           'company-tern)
              )))

;;;_. company-mode support like auto-complete in web-mode

;; Enable CSS completion between <style>...</style>
(defadvice company-css (before web-mode-set-up-ac-sources activate)
  "Set CSS completion based on current language before running `company-css'."
  (if (equal major-mode 'web-mode)
      (let ((web-mode-cur-language (web-mode-language-at-pos)))
        (if (string= web-mode-cur-language "css")
            (unless css-mode (css-mode))))))

;; Enable JavaScript completion between <script>...</script> etc.
(defadvice company-tern (before web-mode-set-up-ac-sources activate)
  "Set `tern-mode' based on current language before running `company-tern'."
  (if (equal major-mode 'web-mode)
      (let ((web-mode-cur-language (web-mode-language-at-pos)))
        (if (or (string= web-mode-cur-language "javascript")
               (string= web-mode-cur-language "jsx"))
            (unless tern-mode (tern-mode))
          ;; (if tern-mode (tern-mode))
          ))))
  • for web-mode, you also can use package `company-web'.
  • or shell script, there are other packages support completion, you can search for it.
3
  • Awesome thanks! this helped out a lot. I tweaked it a bit since I like using elpy-company-backend and I like company-yasnippets merged with other code completion backends. The one thing I can't figure out is how to get css-mode and tern-mode to turn off when not in their respective tags.
    – aiguofer
    Commented Oct 22, 2015 at 14:01
  • 1
    Also, the question you filed was also quite helpful.
    – aiguofer
    Commented Oct 22, 2015 at 14:03
  • Good you can find that. grad it can help you. Commented Oct 23, 2015 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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