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
Drew
  • 75,699
  • 9
  • 109
  • 225
Xianwen Chen
  • 461
  • 1
  • 4
  • 4

6 Answers6

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
|#+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
Lorem Ipsum
  • 4,327
  • 2
  • 14
  • 35
23

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.

phils
  • 48,657
  • 3
  • 76
  • 115
glucas
  • 20,175
  • 1
  • 51
  • 83
  • 16
    For org version 9.2, this does not work for me out of the box. You need to have `(require 'org-tempo)` added to your `.emacs`. Also, here is an updated link to Easy Templates -> https://orgmode.org/manual/Easy-templates.html – JMT2080AD Jan 08 '19 at 18:16
  • 1
    @JMT2080AD Org 9.2 introduced new functionality which addresses this. See my answer below: https://emacs.stackexchange.com/a/47370/15177 – Lorem Ipsum Jan 23 '19 at 22:36
  • Is it `, i b` in `spacemacs`? – Jason Goal Jan 07 '20 at 23:06
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!

NickD
  • 27,023
  • 3
  • 23
  • 42
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: " ""))
manandearth
  • 2,068
  • 1
  • 11
  • 23
  • 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](https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html) I use python and Jupyter notebook via `ein` which should also support R . – manandearth Mar 21 '18 at 18:48
  • For org-version > 9.2, the syntax would be : `(add-to-list 'org-structure-template-alist '("p" . "src python")) ` – kotchwane Feb 25 '20 at 13:44
4

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

Arktik
  • 932
  • 4
  • 15
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.

choroba
  • 1,925
  • 10
  • 16