When input some code in orgmode, I can type:

<s Tab

and it expands to this:

#+BEGIN_SRC cursor


This is fine for writing something from scratch. But most of the time, I'd copy some paragraph which contains some code. (Code interposed in paragraphs).

In this scenario, after Tab, I have to select the code snippet, cut and paste them between BEGIN and END. Or cut the END_SRC, paste it at the end of the code.

Is there a way to do this automatically?

The ideal process would be:

  1. Select some code
  2. run a command, the code is well formatted by the BEGIN, END markdowns.

3 Answers 3


Here's a function that should do what you want. To use it, select a region, and run the command. It will handle putting in the #+BEGIN_SRC and #+END_SRC parts on new lines. It happens to leave you at the beginning of the source code block, but that could be changed.

(defun org-wrap-source ()
  (let ((start (min (point) (mark)))
        (end (max (point) (mark))))
    (goto-char end)
    (unless (bolp)
    (insert "#+END_SRC\n")
    (goto-char start)
    (unless (bolp)
    (insert "#+BEGIN_SRC\n")))

You can bind this function to an appropriate key if you like.

  • Thank you. I modified it slightly to move the cursor after BEGIN_SRC (and a space) to let me specify what language the code is in. So that the syntax highlighting and indentation would be right. Added ` (backward-char) (insert " ")` after the last expression.
    – Nick
    Dec 26, 2014 at 7:12

This function can do it for you: from chrisdone/chrisdone-emacs

(define-key org-mode-map (kbd "C-#") 'org-begin-template)
(defun org-begin-template ()
  "Make a template at point."
  (if (org-at-table-p)
      (call-interactively 'org-table-rotate-recalc-marks)
    (let* ((choices '(("s" . "SRC")
                      ("e" . "EXAMPLE")
                      ("q" . "QUOTE")
                      ("v" . "VERSE")
                      ("c" . "CENTER")
                      ("l" . "LaTeX")
                      ("h" . "HTML")
                      ("a" . "ASCII")))
               (concat (propertize "Template type: " 'face 'minibuffer-prompt)
                       (mapconcat (lambda (choice)
                                    (concat (propertize (car choice) 'face 'font-lock-type-face)
                                            ": "
                                            (cdr choice)))
                                  ", ")))))))
      (let ((result (assoc key choices)))
        (when result
          (let ((choice (cdr result)))
              (let ((start (region-beginning))
                    (end (region-end)))
                (goto-char end)
                (insert "\n#+END_" choice)
                (goto-char start)
                (insert "#+BEGIN_" choice "\n")))
              (insert "#+BEGIN_" choice "\n")
              (save-excursion (insert "\n#+END_" choice))))))))))

My preference:


  • I like the structure templates; they are easy to remember and I use them all of the time
  • You can't automate something until you know what you want to do and the macro recorder makes that really easy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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