5

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
13

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.

  • 4
    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 -> orgmode.org/manual/Easy-templates.html – JMT2080AD Jan 8 at 18:16
5

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
5

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

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

0

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

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

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.