作者coldnew (夜影)
看板Editor
标题[emac] 建立新档案时自动插入样板 (使用yasnippet)
时间Sun Feb 13 20:40:55 2011
前言:
emacs 有很多自动插入template的方法,比如 file-template.el
或是header2.el
这篇文章是叙述如何使用yasnippet在建立新档案时自动插入样板
Step1:
首先必须装好yasnippet
http://code.google.com/p/yasnippet/
Step2:
在自己的emacs config 添加以下的code
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets")
注意:上面的load-directory依个人的snippets资料夹位置修改
Step3:
在自己的 emacs config 添加以下code
;; Auto add HEADER in new file
(add-hook 'find-file-hook
'(lambda ()
(when (and (buffer-file-name)
(not (file-exists-p (buffer-file-name)))
(= (point-max) 1))
(let ((header-snippet "HEADER")
(yas/fallback-behavior 'return-nil))
(insert header-snippet)
;; if can't expand snippet, delete insert string
(if (not (yas/expand))
(delete-region (point-min) (point-max)))))))
这段code会在使用者建立新的档案时,尝试插入拥有 HEADER 这个缩写的 snippet
如果找不到,则删除插入的字串
Step4:
建立属於自己的 HEADER 样板
写样板的教学请见:
http://yasnippet.googlecode.com/svn/trunk/doc/snippet-development.html
依照yasnippet的教学,建立名为HEADER.yas的档案,内容为
# -*- mode: snippet -*-
# name : Header file
# key: HEADER
# --
这边放上想插入的样板
这样就可以了
完整范例请见我的 emacs config
https://github.com/coldnew/coldnew-emacs/
我的snippet 位於 etc/ 资料夹内
yasnippet 设定在 rc.d/025-yasnippet.el
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 120.126.41.45
1F:推 ethan5422:emacs一定要推一下的! 02/14 10:14
2F:推 nonnn:I am searching for this, push!!! 02/18 01:26
3F:推 mgdesigner:喔这个真有用,推一个 06/21 11:58
※ 编辑: coldnew 来自: 120.126.41.45 (04/18 18:48)
※ 编辑: coldnew 来自: 120.126.41.45 (04/25 17:58)