---
title: "emacs 使用 template"
author: "forever8tf"
date: "2012-10-21T19:05:50Z"
tags: []
categories: ["编程语言"]
slug: "emacs-template"
draft: false
lastmod: "2012-10-21T19:05:50Z"
id: 1702602
url: "https://forever8tf.iteye.com/blog/1702602"
---

折腾了一下午，总算搞好了。

首先是折腾 [yasnippet](https://github.com/capitaomorte/yasnippet "yasnippet")，可是 expand 看不懂，官方大家都说简单，可见我水平一般到什么程度。

再次折腾 [template-simple](http://www.emacswiki.org/emacs/template-simple.el "template-simple.el")，根本一点也不 simple 好么，但抱着支持国人的决心，挣扎纠结还是放弃了。

最后折腾 [template.el](http://emacs-template.sourceforge.net/ "template.el")，被 emacs 打败了。全是尼玛的傲骄萌物啊。

 

根据 template 中的 INSTALL 安装好即可使用（前提是你将东西都放到了制定的位置，比如 Templates 要放在 ~/.templates 下）。成功后我想简单的定制一下，将 Templates 放在 ~/.emacs.d/template/templates 下，就这简单的一步浪费了一下午。

 

查看 template.el 的源码，如下：

```
(defcustom template-default-directories
  (cons (if (and (not (file-directory-p "~/.templates/"))
		 (file-directory-p "~/lib/templates"))
	    (expand-file-name "~/lib/templates/")
	  (expand-file-name "~/.templates/"))
	(and (fboundp 'locate-data-directory)
	     (let ((dir (locate-data-directory "template")))
	       (and dir (list dir)))))
  "*List of default directories for template files.
See `template-derivation-alist' for details."
  :group 'template-derivation
  :type '(repeat directory))
```

 我真的天真的以为这是 template.el 设置查找 Templates 的目录，所以我这样：

```
(setq template-default-directories "~/.emacs.d/template/templates/“）
```

 结果尼玛无用啊，还提示错误。

后来在[这篇](http://emacser.com/emacs-conf-system.htm "Emacs配置系统")，还有[这篇](http://lifegoo.pluskid.org/wiki/EmacsTemplate.html "使用 template.el 为文件创建默认模板")中看出了端弥，需要这样配置：

```
(setq template-subdirectories '("~/.emacs.d/template/templates"))
```

完整配置如下：

```
;; 定义模版
(add-to-list 'load-path "~/.emacs.d/template/")
(require 'template)
;; 必须这么指定 搜索目录，蛋疼
(setq template-subdirectories '("~/.emacs.d/template/templates"))
(setq template-auto-insert t)
(template-initialize)
(add-to-list 'template-find-file-commands
             'ido-exit-minibuffer)
;; 结束
```

  这样就搞定了，具体原因需要等我过了6级以后在告诉大家.....

上图一张

![](http://dl.iteye.com/upload/attachment/0075/4462/5406d7f1-b47e-30d8-84a6-d726dfa97606.png)