Elisp: Insert Random UUID

By Xah Lee. Date: . Last updated: .

Here's a emacs command to insert a UUID.

(defun xah-insert-random-uuid ()
  "Insert a UUID.
This commands calls “uuidgen” on MacOS, Linux, and calls PowelShell on Microsoft Windows.
URL `http://xahlee.info/emacs/emacs/elisp_generate_uuid.html'
Version 2020-06-04"
   ((string-equal system-type "windows-nt")
    (shell-command "pwsh.exe -Command [guid]::NewGuid().toString()" t))
   ((string-equal system-type "darwin") ; Mac
    (shell-command "uuidgen" t))
   ((string-equal system-type "gnu/linux")
    (shell-command "uuidgen" t))
    ;; code here by Christopher Wellons, 2011-11-18.
    ;; and editted Hideki Saito further to generate all valid variants for "N" in xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx format.
    (let ((myStr (md5 (format "%s%s%s%s%s%s%s%s%s%s"
      (insert (format "%s-%s-4%s-%s%s-%s"
                      (substring myStr 0 8)
                      (substring myStr 8 12)
                      (substring myStr 13 16)
                      (format "%x" (+ 8 (random 4)))
                      (substring myStr 17 20)
                      (substring myStr 20 32)))))))

Thanks to Christopher Wellons [http://nullprogram.com/] , Hideki Saito http://hideki.hclippr.com/2014/02/02/on-generating-uuid/ . And [Yuri Khan https://plus.google.com/+YuriKhan/posts], Jon Snader http://irreal.org/blog/ for discussion about UUID.

Spent a few hours here?

Emacs Tutorial

Emacs Init

Emacs Keys

Elisp Basics

Elisp Examples

Elisp Advanced

Elisp Write Major Mode

Elisp Examples

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples