Emacs Edit Exercise: puthash Unicode

By Xah Lee. Date:

here's fun “vim golf” problem. This is a practical problem. It will test your emacs skill.

here's the input text:

(puthash "circle" "●" xmsi-abrvs)
(puthash "circle2" "○" xmsi-abrvs)
(puthash "triangle" "▲" xmsi-abrvs)
(puthash "triangle2" "△" xmsi-abrvs)
(puthash "diamond" "◆" xmsi-abrvs)
(puthash "diamond2" "◇" xmsi-abrvs)
(puthash "star" "★" xmsi-abrvs)
(puthash "star2" "☆" xmsi-abrvs)
(puthash "spade" "♠" xmsi-abrvs)
(puthash "spade2" "♤" xmsi-abrvs)
(puthash "club" "♣" xmsi-abrvs)
(puthash "club2" "♧" xmsi-abrvs)
(puthash "heart" "♥" xmsi-abrvs)
(puthash "heart2" "♡" xmsi-abrvs)
(puthash "diam" "♦" xmsi-abrvs)
(puthash "diam2" "♢" xmsi-abrvs)

here's the output text:

(puthash "circle" "●" xmsi-abrvs)
(puthash "triangle" "▲" xmsi-abrvs)
(puthash "diamond" "◆" xmsi-abrvs)
(puthash "star" "★" xmsi-abrvs)
(puthash "spade" "♠" xmsi-abrvs)
(puthash "club" "♣" xmsi-abrvs)
(puthash "heart" "♥" xmsi-abrvs)
(puthash "diam" "♦" xmsi-abrvs)

(puthash "●" "○" xmsi-abrvs)
(puthash "○" "●" xmsi-abrvs)
(puthash "▲" "△" xmsi-abrvs)
(puthash "△" "▲" xmsi-abrvs)
(puthash "◆" "◇" xmsi-abrvs)
(puthash "◇" "◆" xmsi-abrvs)
(puthash "★" "☆" xmsi-abrvs)
(puthash "☆" "★" xmsi-abrvs)
(puthash "♠" "♤" xmsi-abrvs)
(puthash "♤" "♠" xmsi-abrvs)
(puthash "♣" "♧" xmsi-abrvs)
(puthash "♧" "♣" xmsi-abrvs)
(puthash "♥" "♡" xmsi-abrvs)
(puthash "♡" "♥" xmsi-abrvs)
(puthash "♦" "♢" xmsi-abrvs)
(puthash "♢" "♦" xmsi-abrvs)

Order of the lines doesn't matter.

How do you do it?

(posted on g+)

Solution by query-replace-regexp

Jorge A. Alfaro Murillo gave this regex replace solution.

C-M-% 
(puthash \("\w+"\) \("."\) xmsi-abrvs) 
C-q C-j 
(puthash \("\w+"\) \("."\) xmsi-abrvs)
C-m 
(puthash \1 \2 xmsi-abrvs) 
C-q C-j
(puthash \2 \4 xmsi-abrvs) 
C-q C-j
(puthash \4 \2 xmsi-abrvs) 
C-m !

Explanation: you Alt+x query-replace-regexpCtrl+Alt+%】, then give the expression:

(puthash \("\w+"\) \("."\) xmsi-abrvs)
(puthash \("\w+"\) \("."\) xmsi-abrvs)

The 【Ctrl+q Ctrl+j】 is inserting a newline char.

Then, give this replacement expression:

(puthash \1 \2 xmsi-abrvs)
(puthash \2 \4 xmsi-abrvs)
(puthash \4 \2 xmsi-abrvs)

The 【Ctrl+m】 is the same as Enter. [see Emacs: Newline Representations ^M ^J ^L] The ! at the end means replace all.

Solution by Key Macro

Peter Meilstrup gave a macro solution:

M-< C-x ( C-s 2 <return> M-<backspace> C-M-b C-M-b C-M-k C-y C-M-f C-y C-a C-k C-y C-a C-M-d C-M-f C-M-f C-M-t C-e <return> C-y C-x ) C-8 C-x e「•••」

[see Emacs: Keyboard Macro]

Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs