7

C-u C-c ! generates a time stamp like [2015-05-04 Mon 17:13]

I would like to assign a shortcut (e.g F1) to this action.

So far I have:

(defun my/timenow (&optional arg) 
 (interactive) 
 (let ((current-prefix-arg 4)) ;; emulate C-u
 (org-time-stamp arg 'inactive)
 )
)

But this prompts me for the time and I have to press RET to insert it.

How can I insert the inactive timestamp without any prompts at all?

2 Answers 2

11

From the documentation of org-time-stamp:

With two universal prefix arguments, insert an active timestamp with the current time without prompting the user.

So eval the below:

(org-time-stamp '(16) t)
  • '(4) - one prefix arg (4)
  • '(16) - two prefix args (4 * 4)
  • '(64) - three prefix args (4 * 4 * 4)

To read more about the universal arguments and arguments in general:

5
  • 2
    Bonus points for explaining the 4/16/64 notion of prefix args ^_^. Thank you. Commented May 5, 2015 at 14:01
  • This command (insert (org-time-stamp '(16) t)) inserts two timestamps next to each other. Do you know why?
    – emonigma
    Commented Oct 15, 2018 at 15:42
  • 1
    @mmorin if you look at org-time-stamp code (use M-x find-function), it's inserting by itself, so you don't need extra insert. Alternatively, you can use insert with a combination of format-time-string and org-time-stamp-format.
    – karlicoss
    Commented Feb 15, 2019 at 23:37
  • One interesting thing I noticed is that org-time-stamp requires an argument to be called since it's (defun org-time-stamp (arg &optional inactive) ...). I wonder what argument is actually passed in when you press C-c .. I was not able to simulate that programatically.
    – xji
    Commented Mar 26, 2020 at 9:46
  • Can this solution be modified to insert the date part only, and not the time? Commented Dec 10, 2021 at 14:27
1

Now there is a dedicated function for this (see org-time-stamp-inactive). For example:

(defun my/timenow ()
 (interactive)
 (let ((current-prefix-arg '(16)))
   (call-interactively 'org-time-stamp-inactive)))

(define-key org-mode-map (kbd "<f1>") 'my/timenow)

You can also skip the prompt with two universal prefix arguments: C-u C-u C-c !.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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