I want Emacs to work like this:
Let
auto-complete
auto-popup menu:(setq ac-auto-show-menu 0.8) (setq ac-delay 0.1)
Use C-n/p / M-n/p to select
auto-complete
popup menu candidates:(define-key ac-menu-map (kbd "M-n") 'ac-next) (define-key ac-menu-map (kbd "M-p") 'ac-previous)
When selecting a candiate
disable TAB / S-TAB in popup menu selection:
(define-key ac-menu-map (kbd "<tab>") nil) (define-key ac-menu-map (kbd "<S-tab>") nil)
press Enter to select the candiate, without inserting newline:
;; ???
if the candidate is an abbrev, Enter should only select the candiate:
;; ???
... and pressing Space should cause Emacs to auto-expand the abbrev.
if the candidate is a dabbrev, pressing M-\ on candidate should trigger
dabbrev-expand
.pressing TAB / C-i to expand the candidate for
yasnippet
:(setq yas-trigger-key "TAB")
I set this, but the trigger does not expand when I press TAB.
pressing TAB to expand a snippet trigger while in a field:
(setq yas-triggers-in-field t)
pressing C-j to jump to next field:
(setq yas-next-field-key '("<tab>")) ;; or "C-j"
How can I expand a snippet within a snippet using
yasnippet
?
Some explanations
There are two TABs in Emacs:
(kbd "TAB")
/(\t, [9])
(kbd "<tab>")
/([tab])
If modes like yasnippet
and auto-complete
want to bind to TAB, their trigger key must be the same as the original tab command. Since Emacs binds indent-for-tab-command
to (kbd "TAB")
, it's better to use that as the trigger key. yasnippet
binds to it by default, and it is easy to set up auto-complete
to trigger using TAB as well:
;; trigger using TAB and disable auto-start
(custom-set-variables
'(ac-trigger-key "TAB")
'(ac-auto-start nil)
'(ac-use-menu-map t))
But in some modes (ruby-mode
, markdown-mode
, org-mode
, etc.), the command is bound to
(kbd "<tab>")
. When the real tab key is typed, the function bound to (kbd "<tab>)
has higher priority, so yasnippet
and auto-complete
are not invoked. This is easy to fix by moving the key binding:
(defun iy-tab-noconflict ()
(let ((command (key-binding [tab]))) ; remember command
(local-unset-key [tab]) ; unset from (kbd "<tab>")
(local-set-key (kbd "TAB") command))) ; re-bind to (kbd "TAB")
(add-hook 'ruby-mode-hook 'iy-ac-tab-noconflict)
(add-hook 'markdown-mode-hook 'iy-ac-tab-noconflict)
(add-hook 'org-mode-hook 'iy-ac-tab-noconflict)
My setup
I downloaded yasnippet
, auto-complete
via the el-get
packager manager. I'm using Ubuntu 12.04 and Emacs 24.3.50.1.
Wrapping up
I know this problem is a little long, but it really makes it difficult for me to use auto-complete
and yasnippet
. If the basic key binding doesn't work smoothly, this slows down my workflow quite a bit. I think many people have similar problems because I found some similar questions on the internet (though none of them are exactly like mine).
As you can see above, some of the relevant settings I already know. (But if you think I made a mistake somewhere, please tell me.) There are also some things I still don't know how to set up (???
). Maybe there isn't a way to make all of these settings work together? Let me know if that is the case, and otherwise please make sure none of these setting interfere with each other.
After I get the answer to this question, I hope to write an Emacs extension to initialize all of these settings automatically.
Thanks for your help!