Answers:
组织模式具有用于插入源代码块的内置机制。请参见组织手册中的简易模板。默认配置为## BLOCKS提供了模板,您可以通过定制添加自己的模板‘org-structure-template-alist’。
例如,打开组织缓冲区并键入<sTAB以插入BEGIN / END_SRC块。
, i b在spacemacs?
                    [这是@glucas和@manandearth的答案的补充/说明-它不是独立存在的。]
请注意,在组织模式的最新开发版本(> = 9.2)中(因此,除非将来发生变化,否则在将来的稳定版本中),org-structure-template-alist其类型已更改。内置的easy-templates机制已被放弃,取而代之的是基于该tempo软件包的更通用的机制。不幸的是,新机制只允许后,单字符缩写<,因此<s将扩大到#+BEGIN_SRC...#+END_SRC了OP是要求字符串,但事情好像<el会@ manandearth的答案不工作。这项工作仍在进行中,因此情况可能会有所变化,但是如果您使用多字母<XXX缩写,那么在将org-mode升级到这样的版本时,请准备好使它们打破。
还有另一种机制:C-c C-,绑定到org-insert-structure-template该机制,提示您输入要插入的块类型(带有列出所有可用块的菜单,并允许您通过一次按键选择)。它的优点是它将begin/end环绕一个区域,因此,如果您已经键入了一堆东西,并且想要将其包装在一个块中,那么您要做的就是将其选择为一个区域,然后键入C-c C-.一个字符选择块的类型。
请参阅此说明 [fn:1],(新)函数的doc字符串org-insert-structure-template和file org-tempo.el。
[fn:1]链接可能不正确,因为它指向文件中固定的行号,该行号可能会(不,会!)更改。如果该链接未将您带到正确的位置,请搜索字符串“ structure template扩展”-随时编辑此答案并确定行号-谢谢!
该源块行为组织9.2改变。<s默认情况下,缩写不再起作用。而是使用C-c C-,调用org-insert-structure-template。
按下C-c C-,会弹出一个对话框。按TAB,然后输入src R。这将插入一个源代码块,并将光标置于第二行的开头(请|设为point):
#+begin_src R
|#+end_src
然后在块内进行编辑,按C-o打开新行。
您可以通过将条目添加到来保存模板org-structure-template-alist。将以下内容放在您的某个位置init.el或使用以下命令运行它C-x C-e:
(add-to-list 'org-structure-template-alist '("r" . "src R"))
现在,您C-c C-,将r在列表中看到的条目。您可以对其他语言(例如Python,Ruby等)采用相同的方法。只需在上面的虚线对(“ src R”部分)中替换cdr。
此外,要在源块定界符之间插入一行,可以使用插入换行符C-q C-j。即,src R C-q C-j在上面的虚线对中输入cdr。结果将如下所示:
(add-to-list 'org-structure-template-alist '("R" . "src R
"))
现在,当您按下时R,Emacs将插入源代码块并将光标置于它们之间的新行上(让它|成为point):
#+begin_src R
|
#+end_src
<s结合仍然存在,你只需要包含(require org-temp)在你的init来启用它。
                    (require 'org-tempo)
                    对于代码块的键绑定,请babel-org尝试以下绑定,<r然后再绑定<tab>:
;; add <r for R expansion
(add-to-list 'org-structure-template-alist
         '("p" "#+BEGIN_SRC r :results output org drawer\n?\n#+END_SRC"
           "<src lang=\"r\">\n?\n</src>"))
您也可以将源修改为ESS。
要将<p其后绑定<tab>到python块代码,例如:
;; add <p for python expansion
(add-to-list 'org-structure-template-alist
         '("p" "#+BEGIN_SRC python :results output org drawer\n?\n#+END_SRC"
           "<src lang=\"python\">\n?\n</src>"))
设置为emacs-lisp并<el后跟的<tab>将是:
;; add <el for emacs-lisp expansion
(add-to-list 'org-structure-template-alist
         '("el" "#+BEGIN_SRC emacs-lisp\n?\n#+END_SRC"
           "<src lang=\"emacs-lisp\">\n?\n</src>"))
其他有用的绑定org-mode是:
(add-to-list 'org-structure-template-alist
         '("ao" "#+attr_org: " ""))
(add-to-list 'org-structure-template-alist
         '("al" "#+attr_latex: " ""))
(add-to-list 'org-structure-template-alist
         '("ca" "#+caption: " ""))
(add-to-list 'org-structure-template-alist
         '("tn" "#+tblname: " ""))
(add-to-list 'org-structure-template-alist
         '("n" "#+name: " ""))
(add-to-list 'org-structure-template-alist
         '("o" "#+options: " ""))
(add-to-list 'org-structure-template-alist
         '("ti" "#+title: " ""))
ein它也应该支持R。
                    (add-to-list 'org-structure-template-alist 	     '("p" . "src python")) 
                    看看YASnippet Emacs软件包。它允许您添加带有一些关键字和TAB之后的代码段。无论是在组织模式下还是在R脚本中,它都更加简单实用。您应该在.emacs.d / snippets / ess-mode中有一个文件夹(对于组织模式,应有一个文件夹),用于保存以下文件:
    # -*- mode: snippet -*-
    # name: in
    # key: in
    # -- 
    %in% 
键入TAB时,它会以%in%的形式粘贴,类似地:
# -*- mode: snippet -*-
# name: source_r
# key: srcr_
# --
#+BEGIN_SRC R 
$0
#+END_SRC
https://www.emacswiki.org/emacs/Yasnippet
https://joaotavora.github.io/yasnippet/snippet-development.html
(require 'org-tempo)添加到.emacs。此外,这是指向Easy Templates-> orgmode.org/manual/Easy-templates.html