The Emacs sort-columns Command

Xah Lee has caught the VimGolf with Emacs bug and proposed an interesting challenge. The final part of the solution involved sorting the following list by the number of square kilometers.

California             423,970 km²
Taiwan                  36,008 km²
Japan                  377,944 km²
Germany                357,021 km²
Iraq                   438,317 km²
Iran                 1,648,195 km²
Korea (North+South)    219,140 km²
Mexico               1,964,375 km²

Normally this wouldn’t be much of a problem but two things make it more difficult than usual. First, the line for Korea has an extra field so commands like sort-fields or sort-numeric-fields don’t work. Second, the numbers have commas in them so the numeric sorts don’t work.

I remarked on Xah’s blog that I had tried sort-regexp-fields but that that didn’t work because the sort was lexicographic rather than numeric. Then jm commented that the lexicographic sort will work if you include the spaces in front of the shorter numbers. He proposed using sort-regexp-fields with the regular expression .*\([0-9 ,]\{9\}\) km. That did the trick and I thought it was probably as simple a solution as we were apt to find.

Later I suddenly realized that an even simpler solution based on the same idea was to use sort-columns. To use that function, you mark the rectangle containing the columns you want to sort on and then just call sort-columns. Thus, I placed the point two spaces before 423,970 on the first line, typed 【Ctrl+Space】 to set the mark, moved the point to right after 1,964,375 on the last line, and typed 【Meta+xsort-columns to sort the list. That worked perfectly and is a reasonably concise solution.

As usual, Emacs had the right tool to get the job done even if it took a little effort to find it. Maybe next time this solution will jump right into my head. Or not.

Posted in General | Tagged | 5 Comments

Speed Marking In Emacs

Sometimes you want to mark just a word or two so that some command can operate on them. The normal way to do that is with 【Ctrl+Space Meta+fMeta+f】 but there’s a slightly faster way. To mark one word, just type 【Meta+@】. If you want to mark the next N words, type 【Meta+N Meta+@】. Similarly, to mark the previous N words, type 【Meta+ Meta+N Meta+@】.

The documentation for this command describes it as setting the mark ARG words away from the point and mentions that the mark goes in the same place that it would if you were moving by 【Meta+f】 with the same ARG.

Summary

Key Sequence Action
Meta+@ Mark the next word
Meta+N Meta+@ Mark the next N words
Meta+ Meta+N Meta+@ Mark the previous N words
Posted in General | Tagged | 2 Comments

Useful Commands For The Emacs Minibuffer

I have long used 【Meta+p】 to recall previously entered information in the minibuffer. For example, when I’m editing Groff input it’s often convenient to typeset what I have so far to check that the result looks good or just to read what I’ve written without the markup. I do that by selecting the entire buffer with 【Ctrl+x h】, piping the buffer to groff with 【Meta+|】, and then specifying the command

groff -ms -pset -X | gxditview

It’s a pain to keep typing that so after the first time I just type 【Meta+p】 to bring up the pipeline again.

You can, of course, move forward in the minibuffer history with 【Meta+n】 but there are some other not so obvious ways of navigating the minibuffer. The command 【Meta+rregexpReturn】 will search backward in the minibuffer history for the entry matching the regular expression regexp. Similarly, 【Meta+sregexpReturn】 will search forward for the entry matching regexp. You can continue either of these searches to the next match with 【Meta+r Return】 or 【Meta+s Return】.

Finally, you can repeat the last echoed message with 【Ctrl+h e

Summary

Key Sequence Operation
Meta+p Backward one entry
Meta+n Forward one entry
Meta+rregexpReturn Search backwards for regexp
Meta+sregexpReturn Search forward for regexp
Meta+r Return Repeat backward REGEXP search
Meta+s Return Repeat foreard REGEXP search
Ctrl+h e Show last echoed message

Update: 【Meta+s】 → 【Meta+n】 in summary

Posted in General | Tagged | 1 Comment

Administrivia—Moderation

Some of you may have noticed that your comments were held in the moderation queue longer than usual the last two days. That’s because I haven’t been getting email notifications of new comments. I’ll look into what’s happening there but in the mean time this seems like the right moment to institute a change I’ve been thinking about anyway.

Since I implemented the captcha, my forum spam has disappeared so I am no longer going to hold normal messages for moderation. Some messages—those WordPress is suspicious of—get held automatically. If the forum spam becomes a problem again in the future I may have to reconsider but for now your comments should appear when you post them.

Again, I’m sorry about the captcha but over 90% of the comments were spam and I was spending a lot of time dealing with them. If you comment regularly, it may pay to register as then the captcha doesn’t appear.

Posted in Blogging | Tagged | Leave a comment

Today Is Dennis Ritchie Day

Tim O’Reilly has declared today Dennis Ritchie Day and asks us all to remember the contributions of this computing pioneer. For his part, O’Reilly insists that without Ritchie’s work, O’Reilly Media would not exist.

As I wrote in my short post about his death, Ritchie had tremendous impact on my growth and career in computing so I am happy to oblige Mr. O’Reilly and mark the day here at Irreal.

