A Solution to the VimGolf in Emacs Riff

Recall that the last VimGolf in Emacs challenge was to convert

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

to

a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z

I found this harder than I expected. It’s pretty easy to put a space after each character but then you have to deal with the spaces after the z in each line. The previous challenge was easier because all we had to do was find the blanks and replace each of them with four blanks. In this case we want to insert a blank after each character unless it’ the last character in the line.

Here’s my solution:

Ctrl+Meta+% Invoke query-replace-regexp
. \ B Return Match every character except the last in the line
\ & Space Return Replace with the match followed by a space
! Run for the entire buffer

That’s a total of 10 keystrokes. Not too bad, but Aaron Culich managed to shave off 3 (!) more keystrokes by realizing that you don’t have to match the letters, just the empty spaces between them. Thus the regexp for the FROM string is just \B, which means that the TO string is just SPACE for a total of 7 keystrokes. If you can do better, be sure to leave a comment. If you’re a Vim expert, how many keystrokes does it take you?

Posted in General | Tagged | 2 Comments

Copyright Redux

Another post on copyright. I’m sure that most of you are tired of it by now. I sure am. But copyright is out of control and the copyright industry is an insatiable and rapacious beast that will happily gobble up our freedoms and destroy the Internet if we let it.

This week I found two excellent articles addressing these problems. One, The Surprising History of Copyright and The Promise of a Post-Copyright World, explains how copyrights aren’t and never were meant to benefit authors, musicians, or other content producers. They were developed explicitly to protect the distributors of artistic content. That is, the publishers and record companies. The article is long and detailed but is mandatory reading. The TL;DR is that copyright began as a means of censoring authors in sixteenth-century England and then as a way of continuing the power and profit of the London Company of Stationers to whom the task of censorship had been outsourced. The article makes the case that the largest fear of the copyright industry is that people will discover this truth. Really, you must read this article.

The second article, The Declared Value System: Managing Monopolies for the Public Good, was written by Karl Fogel over at Falkvinge & Co. on INFOPOLICY. Fogel proposes a fix for the copyright mess. The idea is simple. If the creator does not register a copyright, the work simply enters the public domain. The copyright owner can choose instead to declare a value for the work and register it for a one year copyright for a fixed percentage, 1% say, of the declared value. The copyright could be renewed a limited number of times. At any time a third party can pay the owner the declared value and at that point the work enters the public domain. Read the article for a thorough explanation of the plan and how it balances the rights of creators and the public.

Fogel notes that exactly the same plan would also work to clean up the patent mess which, if anything, is in even worse shape than copyright. The copyright industry, of course, will not like this idea; it’s not in accord with their business plan. And they sure don’t want people going around talking about the dirty little secret of copyright’s history and real purpose. That’s why it’s important that you spread the word.

Posted in General | Leave a comment

A Riff on the Last VimGolf in Emacs Challenge

Here’s a small variation on the last VimGolf in Emacs challenge. Given

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

transform it into the starting buffer from the last challenge

a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z

Note that there are no spaces after the zs. I’ll post my solution in the next day or two.

Posted in General | Tagged | 3 Comments

A Nice Folding Trick for Emacs

jao over at Minor Emacs Wizardry has a nice post on folding in Emacs. As he points out, there are several packages to do folding but there is also a built-in method that may handle many of the cases where you want folding. The command set-selective-display will cause any lines with an indentation greater than or equal to its argument to be hidden. This is useful (depending on your coding style) when you want to see only function headers and global data declarations. You need only call set-selective-display with an argument of 1.

The command is bound to 【Ctrl+x $】 so you can hide everything but your top level definitions by typing 【Ctrl+1 Ctrl+x $】. To turn off the folding, just call 【Ctrl+x $】.

You may find the key sequence a little clumsy, so jao offers a couple of short cuts. It’s a nice post so you should take a look if you have an interest in this sort of thing.

Posted in General | Tagged | Leave a comment

Attention Troff Users

