unsealed counsel
20 Jun 2015

Make company-yasnippet play nice

Company mode is an excellent completion mechanism for emacs, much better than its documentation would lead one to believe. The best keybinding for company mode completion to common prefix is TAB, which, however, is also the best keybinding for yasnippet. The simplest solution would seem to be to use company mode to drive yasnippet. Such a back end for company mode already exists: company-yasnippet bundled with company mode. By default, company-yasnippet is not enabled since it is painful to use with other back ends. My solution has been to bind shift+TAB (called <backtab> in emacs keymap lingo) to company-yasnippet and not enable it by default, but the problem is that the default company mode completion is activated after typing in a snippet prefix, and sets up a transient keymap that simply ignores shift+TAB. We can work around the problem with a little bit of elisp:

(defun ravi/company-to-yasnippet ()
  (interactive)
  (company-abort)
  (call-interactively 'company-yasnippet))
(bind-key "<backtab>" 'ravi/company-to-yasnippet company-active-map)
(bind-key "<backtab>" 'company-yasnippet)

The trick is that we need to call company-abort before we call company-yasnippet, but only perform this extra step when company mode completion is active.

Tags: emacs company-mode yasnippet
Creative Commons License
runes.lexarcana.com by Ravi R Kiran is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License .