Emacs editing environment, Part 5: Shape your Emacs view

Get going with this famous open source editor

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.

Share:

Before you start

Learn what to expect from this tutorial, and how to get the most out of it.

About this series

The Emacs editing environment is a favorite of UNIX® developers. It's known around the world as the king of editors, but many users find it has a bit of a learning curve. The Emacs environment doesn't seem intuitive at first glance, and it doesn't work like other editors and word processors. But learning Emacs doesn't have to be difficult. Once you get going, you'll see how intuitive it is and become more comfortable with it after each use. This tutorial series (see Resources) shows you the way, taking you from the basics of Emacs, such as its features, philosophy, key-command layout, and methods for editing text, through many of its powerful editing features.

After completing this series (see Resources), you'll be able to comfortably use Emacs for everyday editing, be well on your way to Emacs proficiency, and have a good feel for many of the advanced capabilities of Emacs.

About this tutorial

This intermediate-level, hands-on tutorial builds on what you learned in previous installments, and shows you how to customize and tool the system for your specific needs.

In this tutorial, you learn how to manage and manipulate the viewport of your Emacs session—the main Emacs X client window and the space inside it that displays buffers and other information. You find out the best way to partition this area for your use, including creating multiple X client windows for a single Emacs session and displaying multiple buffers in each window by dividing the screen with horizontal and vertical divisions. You also examine how to use the mouse to manipulate these divisions.

Objectives

This tutorial illustrates how to manipulate your view into Emacs: how to partition and divide an Emacs window and how to make more Emacs windows that attach to a single Emacs session.

After working through this tutorial, you'll know how to manipulate frames and the windows they contain, including using mouse techniques to do so.

Prerequisites

Before working through this tutorial, you should complete the previous tutorials in this series. They lay down the basic foundation and explain many of the Emacs concepts you use in this tutorial (see Resources).

The special Emacs notation for representing keystrokes, which is used in this tutorial and throughout the series, is described in the introduction of the first tutorial of the series, "Learning the Emacs editing environment, Part 1: The basics of Emacs."

Although this tutorial is written for all levels of UNIX expertise, it's helpful if you have at least a rudimentary understanding of the UNIX file system:

  • Files
  • Directories
  • Permissions
  • File system hierarchy

System requirements

This tutorial requires a user account on any UNIX-based system that has a recent copy of Emacs installed.

There are several varieties of Emacs; the original and most popular is GNU Emacs, which is published online by the GNU Project (see Resources).

You should have a recent copy of GNU Emacs—one that is at version 20 or greater. Versions 20 and 21 are the most commonly available, and development snapshots of version 22 are also available. This tutorial works with any of these versions for Emacs. If your system is running something older, it's time to upgrade.

To know what version of Emacs you have running, use the GNU-style --version flag:

$ emacs --version
GNU Emacs 22.0.91.1
Copyright (C) 2006 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
$

Because this tutorial deals exclusively with graphical elements of Emacs operation in the X Window System environment, you should also have an X server up and running.

