Capturing Regions with Org Capture
28. February 2018
I’m an avid user of org-capture
for recording TODOs, code snippets, and
reference material without interrupting my train of thought. A quick C-c c t
lets me rattle off a note-to-self while capturing a link back to the buffer I’m
in for later reference.
I recently found myself frequently repeating the pattern of calling
kill-ring-save
on a region to yank it into my org-capture
buffer. By taking
advantage of org-capture-templates
and a little elisp
, this pattern can be
reduced to simply highlighting the target region before invoking capture
.
1
2
3
4
5
6
7
8
(defun my/org-capture-region-or-nil (s)
(if (and s (not (equal s "")))
(format "#+BEGIN_QUOTE\n%s#+END_QUOTE" s)))
;; Capture Template
("t" "todo" entry
(file+headline ,(my/org-dir-file "todo.org") "Unfiled")
"* TODO %?\n%U\n%a\n%(my/org-capture-region-or-nil \"%i\")\n")
Now if you select this template from the org-capture
template window with a
region highlighted, you’ll
end up in a buffer with the following text and your point located at |
.
1
2
3
4
5
6
** TODO |
[2018-02-28 Wed 13:39]
[[org-store-link-result]]
#+BEGIN_EXAMPLE
<Captured Text Here>
#+END_EXAMPLE
If there’s no region highlighted, the example block will not be present.
I hope you find this useful!