Simple window configuration management

Most of the time, I run Emacs in a single, maximized frame (and, sometimes, just inside a urxvt terminal). Inside that frame i create and destroy windows as needed to contain my buffers [0]. After a while working with Emacs one gets used to a fluid window setup, where windows come and go as need arises.

But I remember that in the early days I felt a bit confused: I started by creating a windows setup (split horizontally, then vertically, open files in each of the windows… that kind of thing) and was non-plussed when any operation (going to a Gnus buffer and reading messages, for instance) destroyed my carefully prepared layout. Over the years i’ve seen many a newbie feeling that way. My advice is usually to let go, and learn how to move around your windows, creating them as needed.

That said, i must admit that, sometimes, i want to recover a certain window configuration in a quick way. A particularly simple one is provided by winner-mode, a package which is part of Emacs. Winner keeps track of your window configurations, and provides functions to navigate your window configuration history.

As advertised, activating winner-mode (which is a global minor mode) is as easy as one can get: just type M-x winner-mode or put this line:

    (winner-mode 1)

in your .emacs. Use Emacs normally, and whenever one cherished window layout gets destroyed, press C-c <left> (where <left> refers to the left cursor key) to recover it–actually, this calls winner-undo, and you can invoke it as many times as you want to visit previous window configurations. As one would expect, C-c <right> (the default binding for winner-redo) navigates configurations in the opposite direction [1]. Simple and very useful, as it should be.

But maybe you want to be automatically tele-transported to a previous configuration without traversing the intermediate ones. No problem: Emacs provides functions to store and retrieve window configurations (which, in fact, are the ones winner-mode is using under the covers). You can store the current window configuration in register ‘a’ (registers are named using single letters or numbers) pressing C-x r w a, and restore it later by means of C-x r j a. Again, it’s that easy.

Although there exist several packages providing more sophisticated window management (named configs, save and restore, etc.), in my experience, the simple tricks described above are more than enough (and work out of the box in any Emacs). As always, YMMV.

[0] For those of you not familiar with Emacs lingo, an Emacs frame is what most window managers would call a window, while an Emacs window is any of the regions displaying a buffer inside a frame. Usual ways of creating windows in Emacs are C-x 2, C-x 3, C-x 4 f, and so on. See Windows and WindowsAndFrames for more.

[1] In Emacs 21, the default keybindings start with C-x instead of C-c.

Imenu, with a workaround

imenu is a very handy command i learnt not long ago. Just type M-x imenu in a source code buffer (an Elisp one, for instance) and start typing the name of a variable or function (or simply TAB for name completion). There you go: instant access to definitions in the buffer.

You can also get the list of completions in a menu item calling imenu-add-menubar-index. Or, if you want the menu to be automatically created for, say, all your C files, you can put something like

(add-hook 'c-mode-hook 'imenu-add-menubar-index)

in your initialisation files.

You can read more about imenu (which has obsoleted speedbar in my workflow) in the Emacs Wiki, including how to adapt it to your favorite language (if it’s not yet supported).

Oh, and the workaround: if you’re using Emacs 22 on Mac OS X, you may need to add this dummy definition to your .emacs to make imenu work:

(defun imenu-progress-message (a &optional c d))

(otherwise, you’ll get an error about imenu-progress-message being an invalid function).

Killing, yanking and copying lines

Kind reader William Annis noticed how clumsily i copied a line in the kill ring in my last screencast. I went to the beginning of the line (C-a), killed it (C-k) so i got it in my kill-ring, and yanked it back (C-y). Admittedly, a bit cumbersome, my only excuse being that i have this keystroke sequence hardwired in my nervous system from the dawn of my emacs times; approximately the same epoch when i put in my .emacs this setting to make kill-line eat also the final carry return:

(setq kill-whole-line t)

But, as William notices, there’re better ways of copying the current line. A quick one would be:

(defun jao-copy-line ()
  "Copy current line in the kill ring"
  (kill-ring-save (line-beginning-position) 
                  (line-beginning-position 2))
  (message "Line copied"))

which can be easily extended to take as a prefix argument the number of lines to copy (with 1 as the default)

(defun jao-copy-line (arg)
  "Copy lines (as many as prefix argument) in the kill ring"
  (interactive "p")
  (kill-ring-save (line-beginning-position) 
                  (line-beginning-position (+ 1 arg)))
  (message "%d line%s copied" arg (if (= 1 arg) "" "s")))

