Inserting current date and time with EMACS

January 10, 2011

I was recently involved in an activity that required keeping track of notes with timestamps. I decided to do it in EMACS. Here’s what I stuffed in my .emacs init file:

;; Insert the current time in the current buffer
        ; If you want to insert date and time, you can use:
        (insert(format-time-string "%Y-%m-%d %H:%M:%S ")))
;; Bind Ctrl-T ("transpose") to the timestamp function - need a better key binding
(global-set-key (kbd "C-t") 'timestamp)

Incidentally, I also needed to save the file to DOS format instead of Unix format. To do that:

M-x set-buffer-file-coding-system

then use “dos” (or “mac” or “unix”).