Buffers and files
In this section, examine the most important Emacs buffer and file commands: how to load files into buffers, how to save buffers to files, how to switch between buffers, and how to "kill" them.
When you start Emacs in the usual way, it opens to a special buffer called the scratch buffer, whose purpose is described in a message at the top of the buffer:
;; This buffer is for notes you don't want to save, and for Lisp evaluation. ;; If you want to create a file, visit that file with C-x C-f, ;; then enter the text in that file's own buffer.
Toward the left side of the mode line is a place where the name of the current buffer is always displayed. Here, you see the name of this buffer, which is *scratch*. The special buffers that Emacs makes automatically have asterisks on either end of their names.
Try typing a line of text:
This is a practice file. and then press Enter to move the cursor down to a new line. As soon as you begin to type, you should see two asterisks appear at the left side of the mode line -- that means the current buffer has text in it that hasn't been saved to disk.
One way to make a new file is to write the scratch buffer to a file. The key combination
C-x C-s runs the
save-buffer command -- it saves the contents of the current buffer to a file. As you saw before, when you run it in a buffer that doesn't need saving, Emacs tells you as much. But when you run it in a buffer that has unsaved changes and the buffer corresponds to a particular file on disk, the contents of the buffer are written to that file. When you run it in a new buffer or in a buffer that isn't yet associated with a file (like the scratch buffer you're in now), Emacs prompts for the name of file you'd like to save the buffer in.
Try it now: Type
Ctrl-x Ctrl-s. See how in the minibuffer asks you to give the name of the file to save in, as in Figure 4. Now type
Figure 4. Saving the scratch buffer
After you press Enter, Emacs reports in the minibuffer that a new file named practice has been written to disk. Exit Emacs
C-x C-c, and then look in your directory to see your file there:
$ ls practice $
There's one more thing to remember about the scratch buffer. When no other buffers are open in Emacs, there's always a scratch buffer -- and when it's the only buffer open, it can't be closed.
To start Emacs with the contents of a given file in a new buffer for editing, give the name of the file as an argument. If you give multiple files, each file is opened in a buffer of its own. If any of the file names you give don't exist on disk, Emacs makes a new buffer for that file and indicates (on the mode line) that it's a new file. This is what you do when you start Emacs and edit a new file. When you save the buffer to disk, Emacs writes to the file whose name you gave as the argument.
Try starting Emacs with the name of your practice file:
$ emacs practice
You still see the Emacs welcome screen, but if you press a key like
C-g (or wait long enough), you see your file in the Emacs window. Yes, the welcome screen is another configurable option.
Emacs never works directly on the contents of files. It reads a copy of the file's contents into a buffer that you edit. Use
C-x C-f, the
find-file command, to open a buffer with the contents of a file.
Try opening your file with this command now:
C-x C-cto exit Emacs.
- Start Emacs again, but this time don't specify your file:
C-x C-fand give the name of your file (practice) when you're asked to do so in the minibuffer.
C-x b command switches from the current buffer to another buffer that you specify. A default option is always given in the minibuffer. If you press Enter without giving a buffer name, that's the buffer you switch to. The default is usually the last buffer you visited. If you haven't visited a previous buffer, the default is usually the scratch buffer.
C-x b now and notice how the scratch buffer is suggested in the minibuffer. Press Enter -- your practice file is gone and the familiar contents of the scratch buffer are in the window. (By default, the scratch buffer contains that three-line message, but sometimes the scratch buffer is empty.) Type
C-x b again, and press Enter to switch back to the practice buffer.
If you want to make a new buffer when you're in Emacs, switch to a buffer with the name you want to give it.
C-x b again, but give the name mybuffer for the buffer. Notice how the window is empty -- this is a brand new buffer. Type a line of text, and press Enter:
On what wings dare we aspire?
Save this buffer to disk by typing
C-xC-s. Notice how now Emacs asks you for a name of the file. When you make a new buffer that doesn't correspond to a file on disk, you have to also give it a file name if and when you decide to save it. You don't have to give it the same name as the buffer -- call it practice.b and notice how Emacs changes the name of the buffer to correspond with the new file.
C-x C-k command to kill a buffer or close it out of your Emacs session. You're prompted to type the name of the buffer to kill; the current buffer is the default and is killed if you press Enter.
Try killing your old practice buffer now: Type
C-x C-k and, when Emacs asks for a buffer name in the minibuffer, type
practice, and press Enter. The practice buffer is killed and the practice.b buffer remains in the window as before.
You already know how to save the contents of a buffer to disk by running the
save-buffer command. You need to know a few more things about this process.
The first few characters in the mode line describe the status of the buffer. Type another line of text in your practice.b buffer now and press Enter:
What the hand dare seize the fire?
Notice how two dashes in the mode line have turned into two asterisks. The straight dashes meant that the contents of the buffer were the same as what was on disk, and the asterisks indicate that the buffer has unsaved edits.
Save your changes and exit Emacs: Type
C-x C-s C-x C-c. You should have several new
files in your directory:
$ ls practice practice.b practice.b~
The practice and practice.b files are the ones you created, but practice.b~ is a file automatically created by Emacs. This is an Emacs backup file, which is created any time you edit a preexisting file in Emacs. When you save your edits to the file, Emacs makes a backup copy by writing to a new file whose name is the same as the original but with a tilde appended to it. The old practice file doesn't have a backup, because you never edited it after you created it. If you ever edit it again in Emacs, Emacs writes a corresponding practice~ backup file.
Emacs also writes another kind of file, called an autosave file, which has the same name as your file but with a pound sign before and after the name. Emacs writes an autosave file at set intervals for as long as you're working on a buffer. The default is to write an autosave after every 300 new characters. You normally never see the autosave file when you kill the buffer. This happens because the autosave file corresponding to that buffer is deleted. Autosave files are handy in case your system crashes or you lose the connection in the middle of an editing session -- this gives you the option of recovering your lost edits by looking for such a file in that directory.
Table 2 contains a list of some of the most common buffer and file commands that you'll use as you learn
Emacs. The table gives both the keystroke the command is bound to and the function name of the command. Remember that you can always run a command by using its key binding as well as by giving its function name as an argument to
M-x (see Learn to type Meta key combinations).
Table 2. Common Emacs buffer and file functions
||Save current buffer to disk.|
||Ask about saving all unsaved buffers to disk.|
||Ask about saving all unsaved buffers to disk and exit Emacs.|
||Suspend Emacs and make it a background process.|
||List all buffers.|
||Kill a buffer (the current buffer, by default).|
||Toggle read-only status on the current buffer (and perform version control if applicable).|
||Insert the contents of a file at point.|