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”).

Advertisements

One Response to Inserting current date and time with EMACS

  1. Ian Eure says:

    The bundled time-stamp library does this and lots more, like auto-update timestamps in your file’s header. The time-stamp-string function is the one you’d want to get the current time to put in your buffer.

    Also, the “C-c (single letter)” bindings are generally reserved for user bindings, so C-c t would be a good choice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: