Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5:...

32
Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate Michael Stutz ([email protected]) Author Consultant 07 Aug 2007 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. Section 1. 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 Shape your Emacs view © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 32

Transcript of Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5:...

Page 1: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Emacs editing environment, Part 5: Shape yourEmacs viewGet going with this famous open source editor

Skill Level: Intermediate

Michael Stutz ([email protected])AuthorConsultant

07 Aug 2007

This tutorial, the fifth in a series, shows you how to manage and manipulate theshape your Emacs session—examine how to partition the Emacs screen, createmultiple X client windows for a single Emacs session, and display multiple buffers ineach window, dividing the screen with horizontal and vertical divisions. You also learnabout 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.

Section 1. 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 aroundthe 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 worklike other editors and word processors. But learning Emacs doesn't have to bedifficult. Once you get going, you'll see how intuitive it is and become morecomfortable with it after each use. This tutorial series (see Resources) shows youthe 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 powerfulediting features.

After completing this series (see Resources), you'll be able to comfortably use

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 32

Page 2: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Emacs for everyday editing, be well on your way to Emacs proficiency, and have agood feel for many of the advanced capabilities of Emacs.

About this tutorial

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

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

Objectives

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

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

Prerequisites

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

The special Emacs notation for representing keystrokes, which is used in this tutorialand throughout the series, is described in the introduction of the first tutorial of theseries, "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 haveat least a rudimentary understanding of the UNIX file system:

• Files

• Directories

• Permissions

• File system hierarchy

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 2 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 3: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

System requirements

This tutorial requires a user account on any UNIX-based system that has a recentcopy 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 snapshotsof version 22 are also available. This tutorial works with any of these versions forEmacs. 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 --versionflag:

$ emacs --versionGNU Emacs 22.0.91.1Copyright (C) 2006 Free Software Foundation, Inc.GNU Emacs comes with ABSOLUTELY NO WARRANTY.You may redistribute copies of Emacsunder 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 inthe X Window System environment, you should also have an X server up andrunning.

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

Section 2. Split and partition your Emacs session

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

The examples in this tutorial use a sample data set, which is available in acompressed archive file (see Downloads). This archive file contains a tar archivecontaining two plain text files, innocence and experience, whose contents are thecomplete text of William Blake's The Songs of Innocence and The Songs ofExperience, respectively—you'll recognize some of the text from previous tutorials.

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 32

Page 4: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 halfacross the middle of the screen. This action makes two new windows, each with itsown mode line and each approximately half the height of the original. You cuthorizontally across the Emacs screen, so the two new windows are stacked on topof each other in a vertical column; thus, this kind of window operation is called avertical split.

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

Try it with the sample files:

1. Start Emacs by typing emacs in the directory containing your copies ofthe 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 bufferyou opened) appears in both windows when the window splits. The bufferin 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

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 4 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 5: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Incidentally, this is the same two-buffer vertical split that Emacs does automaticallywhen you start it with two files as arguments, as described in the last tutorial in thisseries, "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 activewindow.

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

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

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 5 of 32

Page 6: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Your Emacs screen is now divided into three windows, and the middle window isactive. Type C-x 2 to split that middle window again, making a total of four windowsin the Emacs screen—and then type C-x 2 again to try to split the second, tinywindow. Depending on the size of your main Emacs X client window, this recursivesplitting of split windows comes to an end sooner or later, at the point when any newwindow 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 splitfurther. Your session should look like Figure 2.

Figure 2. Multiple Emacs vertical splits

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

Try it now:

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 6 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 7: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 bothwindows.

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. Youcan move through the buffer as it appears in the top window, but Emacsalways displays the end of the same buffer in the bottom window.

