6

In general all timestamps in Org-mode appears as [YYYY-MM-DD Day]. I have one file (buffer) where I would like the format to be different. Either just [YYYY-MM-DD] or [YYYY-MM]. How do I tell Org-mode to use a custom format for this one buffer?

I'd be happy to have it automatic, which I think means something like:

# -*- coding: utf-8; eval: (org-time-stamp-custom-formats '("<%Y-%m>")); -*-

As the first line, or have a code block that I evaluate with C-c C-c each time I work in that buffer.

#+BEGIN_SRC emacs-lisp
(make-local-variable 'org-time-stamp-custom-formats)
(make-local-variable 'org-display-custom-times)
(setq org-time-stamp-custom-formats '("<%Y-%m>"))
(setq-default org-display-custom-times t)
#+END_SRC
4

You need to define the variable org-time-stamp-custom-formats, not to call a function. What you type after the variable name and colon is a value (what you might type after ').

-*- org-time-stamp-custom-formats: ("<%Y-%m>" . "<%Y-%m-%d %H:%M>") -*-

or

# Local Variables:
# org-time-stamp-custom-formats: ("<%Y-%m>" . "<%Y-%m-%d %H:%M>")
# End:

Note that the variable must contain two formats — conventionally one with only the date and one with the date and time.

Org mode doesn't declare this variable as being customizable in file options, so you will get a warning the first time you load a file. Choose ! to accept this particular value of org-time-stamp-custom-formats (Emacs will update your init file). More generally, you can validate a class of values of org-time-stamp-custom-formats as being safe by declaring a safe-local-variable property (it would make sense for Org mode itself to do it).

(defun org-time-stamp-custom-formats-safep (value)
  (and (consp value)
       (stringp (car value))
       (stringp (cdr value))))
(put 'org-time-stamp-custom-formats 'safe-local-variable #'org-time-stamp-custom-formats-safep)

Furthermore you need to add the customtime startup option, as described in the documentation of org-display-custom-times. Thus, in addition to the -*- lines or Local Variables: block above, you need this directive anywhere in your file:

#+STARTUP: customtime
3

The following will work:

# Local Variables:
# org-time-stamp-custom-formats: ("<%Y-%m>" . "<%Y-%m %H:%M>")
# eval: (org-toggle-time-stamp-overlays)
# End:

Using (org-toggle-time-stamp-overlays) (C-cC-xC-t) to ensure they are properly displayed.

You will need to confirm that these are safe local variables for them to apply however once they are approved the document will load with them already showing.

Toggle command referenced from the Org Manual


Edit

Showing differences between the various local variable settings

The three pictures below show:

  1. Answer as above
  2. Answer without including the eval line. This does not show the custom timestamps. C-cC-xC-t will toggle them to be visible
  3. Incomplete org-time-stamp-custom-formats specification. If custom timestamps are toggled the timestamps are no longer fontified. They will show properly once custom timestamp overlays are removed.

Various Local Variable Settings

  • Bleh, eval in file-local variables is evil. Fortunately it isn't needed here as far as I can tell (see my answer). – Gilles 'SO- stop being evil' Oct 16 '14 at 14:49
  • Only reason for eval was to automatically show the text-properties for the custom time format. Can skip it and just C-cC-xC-t after loading file – Jonathan Leech-Pepin Oct 16 '14 at 14:52
  • Can you explain what difference it makes? I see no difference in my testing. – Gilles 'SO- stop being evil' Oct 16 '14 at 15:07
  • @Gilles Added a screenshot showing three cases (with and without eval, as well as with only part of the cons cell for the custom format). – Jonathan Leech-Pepin Oct 16 '14 at 15:14
  • You're missing #+STARTUP: customtime – Gilles 'SO- stop being evil' Oct 16 '14 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.