Skip to content

Instantly share code, notes, and snippets.

@Nidish96
Last active November 13, 2023 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nidish96/fbaeb37b616d7566dec38070e93369c8 to your computer and use it in GitHub Desktop.
Save Nidish96/fbaeb37b616d7566dec38070e93369c8 to your computer and use it in GitHub Desktop.
An Org-capture template to enable maintaining a directory-local notes file for notes that can be shared/version controlled with a project.
(setq org-default-notes-file (concat org-directory "/notes.org"))
(defvar org-local-notes-file)
(defun my/org-local-get-target ()
(if (boundp 'org-local-notes-file)
(expand-file-name org-local-notes-file)
(if (y-or-n-p "A local org note doesn't exist. Create/choose one? ")
(progn
(add-dir-local-variable
nil 'org-local-notes-file
(read-file-name "Create/choose local org notes file:"))
(save-buffer) (kill-buffer)
(hack-dir-local-variables-non-file-buffer)
(when (y-or-n-p "Add it to global agenda?")
(add-to-list 'org-agenda-files org-local-notes-file))
org-local-notes-file)
org-default-notes-file)))
;; Create the template
(setq org-capture-templates
'(("t" "Todo" entry (file+headline "~/org/notes.org" "Tasks")
"* TODO %?\n Dated: %u\n Location: %a")
("j" "Journal" entry (file+datetree "~/org/journal.org")
"* %?\nEntered on %U\n %i\n Location: %a")
("l" "Local Tasks" entry (file+headline my/org-local-get-target
"Local")
"* TODO %?\n Dated: %u\n Location: %a")
))
@Nidish96
Copy link
Author

Nidish96 commented Nov 8, 2023

The usage is simple:

While in a file within some repository, if you wanted to create a "local" note, you launch org-capture (default keybinding: C-c c) and choose the "Local Tasks" template using l.

  • The function my/org-local-get-target checks if there is a dir-local variable called org-local-notes-file already defined.
  • If defined, it puts the capture notes inside that file. If not, it prompts the user to check if the creation of a local notes file is desired.
    • If yes, you can choose/create a local notes file.
    • It next prompts if the user would like to add this local notes file to the global agenda list (org-agenda-files). It is added if yes. If not, the file is just created.
  • If no, it puts the notes in the global agenda file.
  • It next promts the usual org-capture.
  • From the next time org-capture is called with the "Local Tasks" template, the notes always go inside the local notes file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment