November 30, 2012

Emacs配置yasnippet

适用版本: Emacs 24+

  1. 添加package源,将如下代码加到.emacs 或者 .emacs.d/init.el
1 ;; set packages
2 (require 'package)
3 (setq package-archives
4       '(("original"    . "http://tromey.com/elpa/")
5         ("gnu"         . "http://elpa.gnu.org/packages/")
6         ("marmalade"   . "http://marmalade-repo.org/packages/")
7         ("melpa"       . "http://melpa.milkbox.net/packages/")))
8 (package-initialize)
  1. M-x eval-buffer
  2. M-x package-list-packages 按照你的网速快慢,可能需要半分钟左右.
  3. 在打开的Packages Buffer中,找到yasnippet yasnippet-bundle yas-jit, 并按i(install) 标记安装.最后按x安装。 PS:
    • yasnippet-bundle(可选) – 自动编译snippets
    • yas-jit(可选) – 按需加载snippets
  4. 配置yasnippet,这里分两种情况: 安装yas-jit和没有安装yas-jit:

    • 如果安装了yas-int
 1 (require 'yas-jit)
 2 (require 'dropdown-list)
 3 (setq yas/prompt-functions '(
 4                              yas/ido-prompt
 5                              yas/dropdown-prompt
 6                              yas/completing-prompt))
 7 (setq yas/root-directory '(
 8                            "~/.emacs.d/snippets" ;; 自己的snippets
 9                            "~/.emacs.d/elpa/yasnippet-20121127.25/snippets" ;; yasnippet提供的
10                            "~/.emacs.d/vendor/yasnippets-rails/rails-snippets" ;; 其他
11                            "~/.emacs.d/vendor/yasnippets-shoulda"))
12 (yas/jit-load)
  • 如果没有安装yas-jit
1 (require 'yasnippet)
2 (yas-global-mode 1)
3 (require 'dropdown-list)
4 (setq yas/prompt-functions '( yas/ido-prompt
5                               yas/dropdown-prompt
6                               yas/completing-prompt))
7 (yas/load-directory "~/.emacs.d/vendor/yasnippets-rails/rails-snippets")
8 (yas/load-directory "~/.emacs.d/vendor/yasnippets-shoulda")
  1. 完了.

欢迎参考我的emacs-config

Known issue

  1. markdown-mode 下由于 TAB 被绑定到了 markdown-cycle上,所以无法展开snippets。 查看键绑定C-h k RET TAB

TAB runs the command markdown-cycle, which is an interactive compiled Lisp function in ‘markdown-mode.el’.