Moving Between Emacs Windows

As I mentioned in my winner-mode post, I recently configured Emacs to start with a frame-width of 162 columns. I’ve also added a call to split-window-right in my init.el to get the two columns of windows that I prefer. This introduces the problem of how to navigate between windows easily. If there’s only two windows, the usual 【Ctrl+x o】 works nicely but it quickly becomes cumbersome when there are 3 or more windows in the selected frame.

It turns out that there is a really nice solution to this although it’s a bit hard to find. If you add

(windmove-default-keybindings)

to your .emacs or init.el file, you can move between windows with the 【Shift+→】/ 【Shift+←】/ 【Shift+↓】 / 【Shift+↑】 key sequences. You can change the modifier by specifying a different one as the argument to windmove-default-keybindings.

If, like me, you’re a heavy org-mode user, you’ll find that these key bindings won’t work in org-mode buffers because org-mode takes them over. Happily you can solve this by adding the line

(setq org-replace-disputed-keys t)

to your .emacs or init.el. This will cause org-mode to replace the shift-arrow keys with others for its operations. You can also tell org-mode to replace the windmove keys only when the point is not at a place when org-mode would want to use them as explained in the Org Mode Manual.

All this may not sound like much but they are a huge win for me. I can have several windows open in a frame and still move among them easily. Another demonstration of the power and configurability of Emacs.

Update: windmove-default-bindings → windmove-default-keybindings

Posted in General | Tagged | 11 Comments

CUA Selection Mode

Many Emacs users like CUA mode. That makes a certain amount of sense because then copy/cut/paste work in Emacs the same way it does in almost every other application. I, however, am in the camp of the traditionalists and prefer the default Emacs way. I never get confused when I’m cutting and pasting in other applications and I spend most of my time in Emacs anyway. Not everyone agrees. Xah Lee, for example, is adamant about modernizing the Emacs interface and he always mentions turning on CUA mode first when he writes about it.

That said, one thing I do like about CUA mode is its handling of rectangles. It’s far superior to the default behavior. It’s not better enough to make me turn on CUA mode but it turns out that you can get the rectangle features without the 【Ctrl+x】/ 【Ctrl+c】/ 【Ctrl+v】/ 【Ctrl+z】 bindings. To do that just add

(cua-selection-mode 1)

to your .emacs or init.el file. Nirvana: superior rectangle handling but with the standard kill/copy/yank/undo interface.

Once you’ve enabled the mode, you set a rectangle by typing【Ctrl+Return】 and using the normal【Ctrl】 movement keys. Most of the【Meta】 movement keys are remapped to perform the various rectangle functions (you can see what they are with 【Ctrl+?】). You can cancel the rectangle with another【Ctrl+Return】. One of the nice things about this interface is that you see the actual rectangle instead of a virtual rectangle like you do with the standard interface.

You can kill and yank the rectangle with 【Ctrl+w】 and 【Ctrl+y】 as usual. In general, this interface is easier to use and requires fewer keystrokes to perform a given task. As the documentation says, the only new keystroke you need to learn is 【Ctrl+Return】.

Posted in General | Tagged | 1 Comment

Doing the Right Thing

After a year in which we’ve seen Big Media ride roughshod over their customers and do everything they could to rein in, if not destroy, the Internet, I have a ray of sunshine for you. Hollywood and the rest of big media is busy trying to strong arm Australia’s ISPs to spy on their customers so they can identify “pirates.” One ISP, iiNet, has had enough and told the Hollywood thugs to sod off.

TorrentFreak is reporting that iiNet has walked out of talks with Hollywood aimed at reaching an agreement over monitoring users for infringing activity. They make some good points

  • iiNet is not the Internet Police. It’s not appropriate for them to monitor their customers.
  • Hollywood wants the ISPs to collect and store data for Hollywood’s use but at their own expense. iiNet says this is not their responsibility.
  • ISPs have no legal or moral obligation to monitor their customers
  • (most importantly) Piracy is a problem of Hollywood’s own making. They refuse to make their content available in a timely manner at a reasonable cost to Australian citizens.

