36

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

#+BEGIN_SRC 
#+END_SRC

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?

#+BEGIN_SRC R
#+END_SRC

6 Answers 6

49

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
|#+end_src

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
|
#+end_src
3
  • 8
    The <s binding still exists, you just need to include (require org-temp) in your init to enable it.
    – Tyler
    Commented Jan 23, 2019 at 22:56
  • 5
    Typo correction: (require 'org-tempo)
    – NickD
    Commented Feb 6, 2019 at 15:01
  • How did u generate the gif, please?
    – user19777
    Commented Mar 26, 2023 at 18:01
24

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.

3
18

[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!

2
  • One thing I prefer about C-c C-, over <s is that it allows you to select a text and "wrap" it around the structure template by simply typing C-c C-,. With <s you need to cut and paste the line that ends the template (i.e., the #+END_SRC).
    – SaMeji
    Commented Apr 27, 2020 at 15:31
  • Yes, I mention that in the answer.
    – NickD
    Commented Apr 27, 2020 at 16:25
7

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: " ""))
3
  • Could you explain a bit more regarding I could modify the source to ESS as well? Thanks! Commented Mar 21, 2018 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 . Commented Mar 21, 2018 at 18:48
  • For org-version > 9.2, the syntax would be : (add-to-list 'org-structure-template-alist '("p" . "src python"))
    – kotchwane
    Commented Feb 25, 2020 at 13:44
5

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
    # -- 
    %in% 

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

# -*- 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

2

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.

0

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