On 2025-02-01 08:55, Uwe Brauer via General discussions about Org-mode.
wrote:
Is there any way to insert a header and automatically a timestamp with
date/time?
I can write me a small function doing this and bind it to some unused
key, but is there a more general/elegant way I just miss?
Hi Uwe,
TL DR; I add the timestamp automatically on entry creation as a hook
[2], in fact, I have several hooks on entry creation and I think is a
good way to handle this. I am happy with my setup since I started using
it in 2023.
Let me share my experience on that topic (and very happy to hear other
experiences and/or feedback), I hope the offtopic inside is welcome and
useful:
1. Before, I was manually adding timestamp headers as a postfix for the
creation of the header (and this is how I reverted using this script per
target buffer [1]) to transform to CREATED properties (see here the
automation on how I add the timestamp automatically on entry creation
[2]), which I think are even better, specially, because then you can add
that column to your table-reports such as clocktable, org-ql, propview,
custom org-map-entries stuff.
2. After, as a prefix, in the journal files, for facilitating
alphanumeric sorting of items in my customized journal (with a structure
of year > week > day). I finally got rid of that, and brain and eyes
appreciated that movement, it is too much noise to see a lot of items
with timestamp. I discovered that problem when I started using
variable-pitch mode, which with modes themes works very nice with
(enable variable: (setq modus-themes-mixed-fonts t) ). Then,
alternatively, sorting can be done by adding "SCHEDULED" to all items
created on certain buffers and levels [3], which I also add for certain
org-agenda files (then I use schedule as a poor's man alternative to
anki [4] for space repetition [5], which could of course involve real
tasks and projects apart from the typical usecase of memorizing concepts).
I do it this way and it works fine for me since that date I use it:
Cheers,
pinmacs
[1] see attached file: migrate-from-ts-header-to-created-property.org
[2] see attached file: auto-insert-created-timestamp-as-property.org
[3] see attached file: autoadd-scheduled-entry
[4] https://en.wikipedia.org/wiki/Anki_(software)
[5] https://en.wikipedia.org/wiki/Spaced_repetition
** org-insert-heading-hook(s) (7)
:PROPERTIES:
:CREATED: [2023-04-29 Sat 16:04]
:CUSTOM_ID: 2023-04-29_16:04:47.146113
:END:
*** 1. always add CREATED property
:PROPERTIES:
:CREATED: [2023-04-29 Sat 16:05]
:CUSTOM_ID: 2023-04-29_16:05:16.930966
:END:
#+name: created on [2023-04-29 Sat 16:06:09]
#+begin_src emacs-lisp
(defun my/org-heading-insert-heading-inactive-timestamp ()
(save-excursion
;; src https://emacs.stackexchange.com/questions/72147/org-mode-adding-creation-date-property-upon-heading-creation
(org-back-to-heading)
(my/set-property-with-inactive-timestamp "CREATED")))
;; src https://emacs-orgmode.gnu.narkive.com/xdMIn5QZ/o-how-can-i-calculate-the-age-of-a-headline
;; extra src https://emacs.stackexchange.com/questions/72147/org-mode-adding-creation-date-property-upon-heading-creation
(add-hook 'org-insert-heading-hook 'my/org-heading-insert-heading-inactive-timestamp 'append)
#+end_src
** org-insert-heading-hook(s) (7)
:PROPERTIES:
:CREATED: [2023-04-29 Sat 16:04]
:CUSTOM_ID: 2023-04-29_16:04:47.146113
:END:
*** 1. always add CREATED property
:PROPERTIES:
:CREATED: [2023-04-29 Sat 16:05]
:CUSTOM_ID: 2023-04-29_16:05:16.930966
:END:
#+name: created on [2023-04-29 Sat 16:06:09]
#+begin_src emacs-lisp
(defun my/org-heading-insert-heading-inactive-timestamp ()
(save-excursion
;; src https://emacs.stackexchange.com/questions/72147/org-mode-adding-creation-date-property-upon-heading-creation
(org-back-to-heading)
(my/set-property-with-inactive-timestamp "CREATED")))
;; src https://emacs-orgmode.gnu.narkive.com/xdMIn5QZ/o-how-can-i-calculate-the-age-of-a-headline
;; extra src https://emacs.stackexchange.com/questions/72147/org-mode-adding-creation-date-property-upon-heading-creation
(add-hook 'org-insert-heading-hook 'my/org-heading-insert-heading-inactive-timestamp 'append)
#+end_src
*** 4. always add SCHEDULED when in journal and project files
:PROPERTIES:
:CREATED: [2023-11-17 Fri 19:32]
:CUSTOM_ID: emacsconf_61
:END:
:BACKLINKS:
[2024-01-12 Fri 09:20] <- [[file:~/org/1activity.org::#1activity_3774][org agenda cleanup/puesta a punto, y vaciado de notas de móvil]]
:END:
#+name: created on [2023-11-17 Fri 19:33:09]
#+begin_src emacs-lisp
(defun my/org-heading-insert-scheduled ()
(if (or
(and (string-match-p my/diary-file (buffer-name))
(= (org-current-level) 4))
(and (string-match-p my/board-file (buffer-name))
(= (org-current-level) 4))
(and (string-match-p my/proj-file (buffer-name))
;; projects are 3-level (tasks start at 5-level, we don't care on its children)
(> (org-current-level) 4))
(and (string-match-p my/proj-candidates-file (buffer-name))
;; projects are 4-level (tasks start at 3-level, we don't care on its children)
(> (org-current-level) 3))
)
(save-excursion
;; src https://emacs.stackexchange.com/questions/72147/org-mode-adding-creation-date-property-upon-heading-creation
;;(org-back-to-heading)
(org-schedule nil (format-time-string "[%Y-%m-%d %a]" nil)))))
(add-hook 'org-insert-heading-hook 'my/org-heading-insert-scheduled 'append)
#+end_src