O'Reilly logo

Learning GNU Emacs, 3rd Edition by Bill Rosenblatt, Eric S. Raymond, Marc Loy, James Elliott, Debra Cameron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

More About Windows

Depending on your requirements, you may want to work with side-by-side windows in addition to or instead of horizontal windows. For finer control, you may want to know how to size windows (and because they're not GUI windows, you can't do that with the mouse).[2] You may also want to know how to compare files between windows, a good feature for basic file comparison.

Creating Vertical or Side-by-Side Windows

To split the window vertically into two side-by-side windows, type C-x 3. You can execute this step repeatedly to create more side-by-side windows.

Type: C-x 3

image with no caption

Emacs creates two vertical windows.

When you create multiple vertical windows, Emacs usually doesn't have enough room to display a full line of text. Because vertical windows don't usually show full lines of text, a right arrow (on graphical implementations) or a dollar sign (on terminal-based implementations) at the end of a line tells you the line is continued.

To see the rest of the line, you need to know how to scroll text to the left and right. To push the text currently being displayed to the left (so you can see what's on the right), type C-x <. Left arrows or dollar signs are displayed on the left side of the window to indicate that there is more text to the left. To push the text being displayed to the right (so you can see what's on the left), type C-x >. You can use these commands whenever one of your lines is too wide, which can happen with or without windows.

Navigating Windows

How do you move between windows? As we mentioned earlier, C-x o moves you to the "next" window. But how does Emacs determine what that is?

The best way to express it is to say that Emacs moves through the windows in natural reading order, from left to right, then down, and again from left to right. In Figure 4-4, buffer names are numbered to show you how Emacs moves from one window to the next.

Moving between windows (Mac OS X)

Figure 4-4. Moving between windows (Mac OS X)

Alternatively, you can simply select the window you want using the mouse.

Enlarging and Shrinking Windows

Emacs always splits windows into two equal parts. Such a split is often good enough, but sometimes it's not, particularly if you become a window aficionado. When you have four or five or six windows on your screen at once, controlling each window's size becomes important. Otherwise, the windows you are most interested in will eventually become too small, and useful editing is almost impossible when you can see only five or six lines from a file. If you want to make the window you're working on taller, type C-x ^. Emacs lengthens the current window and makes the one below it smaller, accordingly. To make the current window wider, type C-x }. Emacs makes this window wider, at the expense of the one to the right of it.

To make windows smaller, you can shrink them. To shrink a window vertically, type M-x shrink-window. Emacs shrinks the current window by one line and the other windows on the screen grow accordingly. To shrink a window horizontally, type C-x {. This command makes the window one column narrower and enlarges the other windows on the screen horizontally.

Usually you want to work in larger increments than one line or one column at a time, however. When you type C-u preceding any of these commands, the command works in increments of four lines or columns at a time. For example, with two horizontal windows on the screen, let's use C-u C-x ^ to enlarge the james window.

Type: C-u C-x ^

image with no caption

Emacs makes the current window larger.

As you would expect, when you make the window larger, it automatically fills with more text from the buffer. There are shortcuts to sizing windows as well. If you have a very small buffer—for example, a one-line buffer containing the vocabulary-building word for the day and its definition—you can shrink the window to the size of the buffer by typing C-x - (for shrink-window-if-larger-than-buffer). If the buffer is larger than the window, this command does nothing. Typing C-x + (for balance-windows) creates windows of equal size again. (This latter command is also useful if you have an odd number of windows; C-x + divides the display equally among them.)

Limits on Window Size

Windows in Emacs can be as big as your screen. There's a limit to how small windows can be, however, and this limit is specified by the variables window-min-height (whose default is four lines) and window-min-width (whose default is ten characters). If you enlarge other windows to the point that their counterparts become less than ten characters wide or four lines high, Emacs deletes the smaller windows. You can set these variables to other values if you want to; more information on setting variables is found in Chapter 10.

Comparing Files Between Windows

Especially if you're looking for minute differences between large files, the compare-windows command comes in handy. To use compare-windows, you must first have the buffers you want to compare in two windows, either side by side or horizontally. Go to the beginning of each buffer, then type M-x compare-windows. Emacs scrolls each buffer to the place where the discrepancy is. It places the point in each buffer at the place of the discrepancy, so using C-x o to move the cursor between buffers will show you exactly where the files differ.[3]

Of course, this maneuver finds only the first difference between the two buffers. Finding the second, third, and so on, is a bit tricky. The compare-windows command works only if the point in both buffers is in exactly the same place. Therefore, you need to move past the discrepancy in both buffers before you can type M-x compare-windows again. The Unix diff command provides a more comprehensive (although somewhat awkward looking) way to find the differences between two files. Emacs also provides an interface to Ediff, with options on the Compare menu (a submenu of the Tools menu). Ediff is far more comprehensive; see Chapter 12 for details.

Table 4-4 summarizes the window commands discussed in this chapter.

Table 4-4. Window commands

Keystrokes

Command name

Action

C-x 2 File Split Window

split-window-vertically

Divide current window into two windows, one above the other.

C-x 3

split-window-horizontally

Divide current window into two side-by-side windows.

C-x >

scroll-right

Scroll the window right.

C-x <

scroll-left

Scroll the window left.

C-x o

other-window

Move to the other window; if there are several, move to the next window (see "Navigating Windows").

C-x 0

delete-window

Delete the current window.

C-x 1 File Unsplit Windows

delete-other-windows

Delete all windows but this one.

(none)

delete-windows-on

Delete all windows on a given buffer.

C-x ^

enlarge-window

Make window taller.

(none)

shrink-window

Make window shorter.

C-x }

enlarge-window-horizontally

Make window wider.

C-x {

shrink-window-horizontally

Make window narrower.

C-x -

shrink-window-if-larger-than-buffer

Make window smaller if buffer is smaller than window.

C-x +

balance-windows

Make windows the same size.

C-M-v

scroll-other-window

Scroll other window.

C-x 4 f

find-file-other-window

Find a file in the other window.

C-x 4 b

switch-to-buffer-other-window

Select a buffer in the other window.

(none)Tools Compare (Ediff) This Window and Next Window

compare-windows

Compare this window with the next window and show the first difference.



[2] It's true that you can't resize Emacs windows using the mouse. But if you resize an Emacs frame, it does impact the size of the windows, even eliminating windows at times if the frame cannot display all the windows. Of course, as always, eliminating a window doesn't impact the underlying buffer.

[3] You can have more than two windows on the screen, but only two are compared: the one the cursor is in and the next window (remember that the next window is either to the right or down if there is no window to the right).

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required