Emacs Quiz: Insert A to Z Vertically

By Xah Lee. Date:

Emacs editing exercise.

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")

A practical problem. You can use any method.

How do you do it?

Instead of minimum number of keystrokes as in vim golf, you win if your solution is more general and conceptually simple.

i'll post my solution in 2 days.

posted to https://plus.google.com/113859563190964307534/posts/7twaGCEpMqE

Solutions

using elisp

here's my solution. I used elisp.

(dotimes (ii 26 )
  (insert
   (format "(global-set-key (kbd \"<menu> g %c\") \"%c\")\n" (+ ii 65 32) (+ ii 65))
   )
  )

several people also gave similar elisp solution.

Here's one by [Holger Durer https://plus.google.com/108144730735303895371/posts]

;; Holger Durer 2013-01-23
(apply #'concat
  (loop for c from ?a to ?z
        collect (format "(global-set-key (kbd \"<menu> g %c\") \"%c\")\n" c (upcase c))))

Holger also recommend a command that eval elisp and replace the elisp code with its output. Quote:

Then invoke a slightly modified fc-eval-and-replace command (which I copied from http://emacs.wordpress.com/2007/01/17/eval-and-replace-anywhere/ ) — my modification is simply to take a prefix argument and depending on that call prin1 or princ (princ is what you want here).

see Holger's post [here https://plus.google.com/b/113859563190964307534/113859563190964307534/posts/7twaGCEpMqE]

Holger is a lisper. He also has a blog, frequently about emacs or lisp. See: http://posterous.betareduction.info/

Using Emacs + Shell

[Morgen Peschke https://plus.google.com/b/113859563190964307534/114228370997441057276/posts] gave the following.

I'd probably start by typing this into the buffer:

(global-set-key (kbd '<menu> g 'a') 'a')

Then I'd wrap a bash command around it like so:

for a in {a..z}; do echo "(global-set-key (kbd '<menu> g '$a') '$a')"; done

C-a C-k loads it into the buffer, then C-[SPC] C-u C-| C-y [ENTER] runs a shell command, inserting into the buffer.

This gives a basic structure, the case of the second letter I'd fix with a simple macro: M-u [LEFT] [DOWN]

Using the C-u 0 prefix to run down the column almost finishes it off.

The final step would be to fix the quotation marks (the bash script is easier to write with the quotes switched: M-x replace-string [ENTER] ' [ENTER] "

Solution by Key Macro

fred gave the following macro solution (in comment of this page) :

C-x C-k C-f % c <return> <escape> 9 7 <f3> ( g l o b a l - s e t - k e y SPC ( k b d SPC " < m e n u > SPC g SPC <escape> - 3 2 <f3> " ) SPC " <escape> 3 3 <f3> " ) <return> <f4> <escape> 2 5 <f4></f4></escape></f4></return></f3></escape></f3></escape></f3></escape></return>

Emacs key macro is hard to read. I don't quite understand how the letters are generated.

Solution by Emacs Rectangle Commands, and Others

jcs posted the question, and gave a elisp solution with explanation. But his readers, also gave several solutions. See: [A Xah Lee Challenge By Jon Snader. At http://irreal.org/blog/?p=1642 , accessed on 2013-01-24 ]

One particular interesting solution is by rectangle, using rectangle-number-lines by Phil. That is the a solution i was thinking but wasn't familiar using arguments to the command. I think it's the best solution. (rectangle-number-lines is new in Emacs 24 (Released 2012-06) .)

Emacs Lisp: Insert Letters A to Z

Here's a command to do this once for all. See: Emacs Lisp: Insert Letters A to Z.