The emacs Editor

emacs is very powerful for program development and many other uses, it is widely available, and it allows the shell to be run within the editor, it will also allow you to compile. Let's do a simple "Hello World!" example.

emacs hello.cc &

Giving emacs a filename argument, such as hello.cc, causes emacs to load the given file for editing if it exists, or start a new buffer for creating the file if it does not exist. It will also run the right mode according to the extention. In this case C++.

You will now see a blank window with the cursor at the upper left corner and a banner at the bottom displaying emacs and some other information such as (C++), indicating that the editor is in its C++ mode.

Now type

// Hello World! example
//by me.
//Date

#include <iostream>

using namespace std;

int main(void)
{
  cout << "Hello, world!" << endl;
  return 0;
}

again and observe that when you close the curly braces, cursor bounces to show you the matching opening parentheses.

If you want to compile your program enter, on the command line,

g++ -g filename.cc

We shall next learn to use the editing commands of emacs. In C++ mode emacs bounces the cursor for parenthesis matching and some other tricks that are great for C++ programming, but not when you are typing prose (say in English). For that you want to be in text mode. We switch from the C++ mode to text mode by pressing ESC (the escape key), releasing it, then pressing x. Note that M-x appears at the left end of the banner at the bottom of the screen. Now type text-mode<CR>. In the future, typing such a sequence will be indicated by M-x text-mode. (M-x is read "meta x". On some keyboards M-x may also be entered by holding down a meta or alt key while pressing x.)

Notice that now the word (Text) appears in the banner at the bottom of the screen. When you call up emacs with a filename ending with the extension .txt, emacs goes directly into Text mode, whereas when the filename has the extension .cc, it automatically goes into C++ mode.

When entering emacs command names after the M-x (read "meta x") prompt, you can hit the space bar to cause emacs to show you all the current completions of the command you are entering. This can save you from typing the whole command.

Getting Help

The command C-h (control-h) invokes the emacs help facility. Type C-h again for a brief list of help options, or a third time for a full page description of help options. As always when emacs is prompting for more input at the bottom of the screen you can abort the command that is prompting for information by typing C-g.

Moving Around

Now type a few lines of text. To move the cursor up, press C-p, (p for "previous"). Repeat this until the cursor is on the first line of your text. Press C-n to move the cursor down again (n for "next"). Move the cursor up again to the first line of your text. If it is not at the beginning of the line, press C-a. Now press C-f and note that the cursor moves "forward" one space. Next press C-b, the cursor moves "backward" one space.

To move forward over one word, use the command M-f. (Recall this is read "meta f" and may be entered by pressing ESC and then f.) Next try M-b and watch the cursor move backward one word. You can abort a command or sequence of keystrokes with C-g. For example if you have typed ESC, but then decide that you want to move forward a character, enter C-g to abort the ESC keystroke, and then enter C-f. Try it.

Now press C-e. The cursor moves to the right end of the current line. Entering C-a moves it back to the beginning of the current line. Two additional commands that you will find useful are C-v which moves ahead to the next screen and M-v, which moves back to the previous screen. To go to the very end of the document, press M->, and to move to the very begnning, press M-<

Deleting Text

We saw that DEL deleted the character to the left of the cursor. Now press C-d and note that the character directly under the cursor is deleted. Retype the letter that was deleted. Now press C-k. This deletes (kills) whatever is to the right of the cursor on that line and stores it in a "kill buffer". If you press C-y, the last thing stored in the kill buffer is copied into the position starting at the cursor. Try it, you can also press C-y several times.

Saving a File

Observe that there are two asterisks (stars) at the left end of the banner. These indicate that the buffer you have been working in has new material that has not been saved to a file. To save what you have done in a file called mytext.txt, enter C-x C-w (control-x followed by control-w, the "w" stands for "write file"). You will see a prompt in a mini-buffer at the bottom left of the screen under the banner asking for the name of the file. Type mytext.txt and wait until a message is displayed in the mini-buffer indicating that the file has been written. After it has been written, the two asterisks disappear. Note that the filename with its extension appears in the banner line just after the word emacs:. If you want to save the buffer to the same filename, you can enter C-x C-w and then <CR> instead of typing a filename. You can also save a buffer to a file using C-x C-s, which saves the buffer to the current filename.

Inserting a File

The command C-x i is used to insert the contents of a file at the current buffer position. It prompts for the name of the file to insert using the mini-buffer, in the same manner as the C-x C-w command. Trying this now using the file you just saved will give you two copies of your material in the buffer.

Undoing Mistakes

If you make a mistake, such as accidentally deleting text, you can undo it with the command C-x u. This is particularly handy if you accidentally type a control-key combination that you have not learned and it does something completely unexpected!

Running the UNIX shell in emacs

You can run the UNIX shell within emacs directly with the command M-x shell. This creates a buffer in which the shell runs directly, and you can use shell just as you would outside of emacs.

Exiting emacs

To quit emacs, enter C-x C-c. If there are unsaved files or sub-processes (like the shell) running you will be prompted to decide what to do with them. Be careful to save any work you want to keep.

Below is a summary of the emacs commands used in this tutorial. You will have a chance to use them in the assignment which follows the summary.

Summary of Beginner emacs Commands

Keystrokes Effect
C-f Move cursor Forward one character. May use right arrow key.
C-b Move cursor Backward one character. May use left arrow key.
C-n Move cursor down to Next line. May use down arrow key.
C-p Move cursor up to Previous line. May use up arrow key.
DEL Delete character (or linebreak) before cursor.
C-x C-c Exits emacs (offers to save changes to file).
C-x u Undo the last text modification.
C-g Cancels the current action. (Use this if you ever get "stuck"
C-v Scroll down one screedful.
M-v Scroll up one screeful.
C-a Move cursor to beginning of line.
C-e Move cursor to End of line.
C-< Move cursor to beginning of the buffer.
C-> Move cursor to the end of the buffer.
M-q Fill Paragraph (wrap into as many lines as needed).
C-_ Undo (hold down to repeat)
C-d Delete the character at the cursor.
C-k Kill (cut) the rest of the line from the cursor to the end of the line. (repeat or hold down as needed).
C-y Yank (paste) killed text (then, M-y for prevoius killed text).
C-\ search forward (press twice to repeat last search). C-s is the standard way to search forward in emacs, but some terminals doesn't allow the use of C-s. (doesn't work always)
C-r search reverse (press twice to repeat last search)
C-space Set mark at cursor position
C-l Refreshes/redraw the screen, and move the window to be centered at the current cursor position.
C-w Kill (cut) selection (from last mark to current position)
C-x C-f Load a new file into the current buffer. Prompts for a file name.
C-x C-w Write the whole buffer to a file. Prompts for a file name.
C-x C-s Saved the whole buffer to a file. Does not prompts for a file name.
C-x i Insert file. Prompts for the file name.
M-x text-mode Enter text mode.
M-x c++-mode Enter C++ mode.
M-x shell Start a UNIX shell in an emacs window.

Intermediate

Now you're ready to take The emacs Tutorial to learn new tricks not mentioned here, using the on-line help system. Type: C-h t.

Note: It's easier to use the arrow keys to move the cursor; however, the standard emacs control keys are more universal, and guarnteed to work on all terminals.

Summary of Intermediate emacs Commands

C-h Enter the emacs help facility.
C-h t emacs on-line tutorial.

This page was adapted from http://daq-nt100.mps.ohio-state.edu/reu98/unix/emacs.html.