1

I often have this problem in Org mode: I write an item with org-capture, refile it, and days later I see a result like this:

*** TODO some task
some notes** StackExchange

I believe that the problem is that org capture inserts the text into the buffer, and if the item does not end with a newline, it gobbles the delimiter to the next heading and both get refiled together.

I already added two newlines to my capture template to minimize this problem. My capture template is:

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/gtd.org" "Reminders")
     "* TODO %?\n\n"))

If I try a new item with C-c c t and type something, I see it appear in the main buffer. If I delete the newline at the end, the newline is deleted in the buffer too and gobbles the next line. Deleting further characters in the capture buffer does not delete them from the main buffer.

I think I could add a hook such as (add-hook 'org-capture-before-finalize-hook ...) but I'm not sure how to include a newline and how to do it before refiling, to avoid the next item being refiled with the captured item.

How can I fix this problem?

Update: Following @Aquaactress's suggestions, I tried two methods. Both fail and the versions may be important:

Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /Applications/Emacs.app/Contents/Resources/lisp/org/)
GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20 Version 10.14.3 (Build 18D109)) of 2019-09-02

In the first method, I added :empty-lines in the org-capture template:

(setq org-capture-before-finalize-hook nil)

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/test.org" "heading 1")
     "* TODO %?\n" :empty-lines 1)))

The result in screenshots is below: I add a capture item, delete newlines from the end of the org-capture buffer, and refile. The heading next to the refiled item is gobbled:

Empty lines method, step 1 - create an org-capture item

Empty lines method, step 2 - delete lines from end of org-capture buffer

Empty lines method, step 3 - refile

In the second method, I added a function that inserts new lines at the beginning and end of the org capture buffer (for exposition, I also add hello world! to show that these insertions go at the beginning and end of the whole org buffer, not the org-capture buffer):

(setq org-capture-before-finalize-hook nil)

(defun +org|insert-newlines ()
  (beginning-of-buffer) (insert "\nhello world!\n")
  (end-of-buffer) (insert "\nhello world!\n"))

(add-hook 'org-capture-before-finalize-hook #'+org|insert-newlines)

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/test.org" "heading 1")
     "* TODO %?\n" :empty-lines 1)))

The result in screenshots is below: I add a capture item, delete newlines from the end of the org-capture buffer, and refile. The heading next to the refiled item is gobbled and the lines inserted go at the beginning and end of the buffer, instead of the org-capture buffer.

Insert lines method, step 1 - add org-capture item

Insert lines method, step 2 - delete newlines from end of org-capture buffer

Insert lines method, step 3 - refile

2

As you've noticed org capture by default disregards newlines when placing headlines. To remedy is a setting you can add to capture templates called :empty-lines. Here's what the manual says about it.

`:empty-lines'
   Set this to the number of lines to insert before and after the
   new item.  Default 0, only common other value is 1.

Here is an example of it's usage in my own capture template.

(setq org-capture-templates
      `(("t" "emacs" plain (file ,(concat org-directory "todo.org"))
         "* TODO %?\n" :empty-lines 1)))
  • That increases the number of new lines in the capture buffer, but if I accidentally delete them the next heading will still be gobbled. – miguelmorin Nov 3 at 21:09
  • I don't know if this is less of an org-capture question and more of a "how to not accidentally delete newlines at the end of a buffer" question. How about adding this (defun +org|insert-newlines-at-end () (end-of-buffer) (insert "\n\n")) as a hook to org-capture-before-finalize-hook? – Aquaactress Nov 4 at 18:14
  • The next heading is still gobbled, e.g. when I refile I get *** TODO some task**StackExchange. The gobbling happens before the end of org-capture and so before any run of a hook. – miguelmorin Nov 10 at 21:46
  • I'm confused. Globbing requires at least two headings. If you're adding just one heading per capture, I don't see how it could be globbled before org-capture-before-finalize hook is called. Could it be that you need to append the newlines at the beginning (before the heading) instead of at the end? How about (defun +org|insert-newlines-at-beg () (beginning-of-buffer) (insert "\n\n"))? – Aquaactress Nov 11 at 3:19
  • Actually to simulate the behavior of empty-lines use both of the hooks I gave you (or alternatively merge them into one). Adding new lines at the beginning and at the end should really work for sure. I can't imagine that not working. – Aquaactress Nov 11 at 3:42

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.