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

More Bad News For The Publishing Industry

I’ve written before on The Future Of Books and how the publishing industry is in peril. The latest move from Amazon is apparently terrorizing the publishers and with good reason. According to articles in the New York Times and TechFlash, Amazon is recruiting authors—including established authors—directly and cutting out the traditional publishing houses. In some cases even agents are getting cut out of the loop. Talk about disintermediation.

Amazon appears to be serious about this. They’ve hired former agent and publisher Laurence J. Kirshbaum to head their publishing division. The Times reports that they paid Penny Marshall $800,000 for a memoir, which they will publish both as an ebook and a traditional dead tree book. If you Google amazon imprints you’ll see that Amazon is regularly adding imprints.

It’s often been said that the only necessary players in the publishing game are the writers and the readers. With this move, Amazon has reduced the chain to only 3 links. Cory Doctorow has already demonstrated that it’s possible to reduce that chain to just the two necessary links but most authors won’t have the skills or desire to do everything themselves and even Doctorow uses Amazon’s print-on-demand service to help with distribution. If Amazon offers authors more than the typical 15% of sale price, they could cut into the publisher’s business in a serious way.

What can the traditional publishers do? I don’t know but they are certainly going to have to change their business models and their relationships with their authors. They could—finally—learn from the music industry and get rid of DRM on ebooks. They could stop pretending that printing and shipping is essentially free and sell ebooks directly to consumers at a decent price. They could cut deals with Apple and other Amazon competitors to offer their entire catalogs in ebook format. Most of all, they must come to grips with the reality that printing press is rapidly becoming a thing of the past. The threat to their traditional business model is no longer theoretical; Amazon is storming the ramparts in a very real way.

Posted in General | 2 Comments

Making The Emacs Minibuffer Recursive

Mickey, over at Mastering Emacs, has a great post on Executing Shell Commands in Emacs and does his usual excellent job of explaining the fine points of his subject matter. I’ve written about some of this material here, but there’s lot of stuff I haven’t covered so you should definitely go read the post.

One of the things that was new to me was recursive minibuffers. What that means is that you can interrupt what you’re doing in the minibuffer to run some other command and then use it’s output in the original command. The need to do this doesn’t come up often—at least for me—but when it does it’s frustrating not to be able to do it. By default, the recursion is turned off but all you need do to enable it is to add

(setq enable-recursive-minibuffers t)

to your .emacs or init.el file.

Here’s a (slightly contrived) example of how you might use the capability. Suppose you’re one of those annoying bloggers who, unlike me, has a backlog of posts ready to publish and that you keep their file names in a file called scheduled. You want to load the top file into Emacs so that you can publish it with org2blog. You type 【Ctrl+x Ctrl+f】 to call find-file but realize you don’t know the first file on the list. You can load its name into the minibuffer for find-file (or ido-find-file) by typing 【Ctrl+u Meta+!】 and then head -1 scheduled. As Mickey explains, the output of most shell commands will have a newline at the end so you’ll need to delete it before pressing 【Return】.

Mickey gives another example of its use but I don’t want to step on his post by reiterating it here so, again, you should go read his post. If you have an interesting use case for this capability, leave a comment.

Posted in General | Tagged | 2 Comments

A Common Lisp Quick Reference

Bert Burgemeister has a very nice Common Lisp Quick Reference available for download. The idea is that you print it out, fold the pages, and staple them to form a little booklet. He’s got copies for A4 and letter size paper and also files for nesting or stacking the pages. There’s also a version for reading on screen that has the pages in the natural order.

This is an excellent little reference containing short descriptions of the symbols—functions, special operators, macros, variables, and constants—defined in the ANSI Common Lisp standard. If you use CL, you should give it a look. I am going to include at least the on-screen version in my documentation set and, despite my aversion to data on dead trees I might also print a copy.

Posted in Programming | Tagged | 1 Comment

Multiplication Table In Emacs Revisited

Yesterday, I wrote about generating a 10 × 10 multiplication table in Emacs Lisp. The solution I gave involved using the dolist macro with a list of the numbers 1–10 like this

