With this elisp code you can easily configure an org-capture template that will:
- Ask for a title for the new
orgmode
file - Replace unsupported characters in the title for
-
for the file path - Generate a timestamp in the way
YYYYMMDDHHMMSS
- Create a file with the following name format:
YYYYMMDDHHMMSS-{encoded-title}.org
We first need a function to ask for the org file title and generate the file name:
(defun generate-new-file-name () "Ask for a title and generate a file name based on it"
(let* ((file-name (read-string "Title: "))
(my-path (concat
"~/org/notes/" ; Or whatever path you want
(format-time-string "%Y%m%d%H%M%S") "-"
(replace-regexp-in-string "[^a-zA-Z0-9-]+" "-" file-name) ".org"))) ; Replace invalid path characters
(setq mc/org-capture-filename file-name) ; Save variable to be used later in the template
my-path))
Note that this function also sets a variable called mc/org-capture-filename
that will be used later in the template
And here’s the org-capture-template
:
(setq org-capture-templates
'(
; ....
("n" "Note" entry (file (lambda () (generate-new-file-name)))
"* %(format mc/org-capture-filename) \n:PROPERTIES:\n:ID: %(org-id-new)\n:CREATED: %U\n:END:\n%?")
; ....
))
Note how in the template itself we’re using the mc/org-capture-filename
we defined early and also we’re setting some PROPERTIES
that are useful if you’re also using org-ql and org-roam