Swapping with replace-regexp

Most Irreal readers have probably already seen Mickey’s post on Evaluating Lisp Forms in Regular Expressions, but he mentions one trick that is too good to miss so I’m going to repeat it.

Not too long ago I was trying to find a way to swap several occurrences of two words in a buffer (I think it was for the VimGolf lamb had a little Mary challenge). I did find a way but I wasn’t very happy with it. In his post, Mickey shows a way that is, in retrospect, obvious but which didn’t occur to me. Suppose you have

Jill and Jack went up the hill to fetch a pail of water.
Jill fell down and broke his crown and Jack came tumbling after.

and you want to switch Jill and Jack so that you have the traditional rhyme. Here’s Mickey’s trick: do a replace-regexp (or query-replace-regexp) with a FROM string of

\(Jill\)\|Jack

and a TO string of

\,(if \1 "Jack" "Jill")

Notice that we capture only one subgroup and use it to determine which replacement word to use. If \1 is nil then we didn’t match Jill so we must have matched Jack and we want to use Jill as the replacement. On the other hand, if \1 is not nil then we matched Jill and want to replace it with Jack. Very nice.

Mickey’s got some other nice tricks you can do with the form-based TO string in replace-regexp so be sure to read his post if you haven’t already.

Posted in General | Tagged | 2 Comments

A Word Frequency VimGolf in Emacs Challenge

Here’s an interesting VimGolf challenge that nicely demonstrates the power of CUA selection mode. Given the word frequency chart

     align here
the        56271872
of        33950064
and        29944184
to        25956096
in        17420636
I        11764797
that        11073318
was        10078245
his        8799755
he        8397205
it        8058110

align the frequency counts so that it looks like

     align here
the  56271872
of   33950064
and  29944184
to   25956096
in   17420636
I    11764797
that 11073318
was  10078245
his  8799755
he   8397205
it   8058110

My first thought was to use align-regexp but that command doesn’t have a shortcut key sequence and the best regular expression I could find was [1-9]. A pretty nice solution but it certainly won’t win on keystrokes.

After messing around a bit, I came up with a solution of 4 keystrokes using the CUA rectangle functionality.

Meta+m Move to the a in align
Ctrl+Return Invoke rectangle mode
Meta+} Move to end of buffer
Meta+a Align the rectangle

Notice that the trailing digits of a few of the frequency counts aren’t in the rectangle but they get dragged along anyway. I’ve only been using CUA rectangle mode for a short while but I am continually amazed at how powerful it is. Certainly this problem doesn’t look like a rectangle problem but CUA rectangle mode solves it very handily anyway.

Sadly, those darn Vim people were able to solve this in 3 keystrokes so if you can shave off a stroke or two be sure to leave a comment. I don’t think you can squeeze any more keystrokes out of the rectangle strategy so you’ll probably need something entirely different.

Posted in General | Tagged | 3 Comments

Living the Lisp Machine Environment

A little while ago I wrote about Martin Fowler’s post on InternalReprogrammability that celebrated Emacs reprogrammability and its recapitulation of the Lisp Machine environment. That capability is of more than theoretical importance as I, once again, discovered.

Long time readers know that the fancy 【Key】display on this blog is produced by a combination of CSS for the <kbd> tag and some Elisp that converts text such as ctrl+x b into 【@‍<kbd>Ctrl@‍</kbd>+@‍<kbd>x@‍</kbd> @‍<kbd>b@‍</kbd>】 (the @ in front of the tags is an Org mode thing that tells Org mode to export the tag that follows as raw HTML) which gets displayed as【Ctrl+x b】. The Elisp has some regular expression-based combining rules that handle minimally complex expressions such as ctrl+x b or ctrl+x ctrl+s. Recently—probably as a result of all the VimGolf in Emacs posts I’ve been doing—I’ve run into cases that the Elisp didn’t handle correctly. That’s not a big deal because I can just go in and fix the markup by hand. Indeed, I even have a chord, <>, that inserts @‍<> into the buffer so that I can easily insert the @‍<kbd> and @‍</kbd> tags.

The other day I was busy doing a bunch of those fix ups when, just as Fowler, I realized that I’d been dealing with this problem by doing more work than necessary. So I wrote a little Elisp

(defun enkey ()
  "Wrap the next word with (org-mode) <kbd> tags."
  (interactive)
  (insert "@<kbd>")
  (forward-word)
  (insert "@</kbd>"))

to do the wrapping of words (or singleton characters) in the <kbd> tags automatically. Now I just put the point before any word or character that didn’t get handled correctly by the conversion code, call enkey, and move on to the next problem.

It’s easy to forget—even when you write about it occasionally—how simple it is to fix minor editing annoyances with Emacs. A little bit of Elisp and the problem is solved.

Afterward

Another small annoyance that I hadn’t gotten around to dealing with is the inability to join the current line with the next one; you must move down and then combine the two lines with 【Meta+^】. Happily, that was an itch that also bothered Magnar Sveen so he fixed it. It’s another example of how Emacs lets you have it your way.

UpdateMeta+v】 → 【Meta+^

Posted in Programming | Tagged | 3 Comments

A Groups Magic VimGolf in Emacs Challenge

Here’s a simple ViGolf challenge that has a simple solution in Emacs. Given

(a) (abc) (abcd) (123456)

turn it into

___ _____ ______ ________

