index all rss twitter github linkedin email

Álvaro Ramírez

06 October 2019 Rewriting dates with Emacs multiple cursors

Needed to rewrite the date format in a couple of csv columns. Emacs multiple cursors helps here, but needed a function to parse and reformat the dates themselves.

I can likely reformat dates using the built-in parse-time-string and format-time-string functions, but hey why not give the ts.el library a try…

(defun ar/region-to-timestamp ()
  "Convert date like \"29 Apr 2019\" to \"2019-04-29\"."
  (interactive)
  (let ((date (ts-parse (buffer-substring
                         (region-beginning)
                         (region-end)))))
    (delete-region (region-beginning)
                   (region-end))
    (insert (ts-format "%Y-%m-%d" date))))

Bound the new function to a temporary keybinding, so I can invoke from multiple cursors:

(bind-key "M-q" #'ar/region-to-timestamp)

and voila!

ts.gif