22

This question already has an answer here:

I found the command SPC m x c to insert a small code snippet like ~~. I would like to insert a longer snippet like

 #+BEGIN_SRC 
 #+END_SRC

Is there a command to do this? I found a way to add it for markdown, which is SPC m x c

marked as duplicate by Kaushal Modi, Drew, dgtized, elethan, erikstokes Jan 31 '16 at 15:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • See yasnippts. – mankoff Jan 30 '16 at 13:04
  • 4
    No you don't need yasnippets. Just ``<s TAB` will insert that begin end src block. – Emacs User Jan 30 '16 at 14:01
44

Yes, it's called easy templates.

Org mode supports insertion of empty structural elements (like ‘#+BEGIN_SRC’ and ‘#+END_SRC’ pairs) with just a few key strokes. This is achieved through a native template expansion mechanism. Note that Emacs has several other template mechanisms which could be used in a similar way, for example ‘yasnippet’.

To insert a structural element, type a ‘<’, followed by a template selector and ‘TAB’. Completion takes effect only when the above keystrokes are typed on a line by itself.

For your precise question: <s then TAB.

See others in the documentation.

Note it's not specific to spacemacs, since it's from native org-mode

  • 2
    <s doesn't work in Spacemacs? How does one invoke the org-mode template leader key from Spacemacs? – shark8me Sep 25 '17 at 9:02
  • 1
    @shark8me It works in Spacemacs. Just go into the Insert mode and then press <s followed by TAB. That's all. – shivams Nov 4 '17 at 18:59
  • 9
    Note, this is changing a bit from Org 9.2. For "<s" snippets you need to (require 'org-tempo). Blocks can also be inserted using the interface accessed via C-c C-,. The syntax of org-structure-template-alist is also changed. – rasmus Apr 11 '18 at 22:48
  • @rasmus (require 'org-tempo) did the trick for me, in Spacemacs. Thanks! – Sankalp Jun 4 at 22:30
  • FYI, this behavior changed in Org 9.2. The new method is given in the release notes. Basically, press C-c C-, for a list of templates and then select s for source. @rasmus above explains how to continue using the old way in newer versions of org-mode. web.archive.org/web/20190117084828/https://orgmode.org/… – Lorem Ipsum Aug 26 at 23:49

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