This tutorial uses a two-file sample data set, which is available as a single archive file (see the Downloads section for the link).


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-upMove to the window directly above the current window, if it exists.
windmove-downMove to the window directly below the current window, if it exists.
windmove-leftMove to the window directly to the left of the current window, if it exists.
windmove-rightMove 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-verticallyC-x 2Split the current window in half across the middle, stacking the new buffers vertically.
switch-to-buffer-other-windowC-x 4 bSplit the current window in half vertically, prompting for the buffer to use the bottom window and making that the active window.
display-bufferC-x 4 C-oDisplay 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-windowC-x 4 fOpen a new file in a new buffer, drawing it in a new vertical window.
find-file-read-only-other-windowC-x 4 rOpen a new file in a new read-only buffer, drawing it in a new vertical window.
scroll-other-windowC-M-vScroll to the window that would be the next one to switch to with C-x o.
scroll-allToggle 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-windowC-x oMove the cursor to the next window, and make it the active window.
windmove-upMove to the window directly above the current window, if it exists.
windmove-downMove to the window directly below the current window, if it exists.
windmove-leftMove to the window directly to the left of the current window, if it exists.
windmove-rightMove to the window directly to the right of the current window, if it exists.
delete-windowC-x 0Delete 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-windowsC-x 1Delete all windows except the current window.
kill-buffer-and-windowC-x 4 0Delete the current window, and kill its buffer.
split-window-horizontallyC-x 3Split the current window in half down the middle, stacking the new buffers horizontally.
follow-modeToggle follow, a minor mode. When it's on in a buffer, all windows displaying the buffer are connected into a large virtual window.
enlarge-windowC-x ^Make the current window taller by a line; preceded by a negative, this makes the current window shorter by a line.
shrink-window-horizontallyC-x }Make the current active window thinner by a single column.
enlarge-window-horizontallyC-x {Make the current active window wider by a single column.
shrink-window-if-larger-than-bufferC-x -Reduce the current active window to the smallest possible size for the buffer it contains.
balance-windowsC-x +Balance the size of all windows, making them approximately equal.
compare-windowsCompare 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.

Move and manipulate Emacs frames

In X, an application running in an X client window of its own is normally called a window. But because Emacs has its own definition of the word window, as described in the previous section, Emacs uses another term for the whole Emacs X client window: It's called a frame.

Emacs supports multiple frames open from the same Emacs session. When you have a buffer open in more than one frame, changes appear in the buffer across all frames. Killing a frame doesn't affect other frames, but exiting a frame with the usual save-buffers-kill-emacs function (C-x C-c) saves buffers across all frames and exits all frames.

If you're in a console window, these commands still work, even though a console can display only one frame at a time. In the console, frames are differentiated by being given a frame number, which appears in the mode line preceded by an F character to distinguish each frame from any other.

Make a new frame

The make-frame-command function, C-x 5 2, makes a new frame and makes it active:

  1. Exit Emacs by typing C-x C-c, and then start it up with a single file (the new file you created):

    $ emacs new.experience
  2. Type C-x 5 2 to make a new frame. (Where exactly it appears on your desktop depends on your window manager.) It, too, contains a copy of the new.experience buffer.

  3. Edit a line: Type C-u 381 C-n M-f M-f M-f M-t. The asterisks appear in the mode line for this buffer in both frames.

Where the C-x 4 commands work on Emacs windows, the C-x 5 commands work on Emacs frames. All of the C-x 4 commands for making new windows as described in the Split a window vertically section have C-x 5 equivalents: The switch-to-buffer-other-frame function, for instance, is C-x 5 b.

Try it:

  1. Type C-x C-f experience Enter to open a copy of the experience file in a new buffer.

  2. Type C-x 5 b experience Enter to open a copy of this buffer in a new frame.

The find-file-other-frame function, which is bound to C-x 5 f, prompts for a filename and opens that given file in a new frame. Likewise, find-file-read-only-other-frame, which is bound to C-x 5 r, opens the given file as a read-only buffer in a new frame.

Try finding a file and opening it in a new frame with a single step: Type C-x 5 f innocence Enter.

Now your Emacs session should look like Figure 6, with four distinct X client windows.

Figure 6. Running multiple Emacs frames
Running multiple Emacs frames

Move between frames

Use the other-frame function, C-x 5 o, to move between frames.

Like its Emacs window equivalent, C-x o, this function cycles between all the current Emacs frames, raising each frame so it's in focus and above any other windows and selecting it as the current frame.

Try cycling between the four frames you have open: C-x 5 o C-x 5 o C-x 5 o C-x 5 o C-x 5 o.

Delete a frame

If you use the X controls to kill a frame or the C-x C-c command to exit Emacs, you exit all the frames you've made. The X control to destroy a frame, however, destroys just that particular frame; it doesn't kill any of the buffers open in that frame, nor does it destroy other Emacs frames.

You can also delete frames from Emacs. To delete the current frame, use the delete-frame function, C-x 5 0. After deleting the current frame, it makes the next frame the active and current frame. If you try running this on the only frame in your session, Emacs beeps and reports an error. Try it now to delete the frame you're in.

To delete all frames except the current frame, use the delete-other-frames function, C-x 5 1. All frames but the current frame, if they exist, are deleted. Try it now to delete the remaining two frames so that you're back to a single Emacs frame.

Note that these commands don't kill buffers—any buffers that were displayed in windows in deleted frames remain available in the current frame after the deletion.

Iconify a frame

In a console, C-z normally suspends Emacs in the background; in X, it runs the iconify-or-deiconify-frame function. This iconifies the current frame; but if the current frame is already iconified, it deiconifies the frame.

Try it: Type C-z in the current frame you have open. Depending on the version of X you're running and the window manager and desktop software on your system, the Emacs window should iconify. Then, press C-z again with the iconified frame in focus to deiconify it and bring the frame back into focus.

Table of Emacs frame commands

Table 3 contains a list of common Emacs commands for manipulating frames, giving their function names and their default key bindings, if applicable, and describing their function.

Table 3. Common Emacs frame-manipulation commands
FunctionBindingDescription
make-frame-commandC-x 5 2Make a new Emacs frame, and make it the active frame.
switch-to-buffer-other-frameC-x 5 bOpen a specified buffer in another frame. If no other frame exists, create a new frame.
find-file-other-frameC-x 5 fOpen a specified file in another frame. If no other frame exists, create a new frame.
find-file-read-only-other-frameC-x 5 rOpen a specified file in a read-only buffer in another frame. Create a new frame if no other frame exists.
other-frameC-x 5 oMove to the next frame, and make it the active frame.
delete-frameC-x 5 0Delete the current frame, and make the next frame the active frame.
delete-other-framesC-x 5 1Delete all frames but the current frame.
iconify-or-deiconify-frameC-zIconify the current frame. If the frame is already iconified, then deiconify it. (In a console, this binding suspends Emacs.)

Emacs windows and the mouse

Several useful techniques are available for using the mouse with Emacs windows.

The mouse on the mode line

You can use the mouse to perform many of the functions described in the Split and partition your Emacs session section. These window actions are done on the mode line.

In new versions of Emacs, special areas of the mode line have their own mouse bindings. For instance, if your mode line displays whether you have mail (it does so by writing the word Mail), then clicking that word on the mode line in certain ways with the mouse starts a new buffer for your mail; clicking the buffer name in the mode line switches the buffer in that window to the next buffer in your buffer list. These are called tooltips and are shown with a pop-up box that appears when you mouse over the particular area of the mode line. Aside from these special tooltips, the following commands work anywhere on the mode line.

Split windows with the mouse

The C-B2 combination clicked on the mode line splits the window horizontally at the place you click. If you're too close to either end of the window, the split is the minimal amount possible. (You make this combination by pressing and holding the Ctrl key and then clicking the middle mouse button.)

The C-B2 combination on the scrollbar splits the window vertically at the place you click. If you're too close to either end of the window, the split is the minimal amount. (Note that this currently doesn't work with some of the X toolkits that implement the scrollbar.)

