As @kaushalmodi mentions in the comments you can use (org) Structure Templates
to speed up insertion of different types of blocks.
The general procedure is to insert <
followed by a template selector (usually a single letter) on an otherwise empty line and press TAB.
The template selector for a generic source block template is s
, so typing <s
followed by TAB will give you this:
#+BEGIN_SRC
#+END_SRC
Point will be positioned at the end of the first line.
This is a good first approximation of what you want to achieve, but this is Emacs, so let's make it better!
You can define custom templates by adding one or more entries to a variable called org-structure-template-alist
. For example:
(add-to-list 'org-structure-template-alist '("n" "#+NAME: ?"))
This code adds a #+NAME:
template to org-structure-template-alist
, using n
as a template selector. After expanding this template point will be positioned at the location of ?
.
If you always name your code blocks, you can also overwrite the original version of the source block template with an extended version that includes the #+NAME:
line:
(add-to-list 'org-structure-template-alist
'("s" "#+NAME: ?\n#+BEGIN_SRC \n\n#+END_SRC"))
Typing <s
followed by TAB will then give you:
#+NAME:
#+BEGIN_SRC
#+END_SRC
This is just the tip of the iceberg; you can use a similar approach to define additional templates for language-specific code blocks, code blocks with specific header arguments, etc.
hydra
package: blogpost (3) Another snippet for easy org source block insertion.C-c C-,
is detailed on this post.