iiNets’s Chief Regulatory Officer Steve Dalby says that iiNet is happy to work with the rights holders but that they have to fix their broken business models first.

It’s great to see an ISP stand up for their customers and refuse to become spies for Hollywood. If only other ISPs would do the same.

Posted in General | Leave a comment

Winner Mode

In my Two From Phil post I mentioned that Phil had urged me to try winner-mode and that I was going to give it a try. For those who came in late, winner-mode captures the current window configuration and allows you to restore it after it gets changed by some Emacs action.

I was a little skeptical but I enabled winner-mode and 10 minutes later I was hopelessly addicted. This all comes at a good time because I have lately been setting Emacs to be 162 characters wide so that I can have two columns of “standard sized” windows1. That set up makes it really easy to mess up your window configuration. For example, if you bring up a help screen, the focus remains in the original window as usual but now it’s hard to get rid of the *Help* window. If you type 【Ctrl+x 1】 you blow away all your other windows. You can switch into the *Help* window and type 【q】 but that’s a pain. With winner-mode you simply restore the previous window configuration and you’re back to where you were before invoking help.

If you aren’t currently using winner-mode, do yourself a favor and give it a spin. It’s built-in functionality so all you have to do to try it is type 【Meta+x】 winner-mode and you’re all set. Now 【Ctrl+c ←】 will restore the previous window configuration and 【Ctrl+c →】 will redo the configuration you just destroyed.

Try it like this for a bit and see if you aren’t addicted too. If you are, just add

(winner-mode 1)

to your .emacs or init.el file and it will always be their for you. If you don’t like it, just turn it off with 【Meta+-1 Meta+x】 winner-mode. I had intended to try it for a week or so to see how I liked it but, as I say, I enabled it permanently almost immediately. Really, you should try it out.

Footnotes:

1 I know, I know but I’m an old-timey guy and can’t get over the notion that the proper size for a (source code) screen is 80 characters. It makes no sense, I admit, but there it is.

Posted in General | Tagged | 1 Comment

Symbols in Emacs Lisp

One of the hardest things for Lisp newcomers to understand is symbols. They look tantalizingly like variables from other languages and, indeed, they are often just that. But, as the new Lisper soon discovers, they are much more.

Xah Lee just published a very helpful introduction to symbols in Emacs Lisp. His tutorial covers what symbols are and how they work in the context of Emacs Lisp. He also covers some of the functions that work with them and let you access the objects they contain. There is a companion page in which Lee shows you how to check if a symbol is bound to objects of various types.

If you’re trying to learn Emacs Lisp, these pages will help clear up some of the things that I found most confusing when I started. These pages are a welcome addition to Lee’s Emacs Lisp Tutorial series.

Posted in Programming | Tagged , | 1 Comment

C++

The funniest—or is it saddest—thing I’ve read about C++ in a long time.

Posted in Programming | Tagged | Leave a comment

Two From Phil

Regular reader and commenter Phil has made a couple of good observations in the comments of recent posts that I think are worth bringing to everyone’s attention. Since not everyone follows the comments, I decided to mention them in a separate post.

In the Naming Registers post, Phil mentions winner-mode and says that you’re really missing out if you don’t use it. When I replied that I had never understood the point of winner-mode, he explained further why it’s so useful. The basic idea is that it’s like Magnar Sveen’s Magit hack to restore your window configuration after quitting Magit but it works anyplace. I’m going to give it a try and will probably write up my impressions later.

In response to my More isearch Goodness post, Phil mentions that you can get extensive help—including all the available bindings—by typing 【Ctrl+h Ctrl+h】 while in isearch. That’s handy if you forget a key sequence or just to see what’s available.

Thanks to Phil for a couple of handy tips.

Posted in General | Tagged | 1 Comment

Publishing Code in a Blog Post

As regular Irreal readers know, I prepare all my blog posts with org2blog. One of the nice features of that is that any code I include in a post is automatically fontified in a language appropriate way. Recently, both Xah Lee and Magnar Sveen have published short pieces on how they include source code in their blog posts. They both use a strategy of calling htmlize to do the heavy lifting of fontifying the code.

