Terminology

This chapter introduces the Emacs interface and some terms that have special meanings the the context of the editor.

Emacs is often launched when the user starts their work and is left running. The startup time is high compared to vim and so the latter is often started and closed repeatedly while Emacs is started once and run till the user finally decides to end the days work.

The command to launch Emacs is emacs. On Windows, the command is emacs.exe or runemacs.exe. This will launch your editor. There might be slight changes from the interface shown in the image below. Often, distributors and other package maintainers make slight modifications to Emacs to make it "fit" into their environment. However, the basic elements should be the same.

Parts of the interface

Here is an image of what the default Emacs interface looks like with the major parts labelled.

Parts of the Emacs interface
Parts of the Emacs interface

The menubar is something you find in most GUI applications. You can click on the various menu headings to get lists of operations. e.g. "Quit" under the "File" menu, "About Emacs" under the "Help" menu etc. The toolbar is also something commonly found. It offers a panel of buttons for common tasks like "help", "print" etc. The part of the interface which we've labelled "main window" is the main work area. Emacs maximises use of screen real estate. This area is where we'll be doing most of our editing. Emacs doesn't really have a name for this are. We've used "main window". The modeline is a status area of sorts which displays information about which line you're currently editing, the name of the file, what kind of file it is, whether there are unsaved changes in the file etc. The exact list of things that are shown there is customisable but for the time being, it sufficient that you know that the status bar is called the modeline. The minibuffer is a little area that Emacs uses to print status messages (e.g. "No such file" etc.). It is also used as an input area when you want to give some Emacs some input (e.g. the name of a file to open etc.). The scrollbar is something common. You use it to scroll up or down through you editor window. Finally, there is the fringe. This is the thin strip between the scrollbar and the main window. This are is often used to provide visual feedback when you have a long line of text that wraps into the next visual line or when you're interfacing with an editor and want to mark things like breakpoints in your source code.

I must emphasise here that most seasoned Emacs users turn off things like the menubar and toolbar. They're very rarely used and simply waste space. Many turn off the scrollbar too. I like to keep it on to give me a visual cue of where I am while editing a large file.

Windows, frames and buffers

When we use the term window, we often mean a top level graphical window with its own decorations, headings, buttons etc. In Emacs, however, this is referred to as a frame. You can click on the File menu and see a "New Frame" option. Selecting that will open a new top level window. Emacs calls this a frame. You now have a single Emacs running with two frames. You can select the "Delete frame" option in the File menu inside one of the frames to close it and get back to just one.

A side note on using the menus. To take full advantage of Emacs, you should familiarise yourself with the keyboard as much as possible and use it to do most operations. However, for the purpose of this chapter, I'm going to ask you to use the menus since they're simpler and we don't know any keys yet. Remember, however, that this is bad form.

You can split a frame into multiple windows where each can be doing a different thing. A window is a subdivision of a frame and will belong to only a single frame. When you created a new frame, you had two frames each with one window inside it. Now, Under the "File", select "Split window" and you can see that it splits the frame into two pieces. Each of these is a window. Both windows show the same content. You can repeat this to get as many windows as you want. Finally, select "Remove splits" in the "File" menu to get back a single window.

The third thing you need to know about are called buffers. A buffer is an object which Emacs uses to hold text that it's working on. Click on the "Buffers" menu and you'll see an option there called \*scratch\*. Click on that to switch to the scratch buffer. Now split the window and then create a new frame. Your setup should look something like this

Three windows, two frames and one buffer
Three windows, two frames and one buffer

Now, you're editing the same buffer but in three windows two of which are in one frame and the third is in another. Typing some text automatically updates all three windows since you're actually editing the same buffer.

You can see that the cursor is solid inside one of the buffers and this same one has a darker modeline to indicate that it's currently selected. If you type, that's the one that will get updated.

Buffers are often tied to files but not always. Emacs holds text in buffers when it operates on them. Since it's mostly used as a text editor, it holds file contents in buffers while you edit them. However, emacs also uses buffers to work on directories, interact with the shell and a lot of other things which we'll discuss in future chapters. Even the \*scratch\* buffer which we messed with earlier is not tied to a file.

In summary, when Emacs users say "create a new window", "close that buffer" or "why don't you open another frame?", you need to know what they're talking about.

Key sequences

Emacs, unlike vi, is a modeless editor. Most keys which you type while editing are directly entered into the focused window. If you want to enter a command (like "open file"), using the keyboard, you need to use a sequence of keys. This is a key sequence.

Key sequences in Emacs often start by pressing a key along with a modifier (like alt or control) and then pressing another. They're often mentioned in the literature and inside Emacs itself using a notation like this C-x 2 which means hold the control key (C) and hit x. Then let go of the control key and hit 2. This will run a command. Try this now and you'll see the that window has split into two. Some actions require you to hold the control key and pushing two or more keys. These will be written as C-x C-c. This means, hold the control button down and hit x and then c. This, incidentally, is the command to exit Emacs.

The Meta key (often mapped to Alt on modern keyboards) is another modifier. Emacs refers to it using M. If it asks you to hit M-f, it means you should hold down the Alt key and then hit f.

Let's look at a few sample key sequences. You can follow along as you read this. We already saw C-x 2 which splits the window. C-x 1 removes the splits. C-x 3 splits the window like earlier but it will do so horizontally instead of vertically. Try it and then remove the splits using C-x 1. Now try C-x 5 2. This will create a new frame. Then hit C-x 5 0 to delete the new frame.

So far so good. We've done all this using the mouse earlier. Let's try something else now. Hit C-x C-f. Emacs should prompt you with a Find File: in the minibuffer. See how it's used to communicate with you? You can type in the name of a file and Emacs will open it for you. Type ~/sample.txt and hit enter. The ~/ part might already be there in which case you don't need to retype it. In Emacs literature, the enter key is called "Return" and is represented as RET. If you started your Emacs from a directory other than the home directory, that one will show up there and you can delete it using the backspace key. It will open the file in your main window. Type something in there and then hit C-x C-s. That's the key sequence to save a file. If you look through the "Buffers" menu, you can see "sample.txt" there. Delete it using C-x k RET.

Another useful key sequence is C-h k. This will prompt you to enter a key which Emacs will describe for you. Try it on any of the keys we used earlier. Finally, C-g is quit. This is useful if, for example, you hit C-x C-f to open a file but then decide not to. It's a way of telling Emacs to stop and give you back control. And since we've come to the end of our little tour, you can hit C-x C-c to quit Emacs.