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.
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.
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:
|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.
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|
|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|
|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.
Great Series! Very beneficial. Keep it up!
I’m currently taking a college course into C/C++ programming and we began with Emacs. Previously I programmed mainly with Java which is object-oriented programming, so this this is a welcome and useful change, closer to machine language somewhat.
You seem very knowledgeable so could you help me out with a problem that I have.
After installing Cygwin, I tried to install emacs and its libraries on my Windows Vista. During the Running Programs stage, after both downloading and installation stages were complete, the cygwin setup froze at 99% on /etc/postinstall/post-texmf.sh
I ran Windows Task Manager to see if the program was responding at all or not, and it was running and responding, but the .sh executable kept on droning on under my Processes tab. After about 10 mins stuck at 99%, I stopped the .sh executable, and then just pressed Cancel for the setup. Oddly enough, emacs was successfully installed and working. Is it just a Windows Vista issue or something else entirely?
Thanks for the nice comment. I really appreciate it. This series of emacs articles has been on hiatus for a while, but I do intend to continue it. I’ve been a bit too tied up *using* emacs lately to write about it. 🙂
I which I had an answer for your install question, but I really don’t. I honestly have never even run Windows Vista yet.
I do think that from the description of your issue, it sounds like it’s more of a Cygwin issue than an Emacs issue. I’d suggest doing a search of the Cygwin mailing lists (http://www.cygwin.com/lists.html), and if you don’t find anything consider posting a question to one of the Cygwin mailing lists.
BTW you mentioned doing some Java coding… Have you seen the Emacs Java environment? (http://jdee.sunsite.dk/) Also, you can apparently use emacs key bindings in Netbeans (http://www.java-tips.org/other-api-tips/netbeans/how-to-use-emacs-key-bindings-in-netbeans.html)
Anyway, good luck with your Cygwin issue, if you find a solution please let me know.
I am an old w32 user, who wants to acquire Linux skills (in particular do Linux kernel device driver development). I am presently studying Linux aggressively. One of the first requirements is a good text editor, that is well mastered. I am taking the Emacs tutorial and learning it. You comments about buffers, windows, and frames is interesting.
The matter of overlapping windows seems to be important. Emacs has a very rich history, and installed and ran well for my test. I am an old Multi-Edit user, and overlapping windows (for different file buffers) is very natural to me. We tend to have paradigms such that we want certain things to not change. This is NOT my intention.
Sometimes I may have 20 (or more) open overlapping windows (many of the files for a project). This working environment is very productive. Using Emacs frames, I can not see how to emulate this important productivity.
Can you comment about overlapping windows not being part of Emacs? There may be another Linux editor that uses overlapping windows (ok), but Emacs seems to be the most popular, so I would like to master and use it.
thanks, …Jim Hughen
Jim, regarding your question about overlapping windows. I definitely understand where you’re coming from in terms of difficulty getting used to the way Emacs handles window management (or I guess I should say, buffer management in Emacs parlance). It took me a while to get use to as well, but in the end I realized I’d been fighting it needlessly and that the Emacs way really isn’t that bad.
IMHO, the key to getting use to it is to *ignore* emacs frames (seperate OS level windows). Instead, I maximize my Emacs frame to full screen. Then, while working I either use emacs “windows” (the non-overlapping “splits” emacs uses for internal windowing), or I just switch buffers using C-b (control-b).
What I eventually realized is that this way of working is not that different from how, in practice, I’d usually work in a Windows or Gnome application. Although in theory I could use overlapping windows in these applications, in practice if I was doing anything important, I’d maximize all my major work windows to full screen anyway.
One last thing to mention. Emacs is an odd program and is a little difficult to learn. For myself, I think it’s been worth learning for the power and portability of Emacs, but your mileage may vary. If you’d prefer a more conventional editor, there are a lot of good ones out there for Linux (Eclipse and Netbeans come to mind, but I’m certain there are a lot more).
Jim, you should look at ido mode : http://www.emacswiki.org/emacs/InteractivelyDoThings
Another must for this sort of task (managing many buffers in different configurations) is winner-mode : http://www.emacswiki.org/emacs/WinnerMode . Using this you can set up some windows the way you want them, e.g.
| | |
| | |
and ‘remember’ several such arrangements, switching back and forwards between them (a bit like a ‘profile’ in Eclipse if memory serves). Handy when programming.
Hello, I like your tutorial but I am a little confused and frustrated by one thing: when you open a file such as alice1.txt and then C-x 2 or C-x 3 to split the window (open a new frame), you are viewing the same buffer (alice1.txt) in both frames, which doesn’t seem useful. How would you make the 2nd frame view a NEW buffer (for example, open another file?). Thanks.
Reblogged this on ljklonepiece and commented: