0

I've tried to setup a skeleton to speed up entering org source blocks

(define-skeleton skel-org-block
  "Insert an org block, querying for type."
  "Type: "
  "#+begin_" str " " (setq v1 (skeleton-read "Language and headers?: ")) "\n"
  _ - \n
  "#+end_" str "\n")
(global-set-key (kbd "C-c b") 'skel-org-block)

This works fine in a fresh .org file. But problems occur when I try to add a new source block above an existing one:

  • Doing C-c b above the block
#+begin_src python
blah
#+end_src

adds a comma before the first line of the old block:

#+begin_src python

#+end_src


,#+begin_src python
blah
#+end_src

  • If I'm adding a new block via C-c b to the very end of an org file, I can leave the "Language and headers?: " prompt in the minibuffer blank, and I get a simple org block. If I repeat this above an existing org block, I get a message saying org-edit-src-code: No such language mode: nil-mode.

Something's clearly going wrong with the line (setq v1 (skeleton-read "Language and headers?: ")), but I'm not sure what.

Is there a fix? Is there a simpler skeleton to help me create an org block on the fly, with headers? (I could always remove the setq line, but I'd like to be able to specify language and switches.)

2
  • 1
    I don't understand what the skeleton buys you: you still have to type the language and the headers which you might as well type in the source block instead. I personally use C-c C-, (bound to org-insert-structure-template) and Org tempo templates (<s TAB) as described in the manual: do C-h i g(org)Structure Templates.
    – NickD
    Oct 31 at 21:42
  • @NickD Ah OK -- I saw <s TAB> on Worg, but it wasn't working for me -- looking at the documentation, it turns out I needed (require 'org-tempo) in my init file. I wasn't aware of C-c C-,, which does what I want. The idea with the skeleton was that that I could order the construction of the block, so that I could get the language and headers out of the way before entering the functional code, and avoid having to move the cursor back and forth after creating the block. But your suggestions solve my use cases now. (If you add it as an answer I'll accept it). Nov 1 at 8:15
1

[Making my comment into an answer, as suggested]

I don't understand what the skeleton buys you: you still have to type the language and the headers which you might as well type in the source block instead. I personally use C-c C-, (bound to org-insert-structure-template) and Org tempo templates (<s TAB) as described in the manual: do C-h i g(org)Structure Templates.

For the Org tempo templates, you need to (require 'org-tempo).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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