That is, replace everything in the sexpr with an underscore. This is really easy with Emacs using query-replace-regexp. Here’s a solution in only 8 keystrokes. The best Vim solution is 11, so Emacs definitely wins this one.

Meta+Ctrl+% Invoke query-replace-regexp
\S-Return Replace non-spaces with
_Return Underscores
! For the whole buffer

On the other hand, my experience has been that every time I think I have the best possible solution, one of my way smarter readers shows me a much better way. If you can beat 8 keystrokes, leave a comment and tell us how a real master does it.

Posted in General | Tagged | Leave a comment

Google and Warrants

It’s become fashionable of late to beat up on Google and wonder aloud what happened to their celebrated “Don’t be evil” motto. Google provides a slew of useful services and the price we pay is to have our digital existences vacuumed up and sold to advertisers1.

We here at Irreal delight in being contrarian so I’d like to send a little praise Google’s way. Google has, for some time, published periodic Transparency Reports in which they list government (all governments) requests for user information and how they responded to those requests.

Here in the U.S., we have a constitutionally dubious device known as the ECPA subpoena that is much easier to obtain than the normal search warrant. Google has announced that they will no longer comply with such subpoenas and will, instead, require a warrant. This is a bit of a gutsy move because the ECPA is, after all, the law and by defying the subpoenas, Google is inviting a very one sided battle with the U.S. Department of Justice. In a recent SANS Institute News Bites newsletter2, William Hugh Murray notes

That the government has not hauled Google into court when it pushes
back suggests that, not only does it fear an adverse ruling, it fears
the light of day. Use and abuse of the Internet by government will
clearly get much worse before it gets better. There are now so many
exceptions to the Fourth Amendment that it operates only by accident.

Given what’s at stake and the risks involved I think we all owe Google a word of thanks. So, Google, good on you!

Footnotes:

1 Yes, that’s a little hyperbolic but only a little.

2 SANS NewsBites Vol. 15 Num. 007

Posted in General | Leave a comment

Department of Red Meat

Heh Heh. I know, I know; I’m evil but I couldn’t help myself.

Posted in General | Tagged , , | Leave a comment

Short Stories

This is a collection of short observations that don’t merit a post of their own but that some may nevertheless find interesting.

  • Social Science
    Here, in case you were wondering, is why most rational people consider the term “social science” an oxymoron.
  • Apple Maps
    This past weekend I drove up to Orlando to visit with some family members who were taking in Disney World. There’s no problem finding Disney World, of course, but they were staying at a hotel I had never been to before so I didn’t know how to get there. I used the Maps application on my iPad and was pleasantly surprised at how nice it was. The experience was very similar to dedicated GPS systems that come with some cars. You get the map, of course, but also turn by turn directions and verbal instructions as you approach a waypoint. To be sure, this is in the United States and in a very built-up area but the results, at least for this area, were impressive.
  • Tilde as Home
    Last year, I wrote about why vi uses the hjkl keys to move the cursor. That turned out to be because the ADM-3A terminal had arrows on those keys. Now the Unix & Linux StackExchange tells us why the tilde (~) is often used to represent the user’s home directory on Unix-like systems. It turns out that the answer is the same: The ADM-3A keyboard had tilde and Home on the same key. If you follow the link, you can see the layout of the keyboard as well as a picture of the actual keys. An interesting bit of arcana.
Posted in General | Leave a comment

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.

Posted in Programming | Tagged , | 4 Comments

A Real World VimGolf Challenge

In my CUA Mode Video post, I mentioned that the pointer to the video came from a comment to one of the posts in the Google+ Emacs Community. That post asks for advice on the best way of changing

a[0] = b[0];
a[1] = b[1];
a[2] = b[2];

to

c[0] = d[0];
c[1] = d[1];
c[2] = d[2];

My first thought (and what I’d probably just do if I needed this change) is a query-replace to replace a with c, followed by 【Meta+<】 to return to the top of the buffer and then another query-replace to replace b with d. That’s 13 keystrokes. Not too bad but can we do better?

Here’s a solution in 12 keystrokes using cua-selection-mode.

Ctrl+Return Invoke rectangle mode
Ctrl+2 Ctrl+n Form rectangle of a’s
Meta+f c Fill rectangle with c’s
Ctrl+7 Ctrl+f Move to last b
Ctrl+Return Invoke rectangle mode
Ctrl+2 Ctrl+P Form rectangles of b’s
Meta+f d Fill rectangle with d’s

Using Magnar Sveen’s essential multiple-cursors package I can do it in 9 (8 if I don’t exit multiple-cursors mode) with essentially the same strategy as above but, of course, that’s not using stock Emacs. If you have a clever solution, leave a comment.

Posted in General | Tagged | 6 Comments

CUA Mode Video

Over at the Google+ Emacs Community, Jean-Sébastien Ney (in a comment to this post) gives us a pointer to a very nice video on CUA-mode by Mark Mansour. Actually, the video really deals with the rectangle features of CUA-mode that we discussed recently. If, as I recommended, you put cua-selection-mode in your .emacs or init.el file, you can use all the tricks that Mansour demonstrates without having to invoke CUA-mode as he does.

Mansour shows how rectangle mode can do many of the things that Magnar Sveens wonderful multiple-cursors package does. The video is only 3 and a quarter minutes so it won’t take you long to watch it and it’s well worth the time.

Posted in General | Tagged | Leave a comment