I added this part to my user-config in the init.el file.
(for spacemacs users that have their configuration in a .spacemacs file it should be in the user-config part of .spacemacs)
(add-hook 'before-save-hook #'zp/org-set-last-modified)
(defun zp/org-find-time-file-property (property &optional anywhere)
"Return the position of the time file PROPERTY if it exists.
When ANYWHERE is non-nil, search beyond the preamble."
(save-excursion
(goto-char (point-min))
(let ((first-heading
(save-excursion
(re-search-forward org-outline-regexp-bol nil t))))
(when (re-search-forward (format "^#\\+%s:" property)
(if anywhere nil first-heading)
t)
(point)))))
(defun zp/org-has-time-file-property-p (property &optional anywhere)
"Return the position of time file PROPERTY if it is defined.
As a special case, return -1 if the time file PROPERTY exists but
is not defined."
(when-let ((pos (zp/org-find-time-file-property property anywhere)))
(save-excursion
(goto-char pos)
(if (and (looking-at-p " ")
(progn (forward-char)
(org-at-timestamp-p 'lax)))
pos
-1))))
(defun zp/org-set-time-file-property (property &optional anywhere pos)
"Set the time file PROPERTY in the preamble.
When ANYWHERE is non-nil, search beyond the preamble.
If the position of the file PROPERTY has already been computed,
it can be passed in POS."
(when-let ((pos (or pos
(zp/org-find-time-file-property property))))
(save-excursion
(goto-char pos)
(if (looking-at-p " ")
(forward-char)
(insert " "))
(delete-region (point) (line-end-position))
(let* ((now (format-time-string "[%Y-%m-%d %a %H:%M]")))
(insert now)))))
(defun zp/org-set-last-modified ()
"Update the LAST_MODIFIED file property in the preamble."
(when (derived-mode-p 'org-mode)
(zp/org-set-time-file-property "LAST_MODIFIED")))
Is #+LAST_MODIFIED recognized by some other functions? I have tried to make time-stamp use the standard Org-mode #+date (I like lower case) keyword and only run it for Org mode files but I am not sure if #+date is allowed to have time.
I just watched your tutorials, thanks for making them!
One of the first things I noticed was the LAST_MODIFIED property in your file headers, and immediately searched out your config to steal your code. I’ve wanted this for a long time, so thank you.
I was trying to use time-stamp and had issues as well.
turns out the issue was time-stamp wasn’t searching far enough into the file.
set time-stamp-line-limit to something higher than 8 (default). I set it to 18.
also I am using last_modified and added a space between last_modified and time-stamp.
the following matches
Hi, I’m new in org-roam, and stumbled upon this thread wanting to just update my #+LAST_MODIFIED: field in my org-roam files. I have seen many responses, which one would do the job at its simplest? Thanks!