2 min read

Inserting current date and time with EMACS

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
(defun
 timestamp()
        (interactive)
        ; 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โ€).