Saturday, August 8, 2009

Emacs elisp-fu

No built-in command in Emacs to print the date into the current buffer. Bizarre.

Made my own. Prints something like "Sat Aug 8 19:14:47 2009" at point in current buffer when I press <C-c d>. In .emacs file:
;; insert current date in buffer
(defun insert-date-at-point ()
(interactive)
(insert (current-time-string)))
(global-set-key "\C-cd" 'insert-date-at-point)
Could also use a lambda expression directly in the global-set-key call.

I'd forgotten that you have to call (interactive) in your function to make it a valid 'command' which global-set-key will accept. More details in the Emacs elisp documentation under 'Interactive Codes' / 'Defining Commands'.

No comments:

Post a Comment