Skip to content

Switching from AC

Hans Chen edited this page Feb 13, 2017 · 13 revisions

company-mode and auto-complete-mode behave differently in terms of candidate selection and completion. There's a few tweaks you can do to make the switch less jarring. First of all, to make TAB complete first, then cycle, rebind it to company-complete-common-or-cycle:

(eval-after-load 'company
  '(progn
     (define-key company-active-map (kbd "TAB") 'company-complete-common-or-cycle)
     (define-key company-active-map (kbd "<tab>") 'company-complete-common-or-cycle)))

If you wish to have S-TAB as well to cycle the other way around:

(eval-after-load 'company
  '(progn
     (define-key company-active-map (kbd "S-TAB") 'company-select-previous)
     (define-key company-active-map (kbd "<backtab>") 'company-select-previous)))

The current candidate isn't displayed inline except when there's only one left. You can rectify this by switching out company-preview-if-just-one-frontend for company-preview-frontend:

(setq company-frontends
      '(company-pseudo-tooltip-unless-just-one-frontend
        company-preview-frontend
        company-echo-metadata-frontend))

Finally, to cancel selections by typing non-matching characters, customize company-require-match:

(setq company-require-match 'never)

If you prefer it the other way around (keys like SPC finishing completion), customize company-auto-complete instead and for more control, company-auto-complete-chars as well:

(setq company-auto-complete t)

Setting up similar popup behavior

See https://github.com/company-mode/company-mode/pull/524 for more info.

  (defun my-company-visible-and-explicit-action-p ()
    (and (company-tooltip-visible-p)
         (company-explicit-action-p)))

  (defun company-ac-setup ()
    "Sets up `company-mode' to behave similarly to `auto-complete-mode'."
    (setq company-require-match nil)
    (setq company-auto-complete #'my-company-visible-and-explicit-action-p)
    (setq company-frontends '(company-echo-metadata-frontend
                              company-pseudo-tooltip-unless-just-one-frontend-with-delay
                              company-preview-frontend))
    (define-key company-active-map [tab]
      'company-select-next-if-tooltip-visible-or-complete-selection)
    (define-key company-active-map (kbd "TAB")
      'company-select-next-if-tooltip-visible-or-complete-selection))

   (company-ac-setup)

When using (company-ac-setup), the color of faces of the completions (company-preview and company-preview-common) might be different. If this is not preferred, to get matching colors, this is an option:

    (eval-after-load 'company
      (lambda ()
        (set-face-attribute
         'company-preview
         nil
         :background (face-attribute 'company-preview-common :background))))

Similar popup colors

(From the company-go README)

    (custom-set-faces
     '(company-preview
       ((t (:foreground "darkgray" :underline t))))
     '(company-preview-common
       ((t (:inherit company-preview))))
     '(company-tooltip
       ((t (:background "lightgray" :foreground "black"))))
     '(company-tooltip-selection
       ((t (:background "steelblue" :foreground "white"))))
     '(company-tooltip-common
       ((((type x)) (:inherit company-tooltip :weight bold))
        (t (:inherit company-tooltip))))
     '(company-tooltip-common-selection
       ((((type x)) (:inherit company-tooltip-selection :weight bold))
        (t (:inherit company-tooltip-selection)))))
You can’t perform that action at this time.