Emacs 使用YASnippet

1 安装YASnippent

$ cd ~/.emacs.d/plugins
$ git clone https://github.com/capitaomorte/yasnippet

Add the following in your .emacs file:

(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)

2 安装 org-mode字典

# git clone git://github.com/rvf0068/yasnippets.git
## 复制yasnippets/org-mode目录到yasnippet/snippets/目录下
# cp -r yasnippets/org-mode /root/.emacs.d/site-lisp/yasnippet/snippets/

3 org-mode中使用教程

Snippets for org-mode. See Yasnippet in Github.

They have bindings C-c y plus an extra letter.

I have added a condition statement so that the snippets can be expanded only at the beginning of the line.

name key letter explanation
#+begin_ …#+end_ block b
#+srcname:..#+begin_src…#+end_src sb s source block with name
attr_html ath alt="$1" img class="aligncenter"
attr_latex atl ${1:width=$2\textwidth}
author aut a inserts #+author:
corollary cor c (uses reftex)
definition def d (uses reftex)
ditaa dit #+begin_ditaa,+end_ditaa
el el #+begin_src emacs-lisp,+end_src
email email inserts #+email:
equation eqn e (uses reftex)
figure fig f asks for image file
language lan inserts #+language: (with choice)
LaTeX class lcl inserts #+latex_class: (with choice)
LaTeX header lhe inserts #+latex_header:
lemma lem l (uses reftex)
options opt o inserts #+options:
proof prf
properties folded properties inserts visibility folded property
proposition pro p (uses reftex)
startup sta inserts #+startup:
text text inserts #+text:
theorem thm t (uses reftex)
tikz-picture tkz z (uses reftex)
title title inserts #+title:

4 YASnippet增加模板

这么强大的工具要是没有定制功能简直太可惜了.不过我找了很久也没有找到.以为直接编辑文件就可以的,但是结果还是不成功.

还得一步一个脚印的来.幸好有元旦的5天假期,否则不知道我会不会因为没有耐心而放弃研究它了呢.

下面举个例子增加 cnblogs的bash着色模块.

  1. ESC x yas/new-snippet 来进入新建snippet的buffer,可以看到以下的内容:
      # -*- mode: snippet -*-
      # name: 
      # key: 
      # binding: direct-keybinding
      # expand-env: ((some-var some-value))
      # type: command
      # --
    
    

    其中:

    • name: 是在YASnippet中显示的名字.
    • key: 是触发这个snippet所用的关键字.
    • binding 和 expand-env, type 是一些高级特征,这里不做研究.
  2. 将上面的内容改为(不需要的行删掉):
       # -*- mode: snippet -*-
       # name: src_bash
       # key: bash
       # --
       #+BEGIN_HTML
      <div class="likecs_Highlighter">
      <pre class="brush:bash">
    
      ${1:#!/bin/bash}
      $0
    
      </pre>
      </div>
       #+END_HTML
    
    

    其中: ${N: Some Text} 的内容被称为字段, N 是tab stop序号(顺序是从$1-$N的),冒号后面的文本则是默认值.最近 $0 被称为YASnippet的退出点,即一个key被展开为snippet,并按顺序走完所有的tab stop之后光标停留的点.

  3. 保存: C-x C-s 保存在 org-mode/bash文件里面即可.
  4. 重启之后,可以使用.

Date: 2012-12-30 Sun

Author: liweilijie

Org version 7.9.2 with Emacs version 23

Validate XHTML 1.0

相关文章:

  • 2021-09-23
  • 2021-10-14
  • 2021-09-27
  • 2021-10-30
  • 2021-12-22
  • 2021-10-01
  • 2021-08-25
  • 2021-07-02
猜你喜欢
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-11-22
  • 2022-12-23
相关资源
相似解决方案