Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Emacs editing environment, Part 5: Shape your Emacs view

Get going with this famous open source editor

Michael Stutz, Author, Consultant
Photo of Michael Stutz
Michael Stutz is author of The Linux Cookbook, which he also designed and typeset using only open source software. His research interests include digital publishing and the future of the book. He has used various UNIX operating systems for 20 years.

Summary:  This tutorial, the fifth in a series, shows you how to manage and manipulate the shape your Emacs session—examine how to partition the Emacs screen, create multiple X client windows for a single Emacs session, and display multiple buffers in each window, dividing the screen with horizontal and vertical divisions. You also learn about mouse window control and characteristics so that by the time you're through, you can make your Emacs session look and work the way you want it to.

View more content in this series

Date:  07 Aug 2007
Level:  Intermediate PDF:  A4 and Letter (1247 KB | 26 pages)Get Adobe® Reader®

Activity:  22894 views
Comments:  

Split and partition your Emacs session

You can use a number of commands to control an Emacs window, which is the view of a buffer that you see framed in an Emacs X client window containing the buffer itself and its mode line beneath it. These commands let you view multiple buffers at once by partitioning your window in various ways. Practically speaking, these are the most commonly used of all the commands for manipulation of Emacs client windows.

The examples in this tutorial use a sample data set, which is available in a compressed archive file (see Downloads). This archive file contains a tar archive containing two plain text files, innocence and experience, whose contents are the complete text of William Blake's The Songs of Innocence and The Songs of Experience, respectively—you'll recognize some of the text from previous tutorials. To get started, extract these two files into your own examples directory.

Split a window vertically

Probably the most common way to partition an Emacs window is to slice it in half across the middle of the screen. This action makes two new windows, each with its own mode line and each approximately half the height of the original. You cut horizontally across the Emacs screen, so the two new windows are stacked on top of each other in a vertical column; thus, this kind of window operation is called a vertical split.

To make such a split, run the split-window-vertically function, which is bound to the C-x 2 keystroke.

Try it with the sample files:

  1. Start Emacs by typing emacs in the directory containing your copies of the two sample files.

  2. Open the files:

    C-x C-f innocence Enter C-x C-f experience Enter

  3. Split the window:

    C-x 2

  4. Notice that the active buffer (named experience, which was the last buffer you opened) appears in both windows when the window splits. The buffer in the top window is now the active buffer and contains an active cursor. Switch to the innocence buffer in this top window:

    C-x b Enter

Your Emacs session should look like Figure 1.


Figure 1. Splitting an Emacs window vertically
Splitting an Emacs window vertically

Incidentally, this is the same two-buffer vertical split that Emacs does automatically when you start it with two files as arguments, as described in the last tutorial in this series, "Emacs options, registers, and bookmarks." Type C-x C-c to exit Emacs, and then try it on the sample files:

$ emacs innocence experience
                

Notice the one difference: When you specify two files as command-line arguments, the bottom window (whose contents are the second argument) becomes the active window.

You're not limited to two windows at once—Emacs can show as many windows as will fit on the screen. But remember that when you make a split, Emacs splits the current window, and all other windows remain untouched.

For example, the bottom window of the two is now active; type C-x 2 to split just this one lower window.

Your Emacs screen is now divided into three windows, and the middle window is active. Type C-x 2 to split that middle window again, making a total of four windows in the Emacs screen—and then type C-x 2 again to try to split the second, tiny window. Depending on the size of your main Emacs X client window, this recursive splitting of split windows comes to an end sooner or later, at the point when any new window would be too small to display at least two lines of text and a mode line; then, Emacs beeps and reports in the minibuffer that the given window can't be split further. Your session should look like Figure 2.


Figure 2. Multiple Emacs vertical splits
Multiple Emacs vertical splits

You can use split windows to view multiple buffers at once, but it's also helpful to view the same buffer in multiple windows—specifically, to view different parts of a buffer in different windows. This ability to edit one part of the buffer while you're looking at another part of the buffer is one of the most useful tricks in power editing. It's a tremendous aid.

Try it now:

  1. Exit Emacs by typing C-x C-c, and then start it up with a single file:

    $ emacs experience
                            

  2. Type M-> to move to the end of the buffer.

  3. Type C-x 2 to split the window vertically, with the same buffer in both windows.

  4. Move to the top of the buffer in the active window: Type M-<.

  5. Move the cursor down several dozen lines with the down arrow key. You can move through the buffer as it appears in the top window, but Emacs always displays the end of the same buffer in the bottom window.

When you split a window, Emacs automatically determines the dimensions of the split—it usually cuts a buffer in half, so the two new buffers created in a vertical split each use exactly half the space of the original. But you can also specify the size, in number of lines, that the top window of a vertical split should have (including its mode line) by preceding the split command with a number.

