9

I am just starting to experiment with Yasnippet, and it worked very well until I tried to combine it with auto-complete-mode. The problem is that Auto-Complete is taking over completely. For example, in c-mode I can type pr and then press TAB to expand a printf snippet, but after activating auto-complete-mode this does not work, but auto-complete-mode itself works just fine. Instead, I would like to have Yasnippet to have priority over Auto-Complete when I press the TAB key.

I am using Emacs 24.4 on Ubuntu 14.04, Yasnippet version: 0.8.1, and Auto-Complete version 1.5.0.

Here is minimal example

$ emacs -Q -l init.el t.c

where init.el is:

(require 'package)
(package-initialize)
(setq package-enable-at-startup nil)
(yas-global-mode 1)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(add-to-list 'ac-sources 'ac-source-yasnippet)

If I now type pr and then press TAB, I get the Auto-Complete popup menu, (whereas I would like to expand the printf snippet instead). If I type M-x auto-complete-mode to turn off auto-complete-mode and then type pr and TAB, the printf snippet expands as expected.

Other information:

  • C-h v RET ac-source-yasnippet gives:
((depends yasnippet)
 (candidates . ac-yasnippet-candidates)
 (action . yas/expand)
 (candidate-face . ac-yasnippet-candidate-face)
 (selection-face . ac-yasnippet-selection-face)
 (symbol . "a"))
  • C-h v RET ac-sources gives:
(ac-source-yasnippet ac-source-gtags ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers)
  • Clearing ac-source-yasnippet variable ((setq ac-source-yasnippet nil)) does not help.

References:

Håkon Hægland
  • 3,608
  • 1
  • 20
  • 51

1 Answers1

12

As a workaround, you can redefine the Yasnippet expansion key instead, as explained in the FAQ:

(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "<C-tab>") 'yas-expand)

In this way, you use <C-tab> to expand Yasnippets and the TAB key for auto-complete-mode.


Note:

I also discovered that using <C-tab> to expand Yasnippets, instead of <tab>, had the advantage to make snippet expansion not interfer with indent-according-to-mode. For example: If I have a Lisp line: (ac-config-default) but it is not correctly indented, I usually press TAB to indent it correctly (using indent-according-to-mode). But if the cursor were between the a and c in (ac-config-default) it would instead have expanded the Lisp and-snippet (which is not desired here), to get the line ((and )c-config-default))..

Håkon Hægland
  • 3,608
  • 1
  • 20
  • 51