Snippet

Snipppet 是非常棒的代码编辑工具,能极大地提高代码开发效率。Snippet 其实就是一段源代码或文字,由编辑器以模块的形式存放、以索引方式调用和管理它,达到实际开发中的复用。开发者在编程或写作时,只需要输入 Snippet 名(索引名),即可提供补全(以索引名调用此 Snippet)选项。在 Emacs 中,yassnippet 是我用过的最棒的,有了它再也不怕繁琐的重复性工作了。

1 yassnippet

yassnippet 已经是 spacemacs 中 auto-complete layer 自带的 snippet 工具了。因为它是真的很棒呀!

1.1 config

安装完 yassnippet,就可以从其下 snippets/ 中看到自带的大量 snippets,当然也建议自行修改、创建适合自己的 Snippet,存放在自己的目录下。

(setq yas-snippet-dirs
'("~/.emacs.d/private/snippets"
"~/.emacs.d/elpa/yasnippet*/snippets"
))

如果是 spacemacs 用户,这些全都是默认配置了

The following directories are added by default:
- =~/.emacs.d/elpa/yasnippet-xxxxx/snippets=
- =~/.emacs.d/layers/auto-completion/snippets=
- =~/.emacs.d/private/snippets= (conditional to the value of =auto-completion-private-snippets-directory=)
- =~/.spacemacs.d/snippets= (conditional to the existence of =~/.spacemacs.d= directory)

我的.spacemacs 中 auto-complete 设置如下:

(auto-completion :variables
auto-completion-return-key-behavior 'complete
auto-completion-tab-key-behavior 'cycle
auto-completion-complete-with-key-sequence nil
auto-completion-complete-with-key-sequence-delay 0.1
auto-completion-enable-snippets-in-popup t
auto-completion-enable-help-tooltip t
auto-completion-enable-sort-by-usage t
)

1.2 snippets for org-mode

head

# -*- mode: snippet -*-
# name: head
# key: head_
# --
#+OPTIONS: ^:{} toc:2 num:t <:nil c:nil \n:nil email:nil f:t todo:t date:nil timestamp:nil creator:nil author:nil
#+BIND: org-html-validation-link nil
#+STARTUP: indent align content latexpreview
#+EXPORT_EXCLUDE_TAGS: noexport
#+AUTHOR: zuoshi\thanks{This document is generated by Emacs org-mode.}
#+TITLE:$1
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [12pt, a4paper]
#+LATEX_HEADER: \usepackage[total={6in, 8in}]{geometry}
#+LATEX_HEADER: \usepackage{xeCJK}
#+LATEX_HEADER_EXTRA: \setCJKmainfont{Source Han Sans UI SC}
#+LATEX_HEADER_EXTRA: \hypersetup{CJKbookmarks=true, colorlinks=true, linkcolor=magenta, urlcolor=blue, filecolor=green, citecolor=red, menucolor=cyan}
# #+STARTUP: beamer
# #+LATEX_HEADER: \usetheme{Warsaw}
# #+LATEX_HEADER: \usecolortheme{whale}
$0

tab

# -*- mode: snippet -*-
# name: tab
# key: tab_
# --
#+CAPTION: $1
#+NAME: tab:$2
#+TBLFM:$3

wtab

# -*- mode: snippet -*-
# name: wtab
# key: wtab_
# --
|-|-|-|
|function |shortcuts | note|
|-|-|-|
|-|-|-|

img

# -*- mode: snippet -*-
# name: img
# key: img_
# --
#+CAPTION: $1
#+NAME: img:$2