Org-Mode - How do I create a new file with org-capture?

别来无恙 提交于 2021-02-18 11:29:09

问题


I want to create an org-capture template that create a dynamic file name for capture in emacs org-mode.

I want that the name of the file takes the following form: (format-time-string "%Y-%m-%d") "-" (prompt for a name) ".txt"

Example : 2012-08-10-MyNewFile.txt

Based on this answer, I know how to dynamically create the name the file to include the date:

`(defun capture-report-date-file (path)
(expand-file-name (concat path (format-time-string "%Y-%m-%d") ".txt")))

'(("t" "todo" entry (file (capture-report-date-file  "~/path/path/name"))
"* TODO")))

This allows me to create a file 2012-08-10.txt and to insert * TODO at the first line

How could I add a prompt to complete the file name?


回答1:


You'll have to use (read-string ...) in capture-report-data-file to generate the filename on the fly.

(defun capture-report-data-file (path)
  (let ((name (read-string "Name: ")))
    (expand-file-name (format "%s-%s.txt"
                              (format-time-string "%Y-%m-%d")
                              name) path)))

'(("t"
   "todo"
   entry
   (file (capture-report-date-file  "~/path/path/name"))
   "* TODO")))

This will prompt on capture for the file name, and then open the capture buffer will be created.




回答2:


I use below template & function to create new file.

  (defun psachin/create-notes-file ()
    "Create an org file in ~/notes/."
    (interactive)
    (let ((name (read-string "Filename: ")))
      (expand-file-name (format "%s.org"
                                  name) "~/notes/")))



   (setq org-capture-templates
     '(("n" "Notes" entry
        (file psachin/create-notes-file)
       "* TITLE%?\n %U")))


来源:https://stackoverflow.com/questions/11902620/org-mode-how-do-i-create-a-new-file-with-org-capture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!