So far, we’ve learned how to open files, save files, and navigate around a file. But it’s pretty lame to only be able to work on one file at a time. So let’s look at how to work with multiple files in emacs.
Recall that emacs was created waaaay back in the almost prehistoric times before modern windowing interfaces. Why is this important? We’ll see shortly.
But first, lets talk about buffers. In emacs, each file you open is contained in a separate buffer. However, not every buffer contains a file. You can also have an unlimited number of scratch buffers, which are buffers that haven’t been saved to a file. Additionally, there are other specialized buffers, like the minibuffer, buffers that contain the emacs online help system, buffers that are attached to the input/output of a shell or other process, etc.
Buffers are displayed in windows and frames. And here is where the antique nature of emacs becomes important. Remember that emacs originally didn’t run in a windowing GUI. Instead, it ran full screen in a character mode terminal. So in emacs terminology opening a new window didn’t mean creating an overlapping, independent window like you’d expect in a modern GUI. Instead, it meant splitting the screen horizontally and/or vertically two or more times. Within each “window” you can either display separate buffers, or you can display multiple views into the same buffer. What everyone else in the world calls windows are called “frames” by emacs.
So now that we know what buffers, windows, and frames are (in emacs terminology at least), how do we use them? To begin, lets open the alice1.txt file that we worked with in the last tutorial. If you don’t still have the file saved somewhere, go ahead and download it from the link above. Open it in emacs (C-x C-f). Congratulations! you’ve just created a buffer. In a moment we’ll look at how to create more buffers, but first let’s look at how to work with windows.
Emacs Windows
It’s important to learn how to work with emacs windows, as many emacs command will open windows to display their output or to accept input or interaction beyond what can be done in the minibuffer.
The simplest way to create new emacs windows is to split the current window in half, either horizontally or vertically. The commands to do so are:
C-x 2: Split vertically
C-x 3: Split horizontally
When you split the current windows into two windows with C-x 2 or C-x 3, you see that each of the new windows contains a view into the same buffer (in this case, the buffer containing the alice1.txt file). You can scroll around in each of the windows independently, but because they are two windows into the same buffer and not two buffers, any change you make to the text in one window will also affect the text seen in the other windows. Go ahead and try it. Scroll the buffer in each of the two windows so that you’re looking at the same line of text. Then start typing. You’ll see the characters you type “echoed” in the opposite window. Pretty cool huh?
Now that you have two (or more) windows open, how do you switch between them? The command C-x o (that’s a lower case O, not a zero) moves the cursor to the Other window. Using C-x o repeatedly will cycle the cursor through all of the current windows.
To close the current window, type C-x 0 (zero this time, not O). To close all windows except the current one, type C-x 1.
Emacs Buffers
Of course opening multiple windows into the same buffer is only of limited utility. I’d be rather disappointed if that’s all we could do, but like most editors, emacs lets you do a lot more than that. You can open a new file, thereby creating new buffers, in either window, and you can switch which buffer is displayed in your current window.
To list what buffers are available, use the list buffers command, C-x C-b. This command will split open a new window. In that window is displayed the list of currently open buffers. It also displays some additional information about each buffer as well. The buffer list displays 6 columns. From left to right they are:
| Column |
Contents |
| M |
An asterisk (*) is displayed in this column if the buffer has been modified since it was last saved |
| R |
A percent sign (%) is displayed in this column if the buffer is read-only |
| Buffer |
The name of the buffer |
| Size |
Size of the buffer in Bytes |
| Mode |
The Major mode active in the buffer. We haven’t discussed modes yet, so don’t worry if this column doesn’t make any sense just yet. It will soon. |
| File |
The name of the file, if any, load into the buffer |
There are two ways you can switch to a different buffer. While the buffer list is displayed, you can move the cursor to the line of the buffer you want to switch to, then press enter. That will switch the buffer displayed in the window that the buffer list was previously displayed in. If you want to get rid of all other windows, hit C-x 1.
The other, quicker way to switch buffers is with the command C-x b. This command will prompt you in the minibuffer for the name of the buffer you want to switch to. You can type the complete buffer name, or you can type part of it and use the standard emacs tab completion.
You can also close a buffer (emacs calls it “killing” the buffer) with the command C-x k. If there are unsaved changes in the buffer you’ll be prompted to save the buffer first. If you want to save any unsaved changes in all open buffers, use the command C-x s.
Notice the similarity between the keystrokes for the switch buffers command (C-x b) and the list buffers commands (C-x C-b). Emacs is known for having a rather sharp learning curve, but these similarities between the default keybinds for related commands does help a little.
Emacs Frames
If you’re running a GUI version of emacs under X-Windows, MSWindows, or OSX, you also have the option of opening a new frame (which you’ll remember, is what’s called a window to anyone outside the emacs community!). The commands for working with frames are very similar to the command for working with windows. To create a new frame, use the command C-x 5 2. To delete the current frame, use C-x 5 0 (that’s zero, not O). To delete all but the current frame, use C-x 5 1.
These are not all of the emacs commands for working with buffers, windows, and frames., but these are the most common and basic ones. We’ll certainly learn about more of the others as we proceed in our shared journey to emacs mastery.
Speaking of our journey, lets review what we’ve learned so far in this series:
Working with files:
| Command |
What it does |
| C-x C-f |
Open (Find) a file |
| C-x C-s |
Save the current file |
| C-x C-w |
Save As |
| C-x s |
Save any unsaved files |
Cursor movement:
| Command |
What it does |
| C-f |
Move the cursor forward 1 character |
| C-b |
Move the cursor back 1 character |
| C-n |
Move the cursor to the next line (down) |
| C-p |
Move the cursor to the previous line (up) |
| M-f |
Move the cursor forward 1 word |
| M-b |
Move the cursor backward 1 word |
| C-a |
Move the cursor to the beginning of the line |
| C-e |
Move the cursor to the end of the line |
| M-a |
Move the cursor to the beginning of the paragraph |
| M-e |
Move the cursor to the end of the paragraph |
| C-v |
Move the cursor one page down |
| M-v |
Move the cursor one page up |
| M-< |
Move the cursor to the beginning of the file |
| M-> |
Move the cursor to the end of the file |
| C-l |
Recenter the screen around the current line |
Working with buffers:
| Command |
What it does |
| C-x C-b |
List buffers |
| C-x b |
Switch to named buffer (with tab completion) |
| C-x k |
Close (kill) the current buffer |
Working with windows:
| Command |
What it does |
| C-x 0 |
Close current window |
| C-x 1 |
Close all windows except the current one |
| C-x 2 |
Split current window in two vertically |
| C-x 3 |
Split current window in two horizontally |
| C-x o |
Switch to other window |
Working with frames:
| Command |
What it does |
| C-x 5 0 |
Close current frame |
| C-x 5 1 |
Close all frames except the current one |
| C-x 5 2 |
Create a new frame |
Misc:
| Command |
What it does |
| C-x C-c |
Exit Emacs |
That’s all for this installment. Join us next time when we’ll explore marking text, working with marked regions, searching, and how to get out of trouble if you accidentally hit the wrong key sequence!
______
Other installment in this series: part one, part two, part three, and part four.