4

I have a capture template for TODO items defined as follows

("t" "todo" entry (file "~/org/refile.org") "* TODO %?\n%U\n%a\n" :clock-in t :clock-resume t)

While editing a document, I would like to insert the template where my cursor is. An example foll ows:

My buffer looks like this:

* TODO make a sandwich
    <cursor here>

I call my new command, org-insert-template.

I am prompted with the available capture templates and choose [t] TODO

My buffer now looks like this:

* TODO make a sandwich
  * TODO <cursor here>
    :LOGBOOK:
    CLOCK: [2019-05-06 Mon 23:09]--[2019-05-06 Mon 23:09] =>  0:00
    :END:
    [2019-05-06 Mon 23:09]

Could you please provide guidance on implementation of org-insert-template?

0

2 Answers 2

3

There's no need for implementing your own version of this! Just call org-capture with a prefix argument of zero. For example, I bind org-capture to C-c c, so to insert a template at point I can use C-0 C-c c.

This features is mentioned in the help for org-capture:

When called with a ‘C-0’ (zero) prefix, insert a template at point.

0

Simplest working code:

("t" "todo" plain       
(function (lambda nil (goto-char  (point))))
"* TODO %?\n%U\n%a\n" :clock-in t :clock-resume t)

Explanation:

  • (function (lambda nil (goto-char (point))))

    According to the org-capture-template documentation, you can use a function that will find the location that you want the capture temptate to be placed.

    (lambda nil ...) Creates a lambda function with no arguments

    (point) is the current location. (goto-char (point)) goes to the current location.

  • ("t" "todo" plain

    plain means plain text instead of org-entry. When using plain, it will always capture to the current location. Using entry can sometimes insert the entry at the end of the current headline or the end of the file.

Other Notes:

I recommended using customize interface for complex customizations M-x customize. It will show you a list of from options from the [Value Menu]. enter image description here

Your Answer

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

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