Try splitting the window with the mouse:

  1. Exit Emacs, if it's running, by typing C-x C-c. Start it up again with one of the sample files:

    $ emacs innocence
  2. Open the second sample file in a new vertical window: Type C-x 4 f experience Enter.

  3. Your Emacs session should look like Figure 1. Try the C-B2 mouse combination in the middle of the top mode line (somewhere just after the word Top) to split the top window horizontally at that point.

  4. Split the bottom window horizontally at about the same point with a C-B2 mouse combination on its mode line—again, somewhere just after the word Top.

  5. Click and drag B1 on the small vertical bar beneath the scrollbars of the two new windows you just created to adjust them so that all four windows are approximately the same size. If your Emacs frame is too small to display the text in all four windows, you can adjust the size of its X client frame using your window manager controls.

Move to other windows with the mouse

You can move to any Emacs window with the mouse by clicking B1 (the first mouse button) on a blank area of the mode line of the window you want to move to. (In new versions of Emacs, clicking the buffer name changes the visible buffer in that window to the next buffer in the buffer list.) The window whose mode line you click becomes the active window, and the active cursor is moved to the current point in that window.

You can also use the mouse to move anywhere in the visible portion of another window: To do so, click B1 in the window. Point moves to the place where you click.

Try moving between the four windows you just created:

  1. Move to the upper-right window by clicking B1 in a blank area of its mode line, and then type C-u 349 C-n to move down in the file.

  2. Move back to the upper-left window by clicking B1 on any character in the visible buffer window, and then type C-s Till C-s C-s C-a to refocus the display elsewhere in the buffer.

  3. Take a break from the mouse, and move to the lower-left window by typing C-x o C-x o, as you learned in the Move to another window section. Type C-u 286 C-n to move down in the buffer.

  4. Click B1 somewhere just after the buffer name of the mode line on the lower-right window to make it the active window. Type C-s jour C-l to shift the view in this window to elsewhere in the buffer.