If, like me, you continue to be a Troff holdout, there’s good news from O’Reilly: They are now offering a free download of the original Unix Text Processing. The book has long been out of print and, I believe, the source code was lost. Now, however, a PDF file has been created, apparently with OCR, and the authors, Dale Dougherty and Tim O’Reilly, have generously made it available for free download.

Previously, the authors had found some PBM files for the book and released those. That gave rise to a project on the part of some of the Groff mailing list denizens to recreate the book from scratch. You can even get the source code for that effort, if you like.

This book is a real treasure and one that every Troff user wanted to own. I hunted for years before I found a used copy. Now you can just download the PDF.

Posted in General | 1 Comment

A Nice Emacs Trick

In the comments to my Another VimGolf in Emacs Challenge post, Aaron Culich points out something that I didn’t know. It is possible to enter query-replace or query-replace-regexp directly from isearch. When you do this, query-replace and query-replace-regexp will use the search string for the FROM string and only ask you for the TO string. You invoke the query-replace functions from isearch by typing 【Meta+%】 or 【Ctrl+Meta+%】 before quitting isearch.

Why would you want to do this? Here’s an example. Suppose you have code like

(defun anothercase ()
  ...)
...
(anothercase)
...
(anothercase)
...
etc

and that your cursor is at the defun. You decide you want to change the name of the anothercase function to make-new-instance. If you type 【Ctrl+s a Ctrl+w】 the search string will be anothercase and you can type 【Meta+%】 to start query replace with the FROM string already set.

A final note. You don’t have to do anything to enable this functionality. In particular, it is not controlled by the query-replace-interactive variable. That, if non-nil, always uses the last isearch string as the FROM string for the query-replace functions, which is probably not what you want.

Posted in General | Tagged | Leave a comment

Wonderful

This is just wonderful. Reuters is reporting that for the first time NPEs (more commonly known as Patent Trolls) are bringing the majority of U.S. patent lawsuits. Can someone please explain that part about the public interest to me again? Can we have a serious discussion on patent reform now? If not, what will it take?

Posted in General | Leave a comment

Solution To Yesterday’s Challenge

Yesterday’s post asked you to turn 26 lines of

a b c d e f g h i j k l m n o p q r s t u v w x y z

into 26 lines of

a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z

by replacing the blanks with a series of 4 blanks.

The best Vim solution (13 keystrokes ignoring the file saving) on the VimGolf site uses a substitute operation to replace a single blank with 4 blanks. If we use the same strategy in Emacs we get

Meta+%
Return
Return
!

for 9 keystrokes. That’s good but if we replace 【 】 with 【Ctrl+4 】 we get it down to 7.

Update: 【Ctrl】 → 【Meta

Posted in General | Tagged | 5 Comments

Another VimGolf in Emacs Challenge

Here’s another VimGolf in Emacs from a recent challenge on the VimGolf site. Your task is to replace the blanks between the letters with 4 blanks and to do it for each line. Here’s the starting file

a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z
a b c d e f g h i j k l m n o p q r s t u v w x y z

and the ending file

a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z
a    b    c    d    e    f    g    h    i    j    k    l    m    n    o    p    q    r    s    t    u    v    w    x    y    z

Disregarding the keystrokes necessary to save the buffer to a file, the best Vim solution is 13 keystrokes. I can do it in 7 with Emacs. Can you beat that? I’ll post my solution in the next day or two.

Posted in General | Tagged | 5 Comments

Exploratory Data Analysis with Unix

Seth Brown over at Dr. Bunsen has a great post on how the ordinary Unix coreutils can be used for exploratory data analysis. His first example

(head -5; tail -5) < data

to show the first and last 5 lines of a file is something that we all understand immediately but might not think of doing ourselves.

He moves on to showing how to change the shape of data using utilities like paste, and to enumerate data with wc and simple awk scripts. Then he has some examples that show how to massage data into useful forms for input to statistical analysis and plotting programs.

There are a lot of Unix utilities that many of us have forgotten or have only dim remembrances of. That’s too bad because as Brown demonstrates, they can be extraordinarily useful. As one of the commenters remarked, “…far more people need to get back to basics and learn coreutils.” I couldn’t agree more.

Posted in General | Tagged | Leave a comment