Emacs 使用YASnippet

Emacs 使用YASnippet

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.

namekeyletterexplanation
#+begin_ …#+end_blockb
#+srcname:..#+begin_src…#+end_srcsbssource block with name
attr_htmlathalt="$1" img class="aligncenter"
attr_latexatl${1:width=$2\textwidth}
authorautainserts #+author:
corollarycorc(uses reftex)
definitiondefd(uses reftex)
ditaadit#+begin_ditaa,+end_ditaa
elel#+begin_src emacs-lisp,+end_src
emailemailinserts #+email:
equationeqne(uses reftex)
figurefigfasks for image file
languagelaninserts #+language: (with choice)
LaTeX classlclinserts #+latex_class: (with choice)
LaTeX headerlheinserts #+latex_header:
lemmaleml(uses reftex)
optionsoptoinserts #+options:
proofprf
properties foldedpropertiesinserts visibility folded property
propositionprop(uses reftex)
startupstainserts #+startup:
texttextinserts #+text:
theoremthmt(uses reftex)
tikz-picturetkzz(uses reftex)
titletitleinserts #+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="cnblogs_Highlighter">
      <pre class="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
原文地址:https://www.cnblogs.com/liweilijie/p/2840081.html