Posted in General | Leave a comment

Mickey Plays VimGolf In Emacs

I’ve written a couple of times about Tim Visher’s VimGolf In Emacs series of videos. If you’re an Emacs user and haven’t taken a look at them yet, you should do so without delay. They are very informative and entertaining.

Now Mickey Petersen over at Mastering Emacs has gotten into the act. That makes me very happy because I really enjoy seeing how other Emacs users solve editing problems. I almost invariably learn something useful by watching or reading those solutions. Mickey’s take on the problems is a little different from Visher’s. He says up front that there’s no way Emacs is going to be as efficient in terms of keystrokes as Vim and instead he concentrates on finding a general—or as he says, technically correct—solution. By that he means a solution that works for bigger or smaller data. The problem with VimGolf solutions is that they tend to be crafted for the particular piece of data that’s being edited. That’s because in order to minimize keystrokes the solution has to be tailored to the specific data in the problem.

The problem that Mickey considers is to sort the following records into alphabetical order by people’s names.

## Directory
   * Smith, George
      - 1234 Avenue, New York, NY
   * Bailey, Stephen
      - 4545 Harbor Ln, Atlanta, GA
      - Phone: (412) 291-1238
   * Thomas, Brent
      - 1482 Mystic Oaks, Anaheim, CA
   * Grant, Jenny
      - 198 Circle Dr, Houston, TX
      - Phone: (213) 198-9842
      - Email: jenny.grant@gmail.com
   * Percy, Adam
      - 221 Jaguar Pkwy, Missoula, MT
   * Garfield, Misty
      - 5988 Apple Tree Ln, Memphis, TN
   * Parsons, Betty
      - Phone: (235) 523-2378
      - Email: bettyboop123@gmail.com
   * Sanders, Terry
      - Email: sanderst@yahoo.com
   * Smith, Pete
      - Phone: (294) 984-2938
   * Frost, Jennifer
      - 2498 Temple Terrace, Miami, FL
   * Matthews, Frank
      - 418 Happy Trail, Phoenix, AZ
      - Phone: (985) 129-2394
      - Email: frank@phoenixrealtors.net
   * Allen, Taylor
      - Email: allen.taylor@hotmail.com
   * McCullen, Sarah
      - 247 Hidden Elm, Seattle, WA
      - Phone: (288) 283-4568
   * Perkins, Mike
      - 992 Peartree Ln, Bowling Green, KY

Think for a second or two how you might do that in Emacs and then jump over to Mickey’s post to see how he leveraged the power of Org-mode to solve the problem. It’s a brilliant solution and with smex installed is probably competitive with the Vim solutions even without trying.

Posted in General | Tagged | 5 Comments

Emacs Registers

Back in September I wrote about using registers to open frequently accessed files. Shortly afterwards, someone recommended just saving my desktop with desktop-save-mode and I haven’t really used registers since. More recently, I started watching Tim Visher’s excellent VimGolf in Emacs videos. Video #4 talks about, among other things, saving window/frame configurations in registers that you can then jump to. That caught my imagination so I looked into the use and abuse of registers some more.

As the name suggests, registers can hold various types of objects. They can hold

  • Positions
  • Text
  • Rectangles
  • Window and Frame configurations
  • Numbers
  • File names

Each register has a single character name by which it can be accessed. Thus a register could be named 1, or a, or A, or Ctrl-a. Only Ctrl-g is off limits for obvious reasons.

Now let’s take a look at how they can be used. First up is saving positions. This can be handy if you want to repeatedly refer to, say, a function in one of the buffers. To save a position in register ρ you would put the point at the position you want to save and type 【Ctrl+x r Space ρ】. To return to that position from the same or another buffer you would type 【Ctrl+x r j ρ】. This works even if the buffer containing the position has been killed providing the buffer was associated with a file.

You can copy or move a region into register ρ with the command 【Ctrl+x r s ρ】 and insert it at point with the command 【Ctrl+x r i ρ】. That’s how I inserted the rho (ρ) into the text—I looked it up once and then copied it into register r so I didn’t have to keeping going through the ucs-insert routine.

If you give the 【Ctrl+x r s ρ】 command a prefix argument, it will delete the original region after copying it into the register.

There are two other less frequently used commands to store text to registers: 【Meta+xappend-to-registerReturnρ and 【Meta+xprepend-to-registerReturnρ. These will append or prepend the region to the text already in register ρ. If you specify a prefix argument, the region will be erased after it is copied into the register.

You can copy a rectangle to register ρ with 【Ctrl+x r r ρ】 and insert it with 【Ctrl+x r i ρ】 as before. Just as with regions, specifying a prefix with the copy command will delete the original rectangle.

If you have a particular window configuration in the current frame your can save that configuration in register ρ with 【Ctrl+x r w ρ】 and you can later restore that configuration by jumping to it with 【Ctrl+x r j ρ】.

If you want to save the configuration of all frames and their windows in register ρ, use 【Ctrl+x r f ρ】.

