Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 6: Editing Several Files at Once

Previous HourNext Hour

Sections in this Hour:

 

Working with Windows and Frames


Often when you work with multiple buffers, you might find it handy to have several buffers on the screen at the same time. Examples of this include

Before I continue discussing various ways to use windows and frames, refresh your memory about what a window is and what a frame is:

Basic Window Movement: Creation and Deletion

Pressing C-x 2 (split-window-vertically) splits the current window in two with one above the other. Pressing C-x 3 (split-window-horizontally) likewise splits the window in two, but with one window next to the other. This can be seen in Figure 6.12 and 6.13.

Figure 6.12
Splitting the window in two with C-x 2 (split-window-vertically).

Figure 6.13
Splitting the window in two with C-x 3 (split-window-horizontally).

Each of the windows can further be split in two with any of the preceding commands. Two commands exist for removing a window: namely C-x 1 (delete-other-windows), which deletes all windows but the active one, and C-x 0 (delete-window), which deletes the active window.

In GNU Emacs it is also possible to use the mouse to create and delete windows. Pressing the second mouse button on the mode-line of a window deletes all the other windows of that frame, which is also what C-x 1 does. Likewise, pressing the third mouse button on the mode-line deletes the given window, which is equal to C-x 0. Finally, pressing Ctrl and the second mouse button on either the mode-line or the scrollbar splits the window at that point. (If your mouse has only two buttons, simply press both mouse buttons to obtain the functionality of the second one.)

In XEmacs you get a menu with the preceding functions if you press the third mouse button on the mode-line, as you can see in Figure 6.14.

Figure 6.14
Windows commands in XEmacs.

You can get from one window to another by pressing C-x o (other-window). In cases where you have several windows (as in Figure 6.15), C-x o moves forward, as if it were a page of a book that you were reading. If you want to go in the opposite direction, you need to give a negative prefix of one to C-x o. Press C-- 1 C-x o (that is, Ctrl-minus 1, Ctrl-x o).

Figure 6.15
Moving from one window to another using C-x o.

Tip - In sams-lib.el a function exists called sams-other-window-backwards, which does the same as C-x o with a negative prefix argument. Thus if you want to use Ctrl-+ and Ctrl-[ms] on the numeric keypad to move forward or backward, insert the following to your .emacs file:

(global-set-key [(control kp-add)] 'other-window)

(global-set-key [(control kp-subtract)] sams-other- window-backwards)

Resizing Windows

The windows can be resized vertically using the mouse, by dragging the mode-line with the first mouse button. Unfortunately it is not equally easy to resize the windows horizontally, because dragging the scrollbar scrolls the window. (It is not possible to define one function for dragging horizontally, and another for dragging vertically.) To resize horizontally you must press C-x { (shrink-window-horizontally) and C-x } (enlarge-window-horizontally). If you use these functions once in a while I suggest that you add them to your keyboard quick reference card, as the bindings are hard to remember and, even worse, hard to type many times after each other.

When resizing the windows, and when creating them, there is a lower limit for how small a window can get. The lower limit is controlled by the variables window-min-width, which is 10 by default, and window-min-height, which is 4 by default.

Truncated Lines

When splitting a window into two windows side by side, any lines longer than the width of the window are truncated (refer to Figure 6.13).

Note - The idea behind this is that when you split windows in two side by side, you often want to compare two files. By truncating the lines, the text shown on line 10 in both windows is the content of line 10 of the files. Otherwise the content of the tenth line of the screen might be some other line due to a wrapped line previous to this one.


In sams-lib.el a function called sams-toggle-truncate exists, which toggles between truncation and nontruncation for a single buffer.

When truncation is enabled you can scroll the buffer horizontally by pressing C-x > (scroll-right) and C-x < (scroll-left). In XEmacs horizontal scrollbars are added when necessary.

Tip - If you are short on screen height it might be desirable to not have the horizontal scrollbars in XEmacs. To disable them, insert the following line into your .emacs file:

(set-specifier scrollbar-height 0)

The Minibuffer

The minibuffer is a special window. It cannot be deleted, but you can resize it as you can other windows (refer to Figure 6.9). Its minimum height is 1. It is seldom useful to have it fill more onscreen space than necessary, so what you really need is to have it resize itself dynamically to use enough space to show all its content at any given time. To make it do that, insert the following code in your .emacs file:


(resize-minibuffer-mode)

Basic Frame Movement: Creation and Deletion

New frames are created either by any of the commands described in the next subsection or by pressing C-x 5 2 (make-frame-command). A frame is deleted again with C-x 5 0 (delete-frame).

Using the command make-frame-on-display, a frame can be created on another display. This makes it possible for two persons to work on the same buffer at the same time, each sitting at their computer. This function is not bound to any keys, but is accessible from the menus. It is located in the File(s) submenu, and is called Make New Display in GNU Emacs and Frame on Other Display in XEmacs.

Note - In Windows, make-frame-on-display is only an alias for make-frame-command, and does not make a frame on an X server.


Pressing C-x 5 o (other-frame) gives another frame the focus, in the same way that C-x o (other-window) gives another window focus. Iconified frames will, however, never get focus this way.

In the same way that you can give C-x o a negative numeric prefix to move in the other direction, so you can with frames. In sams-lib.el the function sams-other-frame-backwards exists, which works like C-x 5 o with a negative numeric argument. Thus you can bind Meta-+ and Meta-- on the numeric keypad to move between frames by inserting the following into your .emacs file:


(global-set-key [(meta kp-subtract)] 'sams-other-frame-backwards) 
(global-set-key [(meta kp-add)] 'other-frame) 

Note - Earlier I suggested that you bind Ctrl-+ and Ctrl-[ms] to switch between windows, so Meta-+ and Meta-[ms] is an obvious choice for switching between frames. Remember Meta works on larger entities than control does, whenever possible. (See Hour 3.)

Extra Window and Frame Commands

Some additional commands exist with the keybinding prefixes C-x 4 and C-x 5. Those with prefix C-x 4 operate on another window in the same frame as your current buffer, whereas those with prefix C-x 5 operates on another frame . The bindings resemble those for C-x; C-x b switches buffers in the current window, whereas C-x 4 b switches buffers in another window, and C-x 5 b switches buffers in another frame, for example. To see a complete list of these commands, press C-x 4 C-h and C-x 5 C-h, respectively.

Dedicated Frames

It is possible in Emacs to make frames which are dedicated to a single buffer; that is, only this buffer is allowed in the frame and, when the buffer is killed, the frame is killed too. This can be very useful if you want to ensure that a file stays visible in a frame all the time.

Two functions exist in the sams-lib.el that make it possible to create such dedicated frames:

Emacs can also place buffers in dedicated frames itself. This can be useful for the buffer containing help or info files. To tell Emacs which buffers it must create dedicated frames for, insert something such as the following into your .emacs file.


(setq special-display-buffer-names '("*Help*" "*Apropos*" 
                                     "*compilation*" "*grep*" "*igrep*")) 

Caution - Be very careful with the syntax of the preceding! If you have patience, you will learn an easier way to configure variables in Hour 10, "The Emacs Help System and Configuration System."


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 6: Editing Several Files at Once

Previous HourNext Hour

Sections in this Hour: