Shortcut for inserting environments in `org-mode`
Asked Answered
T

4

9

I'm using org-mode for organizing myself (very useful so far!). However, it is kind of annoying writting

  #+begin_comment
  ...
  #+end_comment

each time I'd like to insert an environment.

Question

Is there a shortcut to insert the #+begin_ and #+end_ for a given environment?

In the same way C-c C-o comment RET would insert

\begin{comment}

\end{comment}

in latex-mode.

Tatman answered 2/10, 2013 at 19:41 Comment(0)
K
17

Org has a facility called "Easy templates": http://orgmode.org/manual/Easy-Templates.html

A template for comment is missing but you can add it with:

(add-to-list 'org-structure-template-alist '("C" "#+begin_comment\n?\n#+end_comment"))

And use it by typing <C followed by TAB.

Alternatively, you could use yasnippet.

Koby answered 2/10, 2013 at 20:38 Comment(2)
I just found out that there is another keystroke for inserting src-blocks: C-c C-v d.Spat
and, even nicer, if it is invoked within a code block will split it into two separate code blocks, and if it is invoked outside a code block will surround the current selection with a new code blockCommission
S
7

Now the corresponding template section is called Structure Template and the insertion sequence is invoked by C-c C-,. I didn't (require 'org-tempo) which is described to support insertion keys like <s TAB.

The comment environment is already defined in org-structure-template-alist. So the comment would be inserted by

C-c C-, C

It's still possible to add a user defined sequence by, for example,

C-c C-, [TAB|RET|SPC] src python :results output :session

delivering

#+begin_src python :results output :session
#+end_src

(emacs 25.2.2, org-mode 9.2)

Sirois answered 23/1, 2019 at 15:26 Comment(0)
S
1

Not as elegant as the answer of Michael Markert but maybe more expandable.

1) You can select a region and put the block around it or you can just put the block at point.

2) Keyword expansion and history.

3) Keystrokes: C-c b

The command could be further expanded. E.g., for the src block the various switches like -n -r and export to files could be supported.

(defun list-major-modes ()
  "Returns list of potential major mode names (without the final -mode).
Note, that this is guess work."
  (interactive)
  (let (l)
    (mapatoms #'(lambda (f) (and
                 (commandp f)
                 (string-match "-mode$" (symbol-name f))
                 ;; auto-loaded
                 (or (and (autoloadp (symbol-function f))
                      (let ((doc (documentation f)))
                    (when doc
                      (and
                       (let ((docSplit (help-split-fundoc doc f)))
                         (and docSplit ;; car is argument list
                          (null (cdr (read (car docSplit)))))) ;; major mode starters have no arguments
                       (if (string-match "[mM]inor" doc) ;; If the doc contains "minor"...
                           (string-match "[mM]ajor" doc) ;; it should also contain "major".
                         t) ;; else we cannot decide therefrom
                       ))))
                 (null (help-function-arglist f)))
                 (setq l (cons (substring (symbol-name f) 0 -5) l)))))
    (when (called-interactively-p 'any)
      (with-current-buffer (get-buffer-create "*Major Modes*")
    (clear-buffer-delete)
    (let ((standard-output (current-buffer)))
      (display-completion-list l)
      (display-buffer (current-buffer)))))
    l))

(defvar org-insert-block-hist nil
  "History for command `org-insert-block'")
(defvar org-insert-block-hist/src:major nil
  "History for major mode in org src blocks.")
(defvar org-insert-block-list (append org-protecting-blocks
                   '("comment" ""))
  "List of block types offered as completion for command `org-insert-block'")
;; block_src switches: -n () -r (references) -l "((%s))" (label format) -k (keep labels)
(defvar org-insert-block-list-specials
  "Assoc list of Commands for reading additional specification of org-blocks.")
(setq org-insert-block-list-specials
      '(("src" . (concat " " (completing-read "Major mode:"
                        (list-major-modes)
                        nil nil
                        (car org-insert-block-hist/src:major)
                        '(org-insert-block-hist/src:major . 1)
                        )))))

(defun org-insert-block (bl &optional b e attributes)
  "Put region between b and e into org-block of kind bl.
If b or e is nil then put org-block limiters around point.
The string attributes is inserted behind the string #+begin_... "
  (interactive
   (let ((usereg (use-region-p))
     (blKind (completing-read "Input block kind (tab: completion, uparrow: history):"
               org-insert-block-list nil nil (car org-insert-block-hist) '(org-insert-block-hist . 1))))
     (list
      blKind
      (when usereg (region-beginning))
      (when usereg (region-end))
      (let ((spec (assoc blKind org-insert-block-list-specials)))
    (when spec (eval (cdr spec)))
    ))))
  (let ((begBlock (concat "\n#+begin_" bl attributes "\n"))
    (endBlock (concat "\n#+end_" bl "\n")))
    (if (and b e)
    (save-restriction
      (narrow-to-region b e)
      (goto-char (point-min))
      (insert begBlock)
      (goto-char (point-max))
      (insert endBlock)
      (indent-region (point-min) (point-max)))
      (let ((p (point)))
    (insert endBlock)
    (goto-char p)
    (insert begBlock))
      )))
(add-hook 'org-mode-hook '(lambda ()
                (local-set-key (kbd "C-c b") 'org-insert-block)))
Spat answered 2/10, 2013 at 21:9 Comment(1)
The comment above deserved an upvote all on its own (with the stuff @Commission added)Inflammatory
R
1

You could have a look at "org-auctex-keys.el", a minor mode which I created to offer AUCTeX key bindings within Org documents.

In this case, you'd use C-c C-e to insert an environment (prompt to enter the environment name), as what AUCTeX does.

If you're interested, check it out at https://github.com/fniessen/org-auctex-key-bindings.

Rampage answered 3/10, 2013 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.