Try splitting the buffer in two with nine lines for the top buffer:

  1. Exit Emacs by typing C-x C-c, and then start it up with a single file:

    $ emacs experience
                            

  2. Type M-9 C-x 2 to split the buffer, with nine lines in the top buffer.

To specify the number of lines for the bottom buffer instead of the top, use a negative number.

Try splitting the buffer in two, with four lines for the bottom buffer:

  1. Exit Emacs by typing C-x C-c, and then start it up with a single file:

    $ emacs experience
                            

  2. Type M-- M-4 C-x 2 to split the buffer, with four lines in the bottom buffer.

C-x 2 makes a vertical split, but a number of important key bindings for splitting windows begin with the C-x 4 prefix.

To open a new file in a new buffer and in a new window, use the find-file-other-window function, which is bound to C-x 4 f. Then, give the name of the file. To open the read-only file, use find-file-read-only-other-window instead and give the filename. It's bound to C-x 4 r.

Try opening the innocence file in a new buffer in the other window: Type C-x 4 f innocence Enter .

Although the other window already existed when you specified a four-line window, notice how this command resizes it so that the active window and the new window are about the same size.

The switch-to-buffer-other-window function, which is bound to C-x 4 b, splits the window vertically and lets you choose which buffer to display in the new other window, which also becomes the active window.

Try switching the buffer in the other window to the innocence buffer with this command: Type C-x 4 b innocence Enter and notice that it doesn't change the window you typed it in—it, too, now displays the innocence buffer.

To display a new buffer in another vertical window but keep the active cursor in the current window, run the display-buffer function, which is bound to C-x 4 C-o. It prompts for the name of the buffer to display in the other window, but the current window remains active. If the display has only a single window, this function splits it and creates a new window—but if the display already has two or more windows, a new split isn't made.

Try switching the buffer in the bottom window to the experience buffer with this command: Type C-x 4 C-o experience Enter .

Move inside a window

When you have multiple windows in your Emacs session, cursor motion isn't affected—you move normally in the current, active window just as if there were only one window in your session. And when you scroll this window, using either the scrollbar or the various keys for scrolling, the other window or windows don't scroll—even if those windows show a copy of the same buffer.

To scroll the other window, not the window the cursor is in, use the scroll-other-window function, which is bound to C-M-v. (Emacs keeps all your windows in an ordered list, so if you have more than two windows open, this command scrolls the window that is next on the list.)

Try typing C-M-v now to scroll through the lower window with the experience buffer in it until the title of "A Little Boy Lost" comes into view.

You can scroll all windows that are open to the same buffer at once by enabling scroll-all mode. This function is a toggle and, when it's active, the mode line shows *SL*. It moves all the windows of the same buffer together, even if the windows show completely different portions of the buffer—the scroll commands you type are applied to all windows containing that buffer.

Try it now:

  1. Type C-x b experience Enter to switch to the experience buffer in the top window.

  2. Turn on scroll-all mode by typing M-x scroll-all-mode.

  3. Scroll through both windows at once by typing PgDn while the top window is still active.

  4. Move the cursor up in both windows by pressing the up arrow key; press it enough times so that the top of the buffer appears in the top window, and keep pressing to see the cursor move in the bottom window but not the top.

  5. Move the cursor down in both buffers by pressing the down arrow key several times.

Move to another window

To move between the windows, run the other-window function, C-x o, which moves to the next window. It cycles through all windows when you run it repeatedly. When you move to another window, the cursor is drawn at the current point in that buffer.

You can also move the cursor to other windows by specifying them directionally with the windmove commands, as described by Table 1.


Table 1. Summary of Emacs windmove commands
FunctionDescription
windmove-up Move to the window directly above the current window, if it exists.
windmove-down Move to the window directly below the current window, if it exists.
windmove-left Move to the window directly to the left of the current window, if it exists.
windmove-right Move to the window directly to the right of the current window, if it exists.

Try it now:

  1. Type C-x o to move to the bottom window.

  2. Turn off scroll-all mode: Type M-x scroll-all-mode, and then scroll down to "A Little Boy Lost" again, noticing that the top window no longer scrolls.

  3. Move back to the top window by typing C-x o.

  4. Split this window with C-x 2.

  5. Move down to the window you just split by typing M-x windmove-down.

Delete a window

There are a few ways to delete Emacs windows.

To delete the current window—that is, the one where the active cursor currently is—run the delete-window function, which is bound to C-x 0.

Try it now to delete the third window you just created: Type C-x 0.

To delete all windows except the current window, run the delete-other-windows function, which is bound to C-x 1.