When you split a window, Emacs automatically determines the dimensions of thesplit—it usually cuts a buffer in half, so the two new buffers created in a vertical spliteach use exactly half the space of the original. But you can also specify the size, innumber of lines, that the top window of a vertical split should have (including itsmode 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 anegative 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

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 7 of 32

Page 8: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

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

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

Try opening the innocence file in a new buffer in the other window: Type C-x 4 finnocence 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 windoware 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 newother window, which also becomes the active window.

Try switching the buffer in the other window to the innocence buffer with thiscommand: Type C-x 4 b innocence Enter and notice that it doesn't changethe 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 thecurrent 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 currentwindow remains active. If the display has only a single window, this function splits itand creates a new window—but if the display already has two or more windows, anew split isn't made.

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

Move inside a window

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

To scroll the other window, not the window the cursor is in, use thescroll-other-window function, which is bound to C-M-v. (Emacs keeps all your

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 8 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 9: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

windows in an ordered list, so if you have more than two windows open, thiscommand scrolls the window that is next on the list.)

Try typing C-M-v now to scroll through the lower window with the experience bufferin 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 enablingscroll-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 windowsshow completely different portions of the buffer—the scroll commands you type areapplied to all windows containing that buffer.

Try it now:

1. Type C-x b experience Enter to switch to the experience buffer inthe 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 windowis still active.

4. Move the cursor up in both windows by pressing the up arrow key; pressit 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 notthe top.

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

Move to another window

To move between the windows, run the other-window function, C-x o, whichmoves 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 thatbuffer.

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

Table 1. Summary of Emacs windmove commandsFunction Description

windmove-up Move to the window directly above the currentwindow, if it exists.

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 9 of 32

Page 10: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

windmove-down Move to the window directly below the currentwindow, if it exists.

windmove-left Move to the window directly to the left of thecurrent window, if it exists.

windmove-right Move to the window directly to the right of thecurrent 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 scrolldown to "A Little Boy Lost" again, noticing that the top window no longerscrolls.

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 currentlyis—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-windowsfunction, 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 remainsopen in Emacs. To delete the current window and kill its buffer at the same time, runthe 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 twowindows in your Emacs session.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 10 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 11: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 hasa single window again.

Split a window horizontally

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

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

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 theinnocence buffer.

Your Emacs session should now look like Figure 3.

Figure 3. Sample files in horizontal Emacs windows

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 11 of 32

Page 12: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

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

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

Figure 4. Multiple horizontal windows in Emacs

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 12 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 13: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

As with the vertical splits of the same buffer, any horizontal splits of the same bufferare treated individually. Scrolling and cursor motions in one don't affect the other, sowhen you move the cursor in one window, it doesn't affect any other windowscontaining the same buffer. But you can use another trick. If you turn onfollow-mode, you can make a single large "virtual" window out of multiple windowsof the same buffer.

This way, when you split a window horizontally, the windows show different butconnected parts of the buffer, making one large virtual window of the buffer. Movingthe 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 singlewindow in half horizontally. Now your Emacs session has two windows,each containing the innocence buffer and each with the cursor at the topof the buffer.

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 13 of 32

Page 14: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

3. Move the cursor down through the buffer in the left window by pressingthe down arrow key, and watch what happens when you get to the bottomof 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 windowsare redrawn. Move the cursor up in the right window with the up arrowkey, and watch what happens when you get to the top of the window: Thecursor 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-xfollow-mode and notice that Follow no longer appears in the mode line.

Although follow-mode also works on buffers that are split vertically, using thismode 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 thetime 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 isbound to C-x ^. To make it shorter, precede this function with M--. To reduce orgrow the window by a specific number of lines, give that number (using the universalargument, 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 {. Tomake it wider, run the enlarge-window-horizontally function, which is boundto 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 innocenceEnter to bring the innocence buffer to this window.

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

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 14 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 15: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

4. Split the leftmost window in half vertically with the experience buffer in thebottom 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 typingC-s sunf to search for the title, and then type C-u 6 C-l to redraw thewindow 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 displaythem. 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 linesremain 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 toshrink the window further. Keep going until it disappears.

You can use a few more tweaks. To reduce the size of the window, if possible, runthe 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 thebalance-windows function. It makes all windows the approximate same size. It'sbound to C-x +.

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

In addition to using these commands, you can resize windows using the mouse, asyou'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

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 15 of 32

Page 16: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 bothwindows) to the first character that differs. If the files are identical, point is moved tothe end of both buffers.

Try running it now on the sample file called experience and the new.experience fileyou 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 bytyping 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 ofthe poem you altered earlier. Your Emacs session should look like Figure 5.

Figure 5. Comparing the contents of two Emacs windows

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 16 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 17: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 commandsFunction Binding Description

split-window-vertically C-x 2 Split the current window in halfacross the middle, stacking thenew buffers vertically.

switch-to-buffer-other-windowC-x 4 b Split the current window in halfvertically, prompting for thebuffer to use the bottom windowand making that the activewindow.

display-buffer C-x 4 C-o Display a buffer in anotherwindow, prompting for the bufferto use the other window butkeeping the current window

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 17 of 32

Page 18: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

active. (If only one windowexists, then split the windowvertically to display the otherbuffer.)

find-file-other-window C-x 4 f Open a new file in a new buffer,drawing it in a new verticalwindow.

find-file-read-only-other-windowC-x 4 r Open a new file in a newread-only buffer, drawing it in anew vertical window.

scroll-other-window C-M-v Scroll to the window that wouldbe the next one to switch to withC-x o.

scroll-all Toggle the scroll-all minormode. When it's on, all windowsdisplaying the buffer in thecurrent window are scrolledsimultaneously and in equal,relative amounts.

other-window C-x o Move the cursor to the nextwindow, and make it the activewindow.

windmove-up Move to the window directlyabove the current window, if itexists.

windmove-down Move to the window directlybelow the current window, if itexists.

windmove-left Move to the window directly tothe left of the current window, ifit exists.

windmove-right Move to the window directly tothe right of the current window,if it exists.

delete-window C-x 0 Delete the current window, andmove the cursor to the windowthat would be the next one toswitch to with C-x o.

delete-other-windows C-x 1 Delete all windows except thecurrent window.

kill-buffer-and-window C-x 4 0 Delete the current window, andkill its buffer.

split-window-horizontallyC-x 3 Split the current window in halfdown the middle, stacking thenew buffers horizontally.

follow-mode Toggle follow, a minor mode.When it's on in a buffer, allwindows displaying the bufferare connected into a largevirtual window.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 18 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 19: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

enlarge-window C-x ^ Make the current window tallerby a line; preceded by anegative, this makes the currentwindow shorter by a line.

shrink-window-horizontallyC-x } Make the current active windowthinner by a single column.

enlarge-window-horizontallyC-x { Make the current active windowwider by a single column.

shrink-window-if-larger-than-bufferC-x - Reduce the current activewindow to the smallest possiblesize for the buffer it contains.

balance-windows C-x + Balance the size of all windows,making them approximatelyequal.

compare-windows Compare the current windowwith the next window, beginningwith point in both windows andmoving point in both buffers tothe first character that differsuntil reaching the end of thebuffer.

Section 3. Move and manipulate Emacs frames

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

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

If you're in a console window, these commands still work, even though a consolecan display only one frame at a time. In the console, frames are differentiated bybeing given a frame number, which appears in the mode line preceded by an Fcharacter 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 itactive:

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 19 of 32

Page 20: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

$ emacs new.experience

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

3. Edit a line: Type C-u 381 C-n M-f M-f M-f M-t. The asterisksappear 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 workon Emacs frames. All of the C-x 4 commands for making new windows asdescribed in the Split a window vertically section have C-x 5 equivalents: Theswitch-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 experiencefile in a new buffer.

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

The find-file-other-frame function, which is bound to C-x 5 f, prompts fora 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 thegiven 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 finnocence Enter .

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

Figure 6. Running multiple Emacs frames

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 20 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 21: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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 thecurrent Emacs frames, raising each frame so it's in focus and above any otherwindows 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 oC-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, youexit all the frames you've made. The X control to destroy a frame, however, destroysjust that particular frame; it doesn't kill any of the buffers open in that frame, nor does

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 21 of 32

Page 22: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

it destroy other Emacs frames.

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

To delete all frames except the current frame, use the delete-other-framesfunction, C-x 5 1. All frames but the current frame, if they exist, are deleted. Try itnow 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 inwindows 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 theiconify-or-deiconify-frame function. This iconifies the current frame; but ifthe 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 Xyou're running and the window manager and desktop software on your system, theEmacs window should iconify. Then, press C-z again with the iconified frame infocus 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, givingtheir function names and their default key bindings, if applicable, and describing theirfunction.

Table 3. Common Emacs frame-manipulation commandsFunction Binding Description

make-frame-command C-x 5 2 Make a new Emacs frame, andmake it the active frame.

switch-to-buffer-other-frameC-x 5 b Open a specified buffer inanother frame. If no other frameexists, create a new frame.

find-file-other-frame C-x 5 f Open a specified file in anotherframe. If no other frame exists,create a new frame.

find-file-read-only-other-frameC-x 5 r Open a specified file in aread-only buffer in anotherframe. Create a new frame if noother frame exists.

other-frame C-x 5 o Move to the next frame, andmake it the active frame.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 22 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 23: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

delete-frame C-x 5 0 Delete the current frame, andmake the next frame the activeframe.

delete-other-frames C-x 5 1 Delete all frames but the currentframe.

iconify-or-deiconify-frameC-z Iconify the current frame. If theframe is already iconified, thendeiconify it. (In a console, thisbinding suspends Emacs.)

Section 4. 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 andpartition your Emacs session section. These window actions are done on the modeline.

In new versions of Emacs, special areas of the mode line have their own mousebindings. For instance, if your mode line displays whether you have mail (it does soby writing the word Mail), then clicking that word on the mode line in certain wayswith the mouse starts a new buffer for your mail; clicking the buffer name in themode 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 youmouse 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 theplace you click. If you're too close to either end of the window, the split is the minimalamount possible. (You make this combination by pressing and holding the Ctrl keyand then clicking the middle mouse button.)

The C-B2 combination on the scrollbar splits the window vertically at the place youclick. 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 thescrollbar.)

Try splitting the window with the mouse:

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 23 of 32

Page 24: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

$ emacs innocence

2. Open the second sample file in a new vertical window: Type C-x 4 fexperience Enter .

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

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

5. Click and drag B1 on the small vertical bar beneath the scrollbars of thetwo new windows you just created to adjust them so that all four windowsare approximately the same size. If your Emacs frame is too small todisplay the text in all four windows, you can adjust the size of its X clientframe 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 mousebutton) on a blank area of the mode line of the window you want to move to. (In newversions of Emacs, clicking the buffer name changes the visible buffer in thatwindow to the next buffer in the buffer list.) The window whose mode line you clickbecomes the active window, and the active cursor is moved to the current point inthat window.