Resize windows with the mouse

You can also use the mouse on the mode line to resize windows. This is an alternative to what you learned previously in the Resize a window section.

To make a window taller or shorter, click and drag the mode line with B1. To make a window thinner or wider, click and drag the tiny vertical bar beneath its scrollbar.

Try adjusting the size of your windows with the mouse:

  1. Click and drag B1 and the tiny vertical bar beneath the scrollbar of the upper-right window just a bit, so the width of the upper-right window shrinks by a few characters and the upper-left window grows enough that all the characters on each line appear in the window of both buffers. (You might also have to grow the Emacs frame using your window manager's controls.)

  2. Make the two bottom windows smaller by clicking and dragging B1 on the mode line, so the bottom windows shrink by a few lines.

If you've followed all the steps in this section, your Emacs session should look something like Figure 7.

Figure 7. Making and resizing Emacs windows with the mouse
Making and resizing Emacs windows with the mouse

Delete windows with the mouse

To delete a window, click B3 on its mode line. Click B2 on a blank area of the mode line to kill all the other windows and make that window enlarge to fill the entire frame.

Get rid of some windows:

  1. Click B3 on the mode line of the upper-left window to delete the window. The upper-right window expands over to the left to fill the space; now this window is the active window.

  2. Click B2 on its mode line to delete the two windows beneath it and make this upper window fill the entire Emacs frame.

Mouse avoidance mode

Sometimes the mouse pointer in X gets in the way of your Emacs session—you select an Emacs frame and begin editing, and the mouse pointer is hovering somewhere over your text.

If you find this annoying, you can use mouse-avoidance-mode to change the behavior of the mouse pointer. This mode offers several techniques, as described in Table 4.

Table 4. Styles of Emacs mouse-pointer avoidance
ModeDescription
animateMake the mouse pointer move quickly away to a random position in the frame whenever the cursor gets close to it.
banishBanish the mouse pointer to the upper-right corner of the window as soon as you start typing.
cat-and-mouseSynonym for animate.
exileMove the mouse pointer to the upper-right corner of the window (like banish) only if the pointer gets too close to the cursor. Once the cursor moves away, bring the pointer back to its original position.
jumpMake the mouse pointer instantly jump to a random position in the frame whenever the cursor gets close to it.
noneNo mouse avoidance (the default).
proteusMove the mouse pointer as in animate, but like Proteus of Greek mythology, change the mouse pointer's shape (a random image character is used).

It's best with a demonstration. Try it now:

  1. Type M-x mouse-avoidance-mode Enter cat-and-mouse Enter to turn on mouse-avoidance mode.

  2. Move to the scratch buffer: Type C-x b *scratch* Enter.

  3. Move the mouse pointer so that it's just a few inches to the left of the cursor, and start typing a line of text: "When the cat wants to play the mouse runs away." The mouse pointer should scoot off as soon as the cursor gets near it.


Summary

Wrap-up

You've learned the ins and outs of manipulating the shape of your Emacs session—how to create and control multiple frames, how to split and partition any of these frames into multiple windows, and how to maneuver through frames and windows. You've also learned other tricks, including how to manipulate frames and windows with the mouse and how to deal with the mouse pointer—and these are all good, powerful techniques for making Emacs work (and look) the way you want it to.


Download

DescriptionNameSize
examples.tar.gzau-sample.zip13KB

Resources

Learn

Get products and technologies

  • GNU Project Web site: Download a free copy of the latest GNU Emacs for your OS and architecture free of charge.
  • IBM trial software: Build your next development project with software for download directly from developerWorks.

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

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

 


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

All information submitted is secure.

Choose your display name



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.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

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

 


All information submitted is secure.

Dig deeper into AIX and Unix on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=245702
ArticleTitle=Emacs editing environment, Part 5: Shape your Emacs view
publish-date=08072007