Update a field (#+LAST_MODIFIED: ) at save

Thanks, but it does not work this way.

I know very little about elisp but I don’t understand what part says not to change time-stamp-pattern in original code?

@tea4or2wo

Mine is this based on Jethro’s suggestion, and works perfectly in my markdown files (it should work on any other text file types, like org)

(add-hook 'after-init-hook
          (lambda ()
            (my/load-md-roam)
            ;; time stamp
            (setq time-stamp-start "modified:[       ]+\\\\?")
            (setq time-stamp-end "$")
            (setq time-stamp-format "%Y-%m-%dT%H%M%S")
            (add-hook 'before-save-hook #'time-stamp)))

For where you can see the recommendation not to change the variable:

The documentation of the variable time-stamp-pattern – you should be able to view it via C-h v (describe-variable)

@nobiot Thanks, I understand now what @jethro meant, I have read time-stamp-pattern doc already.

I was presuming make-local-variable (Alex) or setq-local (Jethro) allows to edit the variable locally only.

Yes. I would use setq-local (just personal convenience; never used the other one).

Tried all the modification in this thread. Only @zaeph method works, no luck with all the time-stamp methods.

The time-stamp seems working though with the following:

  (add-hook 'before-save-hook 'time-stamp)

it will fill the date following Time-stamp: <> or Time-stamp: " ", and I can change the time-stamp-format too.

It just does not work with any other keywords. Did anybody else have this issue? I am using Emacs 27.1 in Arch Linux.

What’s the value of time-stamp-start?

If Time-stamp: <> works, then I suspect it’s the matter of configuring this variable – it’s a regex. Have a look at this documentation from Emacs.

This is what worked for me:

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

#+last_modified: [2020-12-28 Mon 02:58:51]

on line 9.

(add-hook 'org-mode-hook (lambda ()
                             (setq-local time-stamp-active t
                                         time-stamp-line-limit 18
                                         time-stamp-start "^#\\+last_modified: [ \t]*"
                                         time-stamp-end "$"
                                         time-stamp-format "\[%Y-%m-%d %a %H:%M:%S\]")
                             (add-hook 'before-save-hook 'time-stamp nil 'local)))
1 Like

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!

Hey, did you find the working solution?