May 31, 2006

Emacs tips: Windows and buffers

Author: Nathan Willis

If you are an Emacs user who read Joe Brockmeier's Vim tips column on Vim viewports and thought, "Viewports sound useful; can I use them in a real text editor?" The answer is yes.

The terminology differs, and there are some slight behavioral distinctions thanks to Emacs's ability to keep multiple files open as separate buffers. But Emacs enables you to work with more than one file in view simultaneously, and even keep the same file open and in sync in several windows.

Basic window arithmetic

A basic Emacs session starts with one open window -- the Emacs name for the viewable area. Whatever you are looking at in the window is a buffer -- usually a file, but also perhaps a system screen or scratch space.

If you are used to working in multiple buffers in a single session, you are probably in the habit of switching between them in that one, big comfortable window. But you can split the current window into two windows with one keystroke, and either work with multiple buffers on screen, or look at different parts of the same buffer in two different windows -- an especially useful feature when you are editing long documents. This is helpful when repairing xorg.conf, for instance.

Ctrl-x 2 splits the current window in two vertically, and Ctrl-x 3 splits it horizontally. You can repeat the process in every new window, creating as many as you need.

Emacs window keybinding quick reference

Ctrl-x 2 splits the current window vertically.
Ctrl-x 3 splits the window horizontally.
Ctrl-x o cycles through the windows, left-to-right, top-to-bottom.
Ctrl-x ^ grows the active window one line.
Ctrl-x } widens the active window.
Ctrl-x { narrows the active window.
Ctrl-x + resizes all windows to equal size (height).
Ctrl-x 0 closes the active window.
Ctrl-x 1 closes all but the active window.
Ctrl-Meta-v scrolls the text in the next (i.e., not active) window.
Meta-x compare-windows searches for and jumps to mismatching text.

Ctrl-x o -- that's the letter o and not the numeral 0 -- will cycle through the open windows. You can grow the active window by one line of height with Ctrl-x ^, or widen or narrow it horizontally with Ctrl-x } and Ctrl-x { respectively.

If things start to get out of hand, you can resize all of the open windows to be the same height with Ctrl-x +. You can close the active window with Ctrl-x 0 (and that is the numeral 0), or close every window except the active one with Ctrl-x 1.

Taking advantage of multiple windows

Once the thrill of opening and resizing windows is gone, you may want to get some actual work done. This is where the fun begins -- doing things in your multiple-window session that you can't do with just one window. For starters, you can use a series of commands to control other open windows without leaving the comfort of the active window.

By default, when Emacs splits a window in two, both of the newly formed windows display the same buffer as the original. Once you open a new window, you can switch to the next window and open a different buffer -- or you could do both at once with Ctrl-x 4 b some_buffer_name if the buffer you want is already open, or Ctrl-x 4 f some_file_name to open a file into a new buffer.

Ctrl-Meta-v scrolls the text in the next window, which is helpful when using one window for reference and another for editing.

When tweaking xorg.conf I often want to compare multiple versions of the file as I make changes. The diff command is great for doing this at the console, but when editing is required it is so much easier just to do it within Emacs: Meta-x compare-windows will compare the text in the active and next windows, and jump to the next mismatch.

Capitalizing on Emacs's extensibility, there are more advanced windowing options as well. Follow mode (Meta-x follow-mode), for example, will synchronize several windows so that you can scroll them all just by scrolling the active window.

For more detail on Emacs's multiple windowing capabilities, read through the Windows section of the Emacs Manual, especially the page on window convenience.

Click Here!