You can also use the mouse to move anywhere in the visible portion of anotherwindow: 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 modeline, 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 thevisible buffer window, and then type C-s Till C-s C-s C-a torefocus the display elsewhere in the buffer.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 24 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 25: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

3. Take a break from the mouse, and move to the lower-left window bytyping C-x o C-x o, as you learned in the Move to another windowsection. 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 thelower-right window to make it the active window. Type C-s jour C-l toshift 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 analternative 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 awindow 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 theupper-right window just a bit, so the width of the upper-right windowshrinks by a few characters and the upper-left window grows enough thatall the characters on each line appear in the window of both buffers. (Youmight also have to grow the Emacs frame using your window manager'scontrols.)

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

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

Figure 7. Making and resizing Emacs windows with the mouse

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 25 of 32

Page 26: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Delete windows with the mouse

To delete a window, click B3 on its mode line. Click B2 on a blank area of the modeline 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 thiswindow is the active window.

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

Mouse avoidance mode

Sometimes the mouse pointer in X gets in the way of your Emacs session—youselect an Emacs frame and begin editing, and the mouse pointer is hoveringsomewhere over your text.

If you find this annoying, you can use mouse-avoidance-mode to change the

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 26 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 27: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

behavior of the mouse pointer. This mode offers several techniques, as described inTable 4.

Table 4. Styles of Emacs mouse-pointer avoidanceMode Description

animate Make the mouse pointer move quickly away to arandom position in the frame whenever thecursor gets close to it.

banish Banish the mouse pointer to the upper-rightcorner of the window as soon as you start typing.

cat-and-mouse Synonym for animate.

exile Move the mouse pointer to the upper-right cornerof the window (like banish) only if the pointergets too close to the cursor. Once the cursormoves away, bring the pointer back to its originalposition.

jump Make the mouse pointer instantly jump to arandom position in the frame whenever thecursor gets close to it.

none No mouse avoidance (the default).

proteus Move the mouse pointer as in animate, but likeProteus of Greek mythology, change the mousepointer's shape (a random image character isused).

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

1. Type M-x mouse-avoidance-mode Enter cat-and-mouse Enterto 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 thecursor, and start typing a line of text: "When the cat wants to playthe mouse runs away." The mouse pointer should scoot off as soonas the cursor gets near it.

Section 5. Summary

Wrap-up

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 27 of 32

Page 28: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

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

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 28 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 29: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Downloads

Description Name Size Download method

examples.tar.gz au-sample.zip 13KB HTTP

Information about download methods

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 29 of 32

Page 30: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Resources

Learn

• "Emacs editing environment": Check out other parts in this series.

• "Use free software within commercial UNIX" (David Dougall, developerWorks,February 2006): Learn how to install GNU Emacs on the IBM AIX® operatingsystem or another commercial UNIX.

• " Stopping "Apex Tracing Log" from appearing in Emacs windows: Read thisIBM technote for using IBM Rational Apex on UNIX with Emacs windows andframes.

• Emacs commands: The IBM DB2® Universal Database™ (UDB) InformationCenter has a quick summary of basic Emacs commands.

• GNU Project and the Free Software Foundation: Learn more about the GNUProject and the Free Software Foundation from its Web site.

• Check out other articles and tutorials written by Michael Stutz:

• AIX and UNIX zone

• Across developerWorks and IBM

• AIX and UNIX: The AIX and UNIX developerWorks zone provides a wealth ofinformation relating to all aspects of AIX systems administration and expandingyour UNIX skills.

• New to AIX and UNIX?: Visit the "New to AIX and UNIX" page to learn moreabout AIX and UNIX.

• Search the AIX and UNIX library by topic:

• System administration

• Application development

• Performance

• Porting

• Security

• Tips

• Tools and utilities

• Java™ technology

• Linux®

• Open source

• AIX 5L™ Wiki: A collaborative environment for technical information related toAIX.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 30 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 31: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

• Safari bookstore: Visit this e-reference library to find specific technicalresources.

• developerWorks technical events and webcasts: Stay current withdeveloperWorks technical events and webcasts.

• Podcasts: Tune in and catch up with IBM technical experts.

Get products and technologies

• GNU Project Web site: Download a free copy of the latest GNU Emacs for yourOS and architecture free of charge.

• IBM trial software: Build your next development project with software fordownload directly from developerWorks.

Discuss

• Participate in the developerWorks blogs and get involved in the developerWorkscommunity.

• Participate in the AIX and UNIX forums:

• AIX 5L—technical forum

• AIX for Developers Forum

• Cluster Systems Management

• IBM Support Assistant

• Performance Tools—technical

• Virtualization—technical

• More AIX and UNIX forums

About the author

Michael StutzMichael Stutz is author of The Linux Cookbook , which he also designed and typesetusing only open source software. His research interests include digital publishing andthe future of the book. He has used various UNIX operating systems for 20 years.You can reach Michael at [email protected]. (Be sure to include "developerWorks" in thesubject line.)

Trademarks

IBM, AIX, AIX 5L, and DB2 are registered trademarks of International BusinessMachines Corporation in the United States, other countries, or both.

ibm.com/developerWorks developerWorks®

Shape your Emacs view© Copyright IBM Corporation 1994, 2008. All rights reserved. Page 31 of 32

Page 32: Emacs editing environment, Part 5: Shape your Emacs view · Emacs editing environment, Part 5: Shape your Emacs view Get going with this famous open source editor Skill Level: Intermediate

Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in theUnited States, other countries, or both.Linux is a registered trademark of Linus Torvalds in the United States, othercountries, or both.UNIX is a registered trademark of The Open Group in the United States and othercountries.

developerWorks® ibm.com/developerWorks

Shape your Emacs viewPage 32 of 32 © Copyright IBM Corporation 1994, 2008. All rights reserved.