更改代码块的 emacs 组织模式键绑定

Change emacs org-mode key binding for code block

在 org-mode 中,我使用 <s 然后 TAB 来插入代码块。此操作将插入一个代码块,如

 #+BEGIN_SRC
    .
    .
    .
 #+END_SRC

但我想修改此操作以插入类似

的内容
#+BEGIN_SRC python -n :results output pp replace :exports both
    .
    .
    .
#+END_SRC

我知道可以在 emacs init 文件中更改 :result:exports 的默认行为,但我更喜欢更改此快捷方式行为,因为它使我能够更改行中的选项。

the Easy Templates section of the org-mode manual中所述,您可以通过自定义变量org-structure-template-alist来修改这些模板。 (使用 M-x customize-option。)

对于<s,默认展开为"#+BEGIN_SRC ?\n\n#+END_SRC"。您可以编辑它以在 BEGIN_SRC 之后包含您想要的选项。或者,您可以添加一个新模板,例如<p,扩展到您想要的文本。

根据legoscia's 回答我自己的问题。

Easy Templates section of the org-mode manual中所述,您可以通过自定义变量org-structure-template-alist来修改这些模板。使用 M-x customize-option 并应用更改会将所有简单模板添加到您的 init 文件中,如果您不喜欢它,您可以只在 init 文件中添加一行以更改模板或添加一。

在我的例子中,我将这一行添加到我的 emacs init 文件中以添加 <p 然后 TAB :

(add-to-list 'org-structure-template-alist '("p" "#+BEGIN_SRC python -n :results output pp replace :exports both\n?\n#+END_SRC"))

所有学分都转到 legoscia

从 Org 9.2 开始,@shae128 使用的方法不再有效。相反,您需要使用 tempo-define-template,如下所示:

(tempo-define-template "python-block"
           '("#+begin_src python :results raw output"
             n n p n n
             "#+end_src")
           "<p"
           "Insert an outputting Python block"
           'org-tempo-tags)

n 代表换行符,p 代表留下标记的位置,<p 代表点击 tab 时展开的命令。

感谢Omar's answer here