I'm an experienced programmer, but totally new to (e)lisp, and I'd love some help orienting myself in a new project I'm trying out, specifically about any relevant org functions that maybe already do some of what I need.

Lets say I have two paragraphs of text, each in its own variable, one representing a question, the other an answer. I would like to insert these as two subheadings under a heading with some random name (say, the current time), which will go under an existing heading in an existing file. The file is always the same (say, questions.org), and the existing heading (i.e. already part of the file) is also always the same (say, * New Questions). If it matters, that file only has two top-level headings).

> * New Questions
> ** New heading (e.g. 17:37)
> *** Question
>     Question1
> *** Answer
>     Answer1

I already have a capture template that I use to create these headers (semi) manually, so I thought maybe I could leverage that somehow..

Thanks for any help!

  • Although isn't what you're asking for, om library may help you to understand the basics studying a lot less code than org-mode itself.
    – Muihlinn
    Jun 25, 2020 at 9:58
  • Thanks! That looks interesting. Jun 26, 2020 at 10:14

1 Answer 1


Ask yourself how you would do this if you were typing things out, then write the corresponding code.

First, you would move the cursor to just after the "New Questions" heading:

(re-search-forward "^\* New Questions$")

Then you would insert the new heading:

(insert "** 17:13\n")

Next, you would insert the question heading, and the question:

(insert "*** Question\n)
(insert the-question)
(insert "\n")

And finally the answer and its heading:

(insert "*** Answer\n")
(insert the-answser)
(insert "\n")

Of course you should wrap it all in a function:

(defun insert-new-q-and-a (the-question the-answer)
    (re-search-forward "^\* New Questions$")
    (insert "** 17:13")
    (insert "*** Question\n")
    (insert the-question)
    (insert "\n")
    (insert "*** Answer\n")
    (insert the-answser))
    (insert "\n")

There are indeed some org function that can help with navigating an org file, but you'll want to read the Org Mode manual for information about them. However, since org files are deliberately plain text, you can do this as simply as you like.

Of course this function assumes that you're already in the correct buffer, and it isn't interactive. You can easily write a function to open the correct file, make its buffer the current buffer, acquire questions and answers from some data source, and then have it call this function to do the insertion.

However, you should know that org capture should already be able to do all of this for you. Your capture template is more than just the default text of the new note, it is also instructions for what file to put it in, and how to figure out where in the file the new note should go. Perhaps you just need to read chapter 9.1.3 Capture Templates of the Org Mode manual again. It's available within Emacs via the info viewer (C-h i to start it), or online (although I notice that in the online version it is chapter 10.1.3, so perhaps that's for a slightly different version than I have installed; you may encounter other differences as well.)

  • Thanks! I'll look into templates more closely then. Jun 26, 2020 at 10:14

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.