Try it: Type C-x 1.

When you delete a window, you don't kill the buffer it shows—the buffer remains open in Emacs. To delete the current window and kill its buffer at the same time, run the kill-buffer-and-window function, which is bound to C-x 4 0.

Try it:

  1. Split the window in half vertically: Type C-x 2 so that now you have two windows in your Emacs session.

  2. Move the cursor to the space between BOY and LOST in the poem title, and type M-t to transpose the two words.

  3. Write the current buffer, experience, to a new file called new.experience: Type C-x C-w new.experience.

  4. Kill the buffer and the window with C-x 4 0. Type y to verify.

When you do this, the new.experience buffer is killed, and your Emacs session has a single window again.

Split a window horizontally

Corresponding to the split-window-vertically function just described is split-window-horizontally, which is bound to C-x 3. This function splits the current Emacs window in half down the middle of the screen and lines up the two new windows horizontally, so they're up against each other. This is particularly good for side-by-side views of similar buffers. Try it:

  1. Open the experience file in a new buffer: Type C-x C-f experience Enter .

  2. Type C-x 3 to split the window in half horizontally.

  3. Type C-x b Enter to change the contents of the leftmost window to the innocence buffer.

Your Emacs session should now look like Figure 3.


Figure 3. Sample files in horizontal Emacs windows
Sample files in horizontal Emacs windows

As with vertical splits, you can precede this function with a numeric argument to specify the number of characters wide the window on the left should be; if you give a negative number, it specifies the width of the window on the right.

Try it: Split the current window in half horizontally, making the leftmost window 11 characters wide, by typing C-u 11 C-x 3.

Notice that the number you give includes a single character for the leftmost window's scrollbar, a single character for the blank column between the scrollbar and the contents of the buffer, the columns of the buffer itself, a column for arrow figures that indicate if a line wraps beyond its displayed width, and finally the scrollbar of the second window in the split. In this case, the leftmost window you just created contains a displayable width of seven characters, as shown in Figure 4.


Figure 4. Multiple horizontal windows in Emacs
Mulitple horizontal windows in Emacs

As with the vertical splits of the same buffer, any horizontal splits of the same buffer are treated individually. Scrolling and cursor motions in one don't affect the other, so when you move the cursor in one window, it doesn't affect any other windows containing the same buffer. But you can use another trick. If you turn on follow-mode, you can make a single large "virtual" window out of multiple windows of the same buffer.

This way, when you split a window horizontally, the windows show different but connected parts of the buffer, making one large virtual window of the buffer. Moving the cursor down at the end of one window brings it to the beginning of the next.

Try it:

  1. Type C-x 1 to remove all the windows and C-x 3 to split the single window in half horizontally. Now your Emacs session has two windows, each containing the innocence buffer and each with the cursor at the top of the buffer.

  2. Type M-x follow-mode to enable this mode for this buffer. Notice that the word Follow appears in the mode line of both windows. The contents of the second window changed: It now takes up right where the left window leaves off, beginning with the line just below that window.

  3. Move the cursor down through the buffer in the left window by pressing the down arrow key, and watch what happens when you get to the bottom of the window: The cursor moves to the top of the right window.

  4. Move the cursor down in the right window, and watch how both windows are redrawn. Move the cursor up in the right window with the up arrow key, and watch what happens when you get to the top of the window: The cursor moves to the bottom of the left window.

  5. This function is a toggle. Run it again to turn it off in that buffer: Type M-x follow-mode and notice that Follow no longer appears in the mode line.

Although follow-mode also works on buffers that are split vertically, using this mode offers no advantage in most cases.

Resize a window

So far, you've either used the default sizes for windows or specified their size at the time of their creation. But you can resize any Emacs window at any time.

To make the current window taller, run the enlarge-window function, which is bound to C-x ^. To make it shorter, precede this function with M--. To reduce or grow the window by a specific number of lines, give that number (using the universal argument, C-u).

You can also change the widths of windows. To make the current window narrower, run the shrink-window-horizontally function, which is bound to C-x {. To make it wider, run the enlarge-window-horizontally function, which is bound to C-x }.

Try shaping some windows in your Emacs session:

  1. Type C-x 1 to close any existing windows, and type C-x b innocence Enter to bring the innocence buffer to this window.

  2. Split this window in half horizontally with C-x 3.

  3. Switch the buffer in the rightmost window by typing C-x 4 C-o experience Enter .

  4. Split the leftmost window in half vertically with the experience buffer in the bottom window by typing C-x 2 C-x o C-x b experience Enter .

  5. Move the cursor to the beginning of the "Ah, Sunflower" poem by typing C-s sunf to search for the title, and then type C-u 6 C-l to redraw the window with this poem in view.

  6. Shrink this window by five lines and redraw it by typing M-- M-5 C-x ^ C-l.

  7. Some lines still wrap because the window isn't wide enough to display them. Make the window wider by two characters by typing C-x } C-x }.

