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:
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.