Assign the new function to a handy shortcut, and you’re done. These functions have the additional benefit of leaving the point untouched.

Next thing you’ll do after using your new shiny shortcut will most probably be yanking those lines somewhere. And it may well happen that they’ll be mis-indented in their new location. A relatively quick way of realigning them is to take advantage of the fact that after a yank the region is set to the yanked text and use indent-region, which is bound to C-M-\. But i prefer to let Emacs do that for me, using this piece of advice in my .emacs:

(defadvice yank (after indent-region activate)
  (if (member major-mode '(emacs-lisp-mode scheme-mode lisp-mode
                           c-mode c++-mode objc-mode
                           LaTeX-mode TeX-mode))
      (indent-region (region-beginning) (region-end) nil)))

where, as you can see, i’m limiting the advice to some modes: just delete the major-mode check if you want it to work everywhere.

Type faster with Predictive

I had Predictive in my list of Emacs packages to try out and review, but Mathias Dahl has saved me the trouble by writing a nice tutorial on using Predictive, complete with screenshots and all. In a nutshell,

Predictive is a package for the GNU Emacs Editor that helps you write text faster. It does this by trying to predict what you are going to write by looking at the first few characters of each word you type, looking into a dictionary for matching words, and suggesting words from this dictionary.

Mathias goes on providing all the details you need to get stated with Predictive. Although i have not yet used it, looks pretty interesting.


Oh, as an aside, i recommend taking a look at Tony ‘Qubit’ Cubitt’s site for more Emacs (and non-Emacs) goodies, or just to enjoy a beautiful website.

Record, play, re-play

Gentle reader Marc Donner has sent me an email sharing one of his favorite emacs hacks:

