Add structure template in Org-mode

The org-mode code blocks can be used for literate programming and creating executable snippets. There is also a quick way to insert some common code blocks, for example, <s TAB would expand as

#+BEGIN_SRC

#+END_SRC

Thus, it is pretty convenient to insert code blocks or examples.

org-structure-template-alist 这个列表中预置了许多常用结构,如官方文档所示:

Key Structure Template
a ‘#+BEGIN_EXPORT ascii’ … ‘#+END_EXPORT’
c ‘#+BEGIN_CENTER’ … ‘#+END_CENTER’
C ‘#+BEGIN_COMMENT’ … ‘#+END_COMMENT’
e ‘#+BEGIN_EXAMPLE’ … ‘#+END_EXAMPLE’
E ‘#+BEGIN_EXPORT’ … ‘#+END_EXPORT’
h ‘#+BEGIN_EXPORT html’ … ‘#+END_EXPORT’
l ‘#+BEGIN_EXPORT latex’ … ‘#+END_EXPORT’
q ‘#+BEGIN_QUOTE’ … ‘#+END_QUOTE’
s ‘#+BEGIN_SRC’ … ‘#+END_SRC’
v ‘#+BEGIN_VERSE’ … ‘#+END_VERSE’

不过有时也会想要自己造个轮子,比如我平时写博客,有时需要插入按键标签 <kbd> 。此时可用 lisp 的 add-to-list 语法来添加新的结构模板:

add-to-list symbol element &optional append compare-fn

(add-to-list 'org-structure-template-alist
             '("k" "@@html:<kbd>@@?@@html:</kbd>@@"))

这个语法的优点在于它会对比原列表内的元素以避免添加重复元素。

这个 add-to-list 语法每次只能添加一个元素进入列表,并不能添加多个元素。而简单执行 append ,则无法避免重复元素的添加。所以我采用 dolist 指令来添加多个元素:

(dolist
    (my-org-structure-template
     '(("k" "@@html:<kbd>@@?@@html:</kbd>@@")
       ("spy" "#+BEGIN_SRC python\n?\n#+END_SRC")
       ("son" "#+BEGIN_SRC org :eval never-export\n,?\n#+END_SRC")))
  (add-to-list 'org-structure-template-alist my-org-structure-template))

效果拔群。

Avatar
Runhan Yu
Data Engineer

My interests include big data, cloud computing, software development and biochemistry.

Related