A Xah Lee Challenge

Over at ergoemacs.org, Xah Lee posses the following challenge: Find a simple and general method to create the following text.

(global-set-key (kbd "<menu> g a") "A")
(global-set-key (kbd "<menu> g b") "B")
(global-set-key (kbd "<menu> g c") "C")
(global-set-key (kbd "<menu> g d") "D")
(global-set-key (kbd "<menu> g e") "E")
(global-set-key (kbd "<menu> g f") "F")
(global-set-key (kbd "<menu> g g") "G")
(global-set-key (kbd "<menu> g h") "H")
(global-set-key (kbd "<menu> g i") "I")
(global-set-key (kbd "<menu> g j") "J")
(global-set-key (kbd "<menu> g k") "K")
(global-set-key (kbd "<menu> g l") "L")
(global-set-key (kbd "<menu> g m") "M")
(global-set-key (kbd "<menu> g n") "N")
(global-set-key (kbd "<menu> g o") "O")
(global-set-key (kbd "<menu> g p") "P")
(global-set-key (kbd "<menu> g q") "Q")
(global-set-key (kbd "<menu> g r") "R")
(global-set-key (kbd "<menu> g s") "S")
(global-set-key (kbd "<menu> g t") "T")
(global-set-key (kbd "<menu> g u") "U")
(global-set-key (kbd "<menu> g v") "V")
(global-set-key (kbd "<menu> g w") "W")
(global-set-key (kbd "<menu> g x") "X")
(global-set-key (kbd "<menu> g y") "Y")
(global-set-key (kbd "<menu> g z") "Z")

As I write this, someone has already contributed a solution using a keyboard macro so I’ll give one in Elisp. The core of the solution is just the three lines

(dotimes (c 26)
  (insert (format "(global-set-key (kbd \"<menu> g %c\") \"%c\")\n"
                  (+ ?a c) (+ ?A c))))

If you’re going to use this more than once, then you should probably turn it into a function like so:

(defun a-to-z ()
  (interactive)
  (dotimes (c 26)
    (insert (format "(global-set-key (kbd \"<menu> g %c\") \"%c\")\n"
                    (+ ?a c) (+ ?A c)))))

Then you can just type 【Meta+xa-to-z whenever you need the text.

If it’s a one-off you can just type the 3 lines into your buffer, execute them with 【Ctrl+x Ctrl+e】 and then delete the Elisp.

If you don’t like having to erase the Elisp or you are going to need the text more than once in the buffer you can type

(with-current-buffer "name-of-working-buffer"
  (dotimes (c 26)
    (insert (format "(global-set-key (kbd \"<menu> g %c\") \"%c\")\n"
                    (+ ?a c) (+ ?A c)))))

in the scratch buffer and execute it with 【Ctrl+x Ctrl+e】 there. It will then put the text in your working buffer.

This entry was posted in Programming and tagged , . Bookmark the permalink.