(global-set-key [f10]  'start-kbd-macro)
(global-set-key [f11]  'end-kbd-macro)
(global-set-key [f12]  'call-last-kbd-macro)

Yes, I could type C-x ( to start the macro and C-x ) to end the macro and C-x e to execute the macro, but that requires that my fingers be more nimble than they really are. Particularly because ( and ) are shifted keys.

This way I can start a macro with a single button push, finish it with another, and then repetitively run it by pressing a third button over and over.

As it happens, Emacs 22 comes with the functionality Marc assigns to his F12 key built-in: press just e right after executing the macro the first time, as many times as you need. There’s also the possibility of using a numerical prefix to specify how many times the macro should be executed (as in M-10 C-x e to execute it ten times), and passing zero as the prefix will keep on (re)executing it until the end of the buffer is reached.

One can also achieve macro re-plays with the command repeat, bound by default to C-x z. repeat works quite a bit like Vim’s ‘.’: it, well, repeats the most recently executed command, and keeps repeating if you keep pressing z. So, instead of C-x e e e e e ..., one can C-x e C-z z z z.... Not as convenient, but this works in Emacs 21, and, besides, repeat is an interesting command on its own.

Saying that keyboard macro recording is a useful Emacs feature would be an understatement. You can record almost any Emacs operation and, therefore, keyboard macros can automate quite non-trivial tasks. For instance, i’ve just used them a couple hours ago to generate C function body stubs from their declarations in a header file. Just for the fun of it, here’s a screencast:

Note how you’re not limited to a single buffer, and how we use generic operations (like searching for next space to kill and yank the function’s return type). It’s also important to left the cursor in the right position for the next execution of the macro. At first you’ll make little mistakes, but fear not: you can actually edit the last recorded macro with kmacro-edit-lossage (Emacs 22 only). Just type C-xC-kl and you’ll be teletransported to a *Edit macro* buffer where you can edit your last keystrokes and record them as needed. Nifty, no?

As you get used to macros, you’ll surely discover many other tricks, but you can speed up your learning by means of the excellent pages KeyboardMacros and KeyboardMacrosTricks in the Emacs wiki. (One of my favourite tricks is using Elisp in macros to dynamically change what gets inserted). This Linux Journal article by Jesper Pedersen is also a good way to get you started as a power macro user.

Happy recording!

Emacs v. vi is rooted in the love of Lisp

In the comments section of an early post, Steven G. Harms has mentioned his little essay Emacs v. vi is rooted in the love of Lisp. I think it’s a pretty interesting take on what makes Emacs a powerful environment, and that it will probably be a pretty interesting reading for readers of mew. (And no, i have no interest at all in yet another boring Emacs vs. Vi religious war–i use vim almost daily, without feeling guilty or anything.)

Eval and replace anywhere

Being a living Elisp virtual machine, Emacs naturally provides the ability to evaluate any Elisp expression anywhere. Just put the cursor right after the expression to be evaluated and press C-xC-e: the result appears in the mini-buffer. I use this continuously, for instance while reading about a variable to know its value. For instance, imagine i see this line in one of my files:

(setq planner-project "planner")

and want to know if planner-project has been modified. I just need to put my cursor right after the variable name and get its value with C-xC-e.

As i said, C-xC-e works in any (well, almost) buffer. Imagine you’re writing a note and need to perform an arithmetic operation: you could write something like

   Yesterday I spent (+ 2234.34 3423.4 (* 12.0 12.2) 10) dollars at ...

put the cursor just before ‘dollars’, press the eval shortcut, and see the result of the arithmetic operation in the mini-buffer. You memorize it, delete the Elisp expression… hmm, wait, this should be easier, shouldn’t it? What we want is Emacs to eval and replace the expression for us.

I’m sure that you already know what comes next 🙂 As it happens, this time i didn’t need to write the Elisp snippet myself: i stole it instead from Jorgen “forcer” Schäfer’s configuration file:

(defun fc-eval-and-replace ()
  "Replace the preceding sexp with its value."
  (prin1 (eval (read (current-kill 0)))

Complemented with the mandatory shortcut:

(global-set-key (kbd "C-c e") 'fc-eval-and-replace)

this little gem has probably the highest usefulness to lines of code ratio in my Elisp toolbox. Give it a try!

Update: pdq notes in a comment that, if the expression to be evaluated is malformed (like, say, (+1 1)) it gets deleted, and it would be nice if it wouldn’t. A quick way to get this functionality is to catch errors in eval and undo the killing in a generic error handler. To wit:

(defun fc-eval-and-replace ()
  "Replace the preceding sexp with its value."
  (condition-case nil
      (prin1 (eval (read (current-kill 0)))
    (error (message "Invalid expression")
           (insert (current-kill 0)))))

Quick and dirty code folding

Code folding, the ability to hide code or text blocks in source or text files, is a feature present in most editors nowadays. Emacs has several modes providing this functionality with various degrees of sophistication: outline-mode, allout, hide-show or folding-mode come to mind. One can even use Emacs narrowing capabilities to cook up custom folding.

Over the years, i’ve used several of these packages (and perhaps i’ll share some tips on them in future postings). But i’ve noticed that, at the end of the day, i have usually little use for folding. I strive for short functions (whenever a function body extends a half-page, i feel a strong refactoring urge), and as a consequence i seldom need to hide their bodies, nor to mention blocks within them.

There is however one folding trick that i use all the time. I often want to hide all function bodies in a file, to get a quick summary of it contents. As it happens, you don’t need any package or extra configuration for that. Just go to any source file, type

        M-1 C-x $

and magic happens! As usual, it’s white magic: C-x $ will bring your code back.

We can use Emacs’ help system to discover what’s going on: C-h k C-x $ tells us that the above key combination is calling set-selective-display, a function that takes one numerical argument (the M-1 prefix passes 1 as the value of that argument) and, unsurprisingly, sets the variable selective-display to the value of that argument. I’ll let you read about the details in Emacs help itself, although i’m sure that by now you get the idea.

If you find yourself setting and unsetting selective-display as often as i do, next thing will be defining a handy keyboard shortcut, right?

(defun jao-toggle-selective-display ()
  (set-selective-display (if selective-display nil 1)))

(global-set-key [f1] 'jao-toggle-selective-display)

or we can also use an optional numerical prefix (with 1 as default) with a little modification:

(defun jao-toggle-selective-display (column)
  (interactive "P")
   (if selective-display nil (or column 1))))

Happy folding!

Enhancing Gmail browsing

I use pretty often emacs-w3m to read technical HTML docs within Emacs. When you’re programming, reading manuals, blogs or tutorials without leaving your operating system editor is all but convenient.

Browsing EmacsWikiActually, emacs-w3m does a very, very good job rendering even moderately complex sites, and i’ve been known to use it as my default browser. It is not limited to plain text (Emacs groks images these days), has tabs (shameless plug: take a look at my w3m-session package to add session management) and renders tables and other HTML constructs nicely. And of course, navigating sites like the Emacs wiki without leaving Emacs is just the way to go. Besides, if you don’t like how a page looks in emacs-w3m, opening it in your graphical browser is just a keystroke away (C-h w w3m-view-url-with-external-browser to discover it).

Gmail's login pageI also have usually open a tab with my Gmail account, to quickly check my email every now and then. As you can see in the screenshot on the right, emacs-w3m renders Gmail pages very decently (hint: you’ll need to activate cookies to use Gmail, by setting the variable w3m-use-cookies to a non-nil value). But of course the Gmail page i visit more frequently is the Spam tab, and the operation i want to perform more frequently in there is to mark all the mails and press ‘Delete Forever’. But alas, w3m does not support javascript, and the handy ‘Select All’ link is missing. Obviously, tabbing your way to every single spam mail to select it would be a pain, and one is tempted to follow the advice at the top of the page about a better Gmail experience.

GmailBut wait, this is not your regular browser. We’re inside Emacs, and in here, when we need something, we usually just hack it and keep on working. After all, what we need to do is quite simple: look for occurrences of ‘[ ]’ in the buffer, click them, and leave the pointer over the ‘Delete forever’ button, ready to send all our spam where it deserves. As it happens, our life is even easier thanks to the functions provided by emacs-w3m. w3m-form-goto-next-field allows easy navigation inside a form, and w3m-view-this-url will perform a click for us. So here we go: let’s write an Elisp command that looks for unmarked checkboxes and clicks them:

  (defun jao-w3m-gmail-mark-all ()
    (when (search-forward "[ ]" nil t)
      (backward-char 4)
      (while (looking-at " \\\\]")

That’s it. After evaluating this definition, we just M-x jao-w3m-gmail-mark-all to mark all our mails. Of course, a keyboard shortcut will come in handy:

  (define-key w3m-mode-map (kbd "C-ck") 'jao-w3m-gmail-mark-all)

and now we only need to press C-ck to get the job done. While we’re at it, it would be nice if typing a prefix would unmark mails instead of marking it, wouldn’t it? Well, that only needs a few tweaks in our original definition:

  (defun jao-w3m-gmail-mark-all (unmark)
    (interactive "P")
    (when (search-forward (if unmark "[*]" "[ ]") nil t)
      (backward-char 4)
      (while (looking-at (if unmark "\\\\*\\\\]" " \\\\]"))

Now, C-ck will mark, and C-uC-ck will unmark.

As you can see, the limit is just your imagination. For instance, when i’m reading non-spam, i usually mark some mails for deletion. Then i need to go to the ‘More actions…’ menu, select the ‘Delete’ operation and press ‘Go’. Hmm, a bit of a chore. Elisp to the rescue:

  (defun jao-w3m-gmail-delete ()
    (when (search-forward "[More Actions" nil t)
      (search-forward "Trash")

Admittedly, these little extensions are far from earth-shattering, but i think that they illustrate quite well what having an extensible system means. You control your environment and adapt it to your particular needs, often building upon functionality provided either by the system or by some other extension library. Another nice example of extensibility at work is my w3m-session package: i wanted persistent sessions, so i wrote an extension. And it was easy, thanks to the nice work of the emacs-w3m hackers and, let’s not forget, the dynamic nature of Elisp.

In addition, the programmer in me finds tweaks like this a continuous source of fun. You know, that fuzzy warm feeling… but i digress. Happy hacking!

Editing your file names

In my experience, dired is one of the most underused modes of Emacs. Just C-xC-f to any directory in your hard disk and you’ll be presented with a list of its files and directories. One can browse this list, and execute all kinds of commands and transformations on them. If you’ve never done that before, just give it a try, and look at the menubar for a list of nifty things you can do inside dired. There’re several ways to mark files and operate on them afterwards: for instance, just type A to find inside the selected files any regexp, or Q to search and substitute.

One of my favorites dired functionalities is wdired-change-to-wdired-mode. When you invoke this interactive function (using M-x wdired-change-to-wdired-mode), the dired buffer becomes editable. That is, you can go around and edit directly the filenames as you would edit any other emacs text buffer. And that means you have all the regular editing commands at your disposal. For instance, if i enter wdired-mode in my emacs configuration directory:


and want to change the name of all those jao-*.el files to, say, jao-config-*.el, all i have to do is to search and replace as i would do in any other text file (i.e., using M-%). Or maybe put the cursor on the first file name, mark (C-SPC), got to the end of the jao-files list, put my cursor after the last dash (as shown in the figure), and use C-xrt to replace the text in the marked rectangle. When you’re happy editing the buffer, just press C-cC-c and all your changes will be reflected in the underlying files.