Question

I'm looking for the cleanest way to have a language agnostic keybinding that will insert my initials + formatted timestamp e.g.:

--<my initials> (28 Oct 2013 11:38:20 AM)

into an Emacs buffer, so that I can initial my comments. I know I could create a function and create a keybinding to call it, but I feel like Emacs probably has a de facto way to do stuff like this, presumedly through the use of macros.

No correct solution

OTHER TIPS

F3 (start recording macro)

--AA C-u M-! date C-e (insert text, insert output from date command)

F4 (stop recording macro)

C-x C-k n my-initial-timestamp (name the macro)

C-x C-k b ... (bind the macro to an appropriate key combination)

And finally, open ~/.emacs and type C-u M-x insert-kbd-macro RET my-initial-timestamp, which will insert the Lisp code to recreate the macro and bind it to the same key at startup.

Emacs does not have such a function, AFAIK. There is function time-stamp, which updates time-stamp templates in a buffer, but that is not what you want here.

This command gives you what you requested. Other time/date formats are available - see format-time-string. You can also use functions such as user-full-name and user-login-name, if you want your name instead of your initials. Here, I've just hard-coded your initials, NF. This should work for any buffer where a comment syntax is defined (comment-start).

(defun my-timestamp-comment ()
  (interactive)
  (insert (format "%s NF (%s)" comment-start
                  (format-time-string "%b %e %Y %r" (current-time)))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top