If you end up reducing the window too much when resizing so that fewer lines remain than can draw the window and its mode line, the window is killed. Try it:

  1. Move to the window above this one: Type windmove-up.

  2. Type C-u -5 C-x ^ to shrink the window by five lines.

  3. Try it again a few more times: Type C-u -5 C-x ^, and type it again to shrink the window further. Keep going until it disappears.

You can use a few more tweaks. To reduce the size of the window, if possible, run the shrink-window-if-larger-than-buffer function, which is bound to C-x -. You can also balance the sizes of all visible windows by running the balance-windows function. It makes all windows the approximate same size. It's bound to C-x +.

Try it: Type C-x 2 C-x 2 to make a few more windows, and type C-x + C-x + to balance their heights.

In addition to using these commands, you can resize windows using the mouse, as you'll learn in the "Resize windows with the mouse" section.

Compare two windows

Frequently, multiple windows are used to compare the contents of buffers. The compare-windows function lets you do this: It compares the text in two windows, starting with the character at point in both windows and moving point (in both windows) to the first character that differs. If the files are identical, point is moved to the end of both buffers.

Try running it now on the sample file called experience and the new.experience file you edited earlier:

  1. Exit Emacs by typing C-x C-c, and then start it up with a single file:

    $ emacs experience
                            

  2. Type C-x f new.experience to open your edited file in a new buffer.

  3. Split the screen vertically with each of the buffers in its own window by typing C-x 3 C-x b Enter .

  4. Type M-x compare-windows to run the comparison.

The cursor in both windows moves to the first thing that differs in the file: the title of the poem you altered earlier. Your Emacs session should look like Figure 5.


Figure 5. Comparing the contents of two Emacs windows
Comparing the contents of two Emacs windows

Table of Emacs window commands

Table 2 contains a list of common Emacs commands for manipulating windows, giving their key binding if applicable and describing their function.


Table 2. Common Emacs window-manipulation commands
FunctionBindingDescription
split-window-vertically C-x 2 Split the current window in half across the middle, stacking the new buffers vertically.
switch-to-buffer-other-window C-x 4 b Split the current window in half vertically, prompting for the buffer to use the bottom window and making that the active window.
display-buffer C-x 4 C-o Display a buffer in another window, prompting for the buffer to use the other window but keeping the current window active. (If only one window exists, then split the window vertically to display the other buffer.)
find-file-other-window C-x 4 f Open a new file in a new buffer, drawing it in a new vertical window.
find-file-read-only-other-window C-x 4 r Open a new file in a new read-only buffer, drawing it in a new vertical window.
scroll-other-window C-M-v Scroll to the window that would be the next one to switch to with C-x o.
scroll-all Toggle the scroll-all minor mode. When it's on, all windows displaying the buffer in the current window are scrolled simultaneously and in equal, relative amounts.
other-window C-x o Move the cursor to the next window, and make it the active window.
windmove-up Move to the window directly above the current window, if it exists.
windmove-down Move to the window directly below the current window, if it exists.
windmove-left Move to the window directly to the left of the current window, if it exists.
windmove-right Move to the window directly to the right of the current window, if it exists.
delete-window C-x 0 Delete the current window, and move the cursor to the window that would be the next one to switch to with C-x o.
delete-other-windows C-x 1 Delete all windows except the current window.
kill-buffer-and-window C-x 4 0 Delete the current window, and kill its buffer.
split-window-horizontally C-x 3 Split the current window in half down the middle, stacking the new buffers horizontally.
follow-mode Toggle follow, a minor mode. When it's on in a buffer, all windows displaying the buffer are connected into a large virtual window.
enlarge-window C-x ^ Make the current window taller by a line; preceded by a negative, this makes the current window shorter by a line.
shrink-window-horizontally C-x } Make the current active window thinner by a single column.
enlarge-window-horizontally C-x { Make the current active window wider by a single column.
shrink-window-if-larger-than-buffer C-x - Reduce the current active window to the smallest possible size for the buffer it contains.
balance-windows C-x + Balance the size of all windows, making them approximately equal.
compare-windows Compare the current window with the next window, beginning with point in both windows and moving point in both buffers to the first character that differs until reaching the end of the buffer.

2 of 8 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=245702
TutorialTitle=Emacs editing environment, Part 5: Shape your Emacs view
publish-date=08072007
author1-email=stutz@dsl.org
author1-email-cc=mmccrary@us.ibm.com