6

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Leo Ufimtsev
  • 4,488
  • 3
  • 22
  • 45

2 Answers2

9

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:

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • 2
    Bonus points for explaining the 4/16/64 notion of prefix args ^_^. Thank you. – Leo Ufimtsev May 05 '15 at 14:01
  • This command `(insert (org-time-stamp '(16) t))` inserts two timestamps next to each other. Do you know why? – miguelmorin Oct 15 '18 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 Feb 15 '19 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 Mar 26 '20 at 09:46
  • Can this solution be modified to insert the date part only, and not the time? – user2567544 Dec 10 '21 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 !.

jagrg
  • 3,824
  • 4
  • 19
xji
  • 2,545
  • 17
  • 36