(dolist (r '(1 2 3 4 5 6 7 8 9 10)) ...)

That’s a little contrived and obviously doesn’t scale well but I couldn’t find a better way. Fortunately jpkotta knew just what to do. He suggested using number-sequence so that the solution becomes

(dolist (r (number-sequence 1 10))
  (dolist (c (number-sequence 1 10))
    (insert (format "%2d " (* r c))))
  (insert "\n"))

As I said in the previous post, none of this is very important so it wouldn’t be worth the followup post except that number-sequence is essentially like the Python range function (number-sequence includes its upper bound while range doesn’t) so together dolist and number-sequence provide a nice Elisp idiom for operating on a sequence of numbers. Thus a construct such as

for x in range(i,j):
    do_something(x)

in Python could be rendered as

(dolist (x (number-sequence i j))
  (do-something x))

in Emacs Lisp. That’s a handy thing to know and I thought it was worth a followup so that jpkotta’s wisdom didn’t get lost in the comments.

Posted in Programming | Tagged , | 2 Comments

Multiplication Table In Emacs Lisp

Xah Lee saw my post about Tim Visher’s video on building a multiplication table in Emacs and decided to experiment. Visher’s video was in the context of VimGolf where the goal was to build the table in the minimum number of keystrokes. Lee wasn’t interested in building the table in the minimum number of keystrokes; he wanted to see how you could do it in various programming languages. He gives solutions in Emacs Lisp, Python, and Perl. That was interesting but then he says

Lisp is a bit verbose for this kinda thing due to the nested syntax.
But you can write it in perl, python, ruby, bash, or any dynamic lang
quickly, and press a button to have emacs call it and insert the
result.

To a Lisper like me this is a challenge that can’t go unanswered. Therefore I started to think about how I could code this concisely with Emacs Lisp. My first thought was to use the dotimes macro but that starts with 0, which complicates things. Then I realized that since we only needed to deal with the numbers 1–10, I could just use dolist like this

(dolist (r '(1 2 3 4 5 6 7 8 9 10))
  (dolist (c '(1 2 3 4 5 6 7 8 9 10))
    (insert (format "%2d " (* r c))))
  (insert "\n"))

Now put the point at the beginning of the next line, type 【Ctrl+x Ctrl+e】 and the table is inserted into the buffer.

None of this is of any importance, of course, but it is fun and instructive. My solution can be criticized on the grounds that it’s really a Common Lisp solution but it does work in Emacs due to the cl.el package. It’s also slightly objectionable to have to list the numbers explicitly but I couldn’t think of a better way. Leave a comment if you have a nicer solution.

Update: Added link to Xah Lee’s post.

Posted in Programming | Tagged , | 10 Comments

The Emacs Help Function

Most Emacs users are familiar with the “common” help functions

Key Function
f Describe function
v Describe variable
k Describe key sequence
a Apropos
i Info

and perhaps a few others such as 【Ctrl+a】 to display the Emacs start page.

It turns out, though, that there are 37 help commands. Some of these, like 【Ctrl+a】, you’ll rarely use and others, like 【Ctrl+m】 (How to order printed manuals) and 【Ctrl+c】 (Display GPL), you’ll probably never use. That still leaves many useful help commands that you may not know about.

You can get a list of the all the options with 【Ctrl+h ?】 but here are some of the more useful ones

key Function
b Display all key bindings
c】 SEQ Show the name of the command that SEQ runs
d】 TOPIC Display commands & variables whose documentation matches TOPIC
e Display the *Messages* buffer
l Display your last 300 keystrokes
m Display documentation of current major mode
p Find packages by topic keyword
r Display Emacs manual in Info
s Display current syntax table
w】 CMD Show which keys run command CMD
C Describe current coding system
F】 CMD Goto Info node documenting CMD
I Describe input method
K】 SEQ Goto Info node documenting command run by SEQ
L Describe language environment

As I said, there are others that may be just what you’re looking for so be sure to check the documentation.

Posted in General | Tagged | 2 Comments