Sveen’s method is specialized for his What the .emacs.d!? blog. He calls htmlize-region to mark up the selected code with appropriate <span> tags for use with CSS. The result is inserted into a template for the blog post and written to a buffer associated with a file for that post. It’s simple, direct, and purpose-built for writing What the .emacs.d!? blog posts. He can mark some code from his configuration files, call what-the-emacsd-post to htmlize it and insert the result into a post template ready to be completed and published.

Lee’s solution is more comprehensive. He writes about many languages so his strategy is different. With his code you put the cursor between the <pre> tags for code you want to include and call htmlize-pre-block. The language of the source code is extracted from the <pre> tag and then the code in the pre block is inserted into a temporary buffer that is set to the appropriate mode according to the language extracted from the <pre> tag. Next htmlize-buffer is called to mark up the code and the result replaces the original code between the <pre> tags.

This is a nice solution because it can handle a large range of languages and is reasonably flexible. Notice that Lee’s method assumes you already have the code in a pre block and that you just want to fontify it. In a sense, it’s the reverse of what Sveen does: extract the code from a source file and insert it into a new HTML file.

Both of these solutions are useful. Which one you should use depends on your work flow and whether you are dealing with one language or many. For my part, I have always written my posts in Org mode. Before I started using WordPress and org2blog, I wrote my posts in Org mode and called

(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 turn the whole buffer into HTML. Then I pasted that into the Blogger control panel to publish the post. A little clunky but much better than typing in posts to the control panel.

I like to see how other bloggers handle the writing and publishing of their posts so I always enjoy posts like Lee’s and Sveen’s. Besides, it’s always nice to see Emacs in action.

Posted in General | Tagged | Leave a comment

More isearch Goodness

In A Nice Emacs Trick, I wrote about how you can enter query-replace or query-replace-regexp directly from isearch and use the isearch search string as the FROM parameter to query-replace or query-replace-regexp. It turns out that there’s a lot more isearch goodness available.

Steve Purcell, who I first talked about in Zap Up To A Character, has a nice collection of isearch enhancements. One of these merely drew my attention to functionality that’s already there but hardly anyone knows about: you can call occur directly from isearch using the search string for the input to occur by calling isearch-occur. That’s bound to 【Meta+s o】 by default but Purcell binds it to the easier 【Ctrl+o】:

(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)

Another common use case is wanting to end the isearch on the other side of the match. For example, if you are searching forward but want to put point before the match instead of after it you have to do something special to get it there. With a single word for the match,【Meta+b】 does the trick but more complicated matches (multiple words, say) aren’t as easy. Purcell provides isearch-exit-other-end to do this and binds it to the very sensible【Ctrl+Return】:

(define-key isearch-mode-map [(control return)] 'isearch-exit-other-end)

I’ve already added both of these to my init.el.

There are a couple of other enhancements in Purcell’s init-isearch.el file so be sure to head over and take a look. Perhaps you’ll want to add some of the others.

Posted in General | Tagged | 2 Comments

Lispdoc

The other day I wrote about discovering Steve Purcell’s emacs.d repository on GitHub. Today, I want to talk about another wonderful thing I discovered there: Lispdoc. Bill Moorier’s Lispdoc is a documentation search engine for Common Lisp that searches

  • The Hyperspec
  • CLtL2
  • Practical Common Lisp
  • Successful Lisp
  • On Lisp
  • Paradigm of Artificial Intelligence Programming
  • ANSI Common Lisp
  • The docstrings from SBCL
  • A bunch of ASDF libraries

This is a really great documentation resource. If you’re a Lisper, go on over there right now and type in a CL symbol and see what happens. Amazing.

Purcell uses a bit of Elisp, which he got from Bill Clementson, to automatically look up a symbol from Emacs. By default, it will look up the symbol at point, but you can enter any symbol you like. Very nice. Sadly, Clementson isn’t blogging anymore but his site also has a bunch of Lisp related material.

Update: a → and

Posted in Programming | Tagged , , | Leave a comment