2

I created a yasnippet template to quickly timestamp a note I'd like to make in org-mode. The body of the yasnippet file reads

$0 
:PROPERTIES:
:CREATED: `(org-insert-time-stamp nil t t)`
:END:

However, the effect is the CREATED property being populated with double inactive timestamps, e.g., :CREATED: [2015-06-16 Tue 10:08][2015-06-16 Tue 10:08]. Why is this behavior occurring, and how do I prevent the redundant timestamp?


References:

Note: Trying either one of the methods described in the reference links yields the same doubled timestamp result.

kgo
  • 532
  • 3
  • 15
  • 1
    You may wish to investigate using `org-capture`, which provides for an option to insert at point: *To insert the capture at point in an Org buffer, call `org-capture` with a `C-0` prefix argument.* https://www.gnu.org/software/emacs/manual/html_node/org/Using-capture.html You can create your own templates -- inserting the current date is a simple as adding `CREATED: <%<%Y-%m-%d %a>>` for an active stamp . . . For templates, see: https://www.gnu.org/software/emacs/manual/html_node/org/Capture-templates.html#Capture-templates Be sure to read the doc-string for `org-capture-templates`. – lawlist Jun 16 '15 at 19:26

2 Answers2

6

This happens because org-insert-time-stamp inserts a time stamp (like the name says), but also returns the time stamp. Yasnippit inserts the return value but you're also left with the one inserted by the function call. A fix is to capture the return value, leaving only the inserted value: (let ((x (org-insert-time-stamp nil t t )))).

erikstokes
  • 12,686
  • 2
  • 34
  • 56
1

Adapted from an answer found in Elisp - Avoid prompt in interactive function, the following body of the yasnippet file works, but I'm sure there's a better solution using (org-insert-time-stamp) or similar. This also doesn't address the peculiar double timestamp behavior described in the original question.

$0
:PROPERTIES:
:CREATED: `(insert (format-time-string "[%Y-%m-%d %a %H:%M]"))`
:END:

NOTE: This solution does oddly leave two spaces between :CREATED: and the inserted timestamp. I'm not sure why.

kgo
  • 532
  • 3
  • 15
  • it works if you use `\`(format-time-string "[%Y-%m-%d %a %H:%M]" nil nil)\`` without the surrounding `(insert ... )`. This prevents double posting and also avoids the "modified buffer" error which I still get with the solution of @ersikstokes. For details check: https://joaotavora.github.io/yasnippet/snippet-development.html#org9804f4c – breathe_in_breathe_out Mar 10 '21 at 21:20
  • You are inserting something into a buffer: why would you *NOT* expect the buffer to be modified? – NickD Apr 11 '22 at 17:57