I read the org-mode manual but did not find a short cut to insert:


I guess I need to create a configuration in ~/.emacs to set a shortcut? Or, is there a shortcut but I did not know?

I use R much in emacs. Is there a way to create a separate shortcut to create this?


Org-mode has a built-in mechanism for inserting source blocks. See Easy Templates in the Org manual. The default configuration provides templates for a number of #+BLOCKS, and you can add your own by customizing ‘org-structure-template-alist’.

For example, open an org buffer and type <sTAB to insert a BEGIN/END_SRC block.


[This is an addition/clarification to the answers by @glucas and @manandearth - it does not stand on its own.]

Note that in recent development versions (>= 9.2) of org-mode (and therefore, unless things change, in future stable versions), org-structure-template-alist has changed its type. The built-in easy-templates mechanism has been abandoned in favor of a more generic mechanism based on the tempo package. Unfortunately, the new mechanism only allows single character abbreviations after the <, so <s will be expanded to the #+BEGIN_SRC...#+END_SRC string the OP is asking for, but things like <el in @manandearth's answer will NOT work. This is still a work in progress, so things may change, but if you go with multi-letter <XXX abbreviations, be prepared for them to break when you upgrade org-mode to such a version.

There is also another mechanism: C-c C-, is bound to org-insert-structure-template which prompts you for the kind of block you want to insert (with a menu that lists all the available blocks and allows you to choose with a single key press). Its advantage is that it will wrap the begin/end around a region, so if you have already typed a bunch of stuff and you want to wrap it in a block, all you have to do is select it as a region and type C-c C-. followed by a single char to choose the type of block.

See this note [fn:1], the doc string of the (new) function org-insert-structure-template and the file org-tempo.el.

[fn:1] The link may be inaccurate because it points to a fixed line number in a file that may (nay, will!) change. If the link does not take you to the right place, search for the string "structure template expansion" - and feel free to edit this answer and fix the line number - thanks!


for key bindings for code blocks in babel-org try the following to bind <r followed by a <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>"))

You can modify the source to ESS as well.

To bind <p followed by a <tab> to a python block code, for example:

;; 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>"))

an emacs-lisp set to <el followed by a <tab> will be:

;; 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>"))

other useful bindings in org-mode are:

(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: " ""))
  • Could you explain a bit more regarding I could modify the source to ESS as well? Thanks! – Xianwen Chen Mar 21 '18 at 18:42
  • have a look at this link to the babel R page I use python and Jupyter notebook via ein which should also support R . – manandearth Mar 21 '18 at 18:48

The source block behavior changed in Org 9.2. The abbreviation <s no longer works by default. Instead, you use C-c C-, which calls org-insert-structure-template.

Adding a source block

Pressing C-c C-, brings up a dialog. Press TAB and then enter src R. This inserts a source block and puts your cursor at the start of the second line (let | be point):

#+begin_src R

To then edit inside the block, press C-o to open a new line.

You can save a template by adding an entry to the org-structure-template-alist. Put the following somewhere in your init.el or run it using C-x C-e:

(add-to-list 'org-structure-template-alist '("r" . "src R"))

Now when you do C-c C-, you will see an entry for r in the list. You can follow the same approach for other languages, such as Python, Ruby, etc. Simply replace the cdr in the dotted pair above (the "src R" part).

Additionally, to insert a line between the source block delimiters, you can insert a newline character using C-q C-j. That is, type src R C-q C-j in the cdr of the dotted pair above. The result will look like:

(add-to-list 'org-structure-template-alist '("R" . "src R

Now when you press R, Emacs will insert the source block and put your cursor between them on a new line (let | be point):

#+begin_src R
  • 4
    The <s binding still exists, you just need to include (require org-temp) in your init to enable it. – Tyler Jan 23 at 22:56
  • Typo correction: (require 'org-tempo) – NickD Feb 6 at 15:01

Have a look at YASnippet Emacs package. It allows you to add code snippets with some keywords and TAB after. It is far more simpler and useful for both within org mode and with R scripts. You should have a folder in .emacs.d/snippets/ess-mode (and one for org-mode) where you save files like this:

    # -*- mode: snippet -*-
    # name: in
    # key: in
    # -- 

When you type in TAB it sticks in %in%, similarly:

# -*- mode: snippet -*-
# name: source_r
# key: srcr_
# --




To add a new key combination to the org-mode, you can use

(define-key org-mode-map "\C-cs" 
    (lambda()(interactive)(insert "#+BEGIN_SRC R\n#+END_SRC")))

This will insert the string if you press C-cs.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.