Skip to content

Using org capture to create new org files

Posted on:November 15, 2023 at 07:00 AM

With this elisp code you can easily configure an org-capture template that will:

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