Выбрать главу

Now that you can create windows and can move between them, what else can you do? Practically anything. With our two windows on dickens open, one on top of the other. Initially, both of these windows are looking at the same file.

Type: C-x 2

Two windows open on dickens.

We can give any editing commands we want within either window. We can move back and forth in one window without affecting the other. Let's see what happens if we want to edit another file.

Type: C-x C-f blake

Now you have two windows, two buffers, and two files.

By using C-x o, we can edit one file and then the other. We can kill text from one buffer and yank it back in another. For example, let's move the first line of Blake's poem to the top of the dickens buffer.

Type: C-k C-k C-x o M-< C-y Enter

The Blake text has been yanked into the dickens buffer.

Editing with multiple buffers in separate windows is particularly useful if, for example, you want to copy material from one file to another or if you want to read a file containing reference material while editing another. Programmers often need to look at several different files at the same time—for example, a header file and a code file, or a function call site and the routine that's being called. Once you get used to the commands for moving between different windows, you may spend most of your time with two or three windows on your screen.

4.3.3 Getting Rid of Windows

Deleting a window only means that it isn't displayed anymore; it doesn't delete any of the information or any of your unsaved changes. The underlying buffer is still there, and you can switch to it using C-x b. To delete the window you're in, type C-x 0 (zero). If you want to delete all windows but the one you're working on, type C-x 1 (one), meaning "make this my one and only window." As you'd expect, the remaining window "grows" to fill up the rest of the space. You can also delete all windows on a certain buffer by typing: M-x delete-windows-on Enter buffername Enter.

4.4 Working with Frames

By now you know that Emacs calls GUI windows "frames." In this section, we'll cover how to create frames, navigate between frames, and delete frames.

4.4.1 Creating a New Frame

To open a new frame, type C-x 5 2 (for make-frame). Emacs makes a new frame containing the current buffer and puts it on top of the current frame.

If your new frame completely overlaps your current frame, you may need to size the new frame to tell them apart. For a more convenient solution, add these lines to your .emacs file:

(setq initial-frame-alist '((top . 10) (left . 30)

                            (width . 90) (height . 50)))

(setq default-frame-alist '((width . 80) (height . 45)))

These lines set up sizes for the width and height of Emacs frames. The first frame is the size set in initial-frame-alist (in this example, 90 characters wide by 50 lines high with top and left defining an inset), and subsequent frames, specified by default-frame-alist, will be 80 characters wide and 45 lines high. Depending on your display, you can make these numbers smaller or larger.

Here we edit a bit of Henry James.

Type: C-x 5 2

Emacs opens a new frame titled james

Frame Names

Note the title of your new frame. The first frame in your session, your initial Emacs frame, displays Emacs@system name at the top (or Emacs's best guess at the system name). Any other frames you create display the buffer name at the top. In fact, once you have multiple frames, all frames display the buffer name as their title. If you delete all frames but one, the title once again reverts to Emacs@system name.

Let's say we want to open a frame on our dickens buffer.

Type: C-x 5 f dickens Enter

Emacs opens a new frame on dickens.

If you type C-x b to move to another buffer, the name at the top of the frame changes to the new buffer's name (and on Linux, it shows the path as well). To move to a buffer and put it in a new frame, type C-x 5 b. You might have guessed that one.

4.4.2 Moving Between Frames

You can move between frames in several ways. You can use the mouse to select a frame or press C-x 5 o to go to another frame. To see a list of current frames, select Frames from the Buffers menu. (If you have only one frame, the Frames option does not appear on this menu.)

4.4.3 Deleting and Minimizing Frames

To get rid of a frame, press C-x 5 0. Emacs deletes the frame you are in. Deleting a frame, like deleting a window, affects only the display. The underlying buffer is still active, and you can move to it by typing C-x b.

If you try to use C-x 5 0 to delete the only frame that is left, Emacs won't do it. To exit Emacs, type C-x C-c or close the frame as you would any other GUI window using the mouse.

To minimize a frame, either minimize it in the usual way or press C-z. Table 4-1 summarizes the frame commands.

Table 4-1. Frame commands

Keystrokes Command name Action
C-x 5 o BuffersFrames other-frame Move to other frame.
C-x 5 0 FileDelete Frame delete-frame Delete current frame.
C-x 5 2 FileNew Frame make-frame Create a new frame on the current buffer.
C-x 5 f find-file-other-frame Find file in a new frame.
C-x 5 r find-file-read-only-other-frame Finds a file in a new frame, but it is read-only.
C-x 5 b switch-to-buffer-other-frame Make frame and display other buffer in it.