doom emacs file template

触发方式

输入 __ + TAB

开源证书 template

M-x +fle-templates/insert-license

template 后缀

  • -bp : boilerplate 样版文件

注册新的 file template

set-file-template!set-file-templates! 实现

调试

+file-templates/debug

如何确定使用哪个 file template 文件

决定因素:

  1. 变量 +file-templates-alist

    • 各个 major mode, buffer-file-name 关联的 file template
    • 触发条件列表
  2. 函数 set-file-template!set-file-templates!

    • 添加触发条件到变量 +file-templates-alist

file template 文件存放位置

  • 直接位置

    • 变量 +file-templates-dir
  • 间接位置

    • 列表变量 yas-snippet-dirs

插入数据函数

1
2
(let* (fmt (concat "<")))
(insert (format-time-string (cdr org-time-stamp-formats) (current-time)))

auto-insert-mode

参考:

基本配置

1
2
3
4
5
(auto-insert-mode)  ;;; Adds hook to find-files-hook
(setq auto-insert-directory "~/.mytemplates/") ;;; Or use custom, *NOTE* Trailing slash important
(setq auto-insert-query nil) ;;; If you don't want to be prompted before insertion
(define-auto-insert "\.py" "my-python-template.py")
(define-auto-insert "\.php" "my-php-template.php")

通过 yasnippet 配置模板代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
(defun my/autoinsert-yas-expand()
  "Replace text in yasnippet template."
  (yas/expand-snippet (buffer-string) (point-min) (point-max)))

(custom-set-variables
 '(auto-insert 'other)
 '(auto-insert-directory "~/autoinsert/")
 '(auto-insert-alist '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header") . ["template.h" c++-mode my/autoinsert-yas-expand])
                       (("\\.\\([C]\\|cc\\|cpp\\)\\'" . "C++ source") . ["template.cc" my/autoinsert-yas-expand])
                       (("\\.sh\\'" . "Shell script") . ["template.sh" my/autoinsert-yas-expand])
                       (("\\.el\\'" . "Emacs Lisp") . ["template.el" my/autoinsert-yas-expand])
                       (("\\.pl\\'" . "Perl script") . ["template.pl" my/autoinsert-yas-expand])
                       (("\\.pm\\'" . "Perl module") . ["template.pm" my/autoinsert-yas-expand])
                       (("\\.py\\'" . "Python script") . ["template.py" my/autoinsert-yas-expand])
                       (("[mM]akefile\\'" . "Makefile") . ["Makefile" my/autoinsert-yas-expand])
                       (("\\.tex\\'" . "TeX/LaTeX") . ["template.tex" my/autoinsert-yas-expand]))))