Registers can also contain numbers. These numbers can be incremented, which make them useful for certain types of keyboard macros. To store N into register ρ, use 【Ctrl+u N Ctrl+x r n ρ】. To increment the number in register ρ by I use 【Ctrl+u I Ctrl+x r + ρ】.

You can insert the number in register ρ at point by using the usual insert command: 【Ctrl+x r i ρ】.

I’ve already dealt with storing file names in registers in the previous post linked above.

Summary

Key Sequence Function
Ctrl+x r Space ρ Copy position to register ρ
Ctrl+x r j ρ Jump to the position in register ρ
Ctrl+x r s ρ Copy/Move region to register ρ
Ctrl+x r i ρ Insert object in register ρ at point
Ctrl+x r r ρ Copy/Move rectangle to register ρ
Ctrl+x r w ρ Copy current frame configuration to register ρ
Ctrl+x r f ρ Copy all frame configurations to register ρ
Ctrl+u N Ctrl+x r n ρ Insert N into register ρ
Ctrl+u I Ctrl+x r + ρ Increment register ρ by I
Posted in General | Tagged | 1 Comment

Blogging With Emacs

Xah Lee asked me to write about my blog publishing process and how an article gets from Emacs to my site. I’ve written about most of the parts of the process in various posts but it might be useful to tie everything together in a single post for the benefit of any Emacs users who want to start a blog.

I write my posts with Org-mode in Emacs. As the Org Manual puts it,

“Org-mode contains an HTML (XHTML 1.0 strict) exporter with extensive
HTML formatting, in ways similar to John Gruber’s markdown language,
but with additional support for tables.”

The Org-mode markup language makes it easy to insert formatting—such as bold, italics, and code—and hyperlinks to other documents. Even someone with no knowledge of HTML could produce very nice Web pages.

During the writing of the post, and afterward as a final check, I can send it to my browser to see what it will look like. That won’t be exactly what you see on Irreal, of course, because I’m really just exporting the post to HTML and displaying in my browser without the benefit of the WordPress style sheet.

As I explain below, it’s possible to write a post, export it to HTML, and then paste it directly into your site’s editing window but I have a (much) better way. Once I’m satisfied with the post I export it directly to my site using org2blog. Making one call to org2blog takes care of everything in a single step:

  • The post is converted to HTML
  • The HTML is uploaded to the WordPress process on my site
  • Any pictures or other images in the post are uploaded to the site and their links are adjusted to point to the site’s machine rather than my workstation
  • The date and Post ID are added to the Emacs buffer containing the post
  • Information about the post is recorded in .org2blog.org so that any updates to the post will result in the original post being updated rather than a new post being added.

By doing all these things, org2blog takes care of the majority of the bookkeeping chores associated with making a post.

When org2blog finishes with the upload, I save the file again (to save the date and Post ID that org2blog added), and call Magit to put it in Git. Then I call dired for my blog directory, type 【* .html to mark any HTML files and then 【D】 to delete the HTML files and any associated buffers. At this point, I’m done. The post has been published, the source for it filed away in Git, and extraneous files and buffers have been deleted.

My old blog, also called Irreal, was hosted on Blogger. Although there is a version of org2blog (by a different author) for Blogger I didn’t use it. Instead, I used this function

(defun blog-post ()
  "Export an org buffer as HTML but only export the body."
  (interactive)
  (org-export-as-html 3 nil nil "*blog-post*" t))

to convert the post to HTML and export the body. Then I would copy the *blog-post* buffer to the clipboard and paste it into Blogger’s HTML editor. That worked fairly painlessly unless there was an image involved. Blogger stores the images in Picasa instead of with the post so doing things by hand can be a bit involved. I wrote about that here. I don’t know if the Blogger version of org2blog handles pictures automatically but if it did, that would be reason enough to use it. If it is anywhere close to as convenient as the WordPress version it would be worth using. If you use org2blog on Blogger, leave a comment telling us about your experiences with it.

Posted in Blogging | Tagged , | 6 Comments

Applying and And or In Emacs

Zach Beane has a nice Common Lisp tip about applying and. The problem that the tip addresses is how to apply and to a list of values to test that they are all true. One might think that you could simply use apply in the normal way

(apply #'and list)

but and is a macro so this doesn’t work. The solution is to use the every function like this

(every #'identity list)

This will return t if every element of list is t. All this is also true of Emacs Lisp so it’s worth mentioning in an Elisp context too.

Although Beane doesn’t mention it, the same problem exists for or. The way to handle or is with the some function

(some #'identity list)

The some function will return the first true element in the list or nil if there is none.

Longtime readers will remember that we’ve talked about some before. Both some and every are a bit more general than I’ve indicated here. Their first argument can be any predicate so that

(some 'pred list)

will return the first element of the list for which pred evaluates to t. Similarly for every. Also note that for Emacs Lisp it suffices to just quote the predicate function rather than using the #' that Common Lisp requires.

Posted in Programming | Tagged , | 3 Comments

John McCarthy

It’s been a sad month for the computer community. First Steve Jobs and Dennis Ritchie and now John McCarthy has died. McCarthy was the creator of Lisp and an important researcher in—some say the father of—artificial intelligence.

Posted in General | Leave a comment