3.2 Emacs

No discussion of VI or VIM is complete without also mentioning Emacs. VI and Emacs are the two most popular editors on the UNIX platform, and many heated debates have been held over which one is "best". Choose whichever one you feel most comfortable with, as being comfortable with the editor you use will help you code more efficiently.

Emacs is a text editor and more. At its core is a Lisp derivative with extensions to support text editing, called elisp. Just like VIM, it has syntax coloring, split windows, visual selection, a graphical user interface (with menus, mouse control, scrollbars, text selection, and the like), and much more. There are also a large number of extensions which add other functionality (most of which aren't installed on the lab machines). To edit a file with Emacs, type:

emacs [file...]

3.2.1 Entering Commands and Text

Unlike VIM, Emacs is not a mode-oriented editor. Insert mode is always active, and commands are triggered only by combinations of modifier keys (such as CTRL and ALT) and other keys. Sometimes a series of key combinations need to be entered to perform an action: for example, CTRL-x followed by CTRL-c exits the editor (and prompts for modified files to be saved).

3.2.2 Summary of Commands

Following is a summary of the commands Emacs accepts. To conserve space in this summary, CTRL is designated by "C", ALT is designated by "A", and META is designated by "M".

3.2.2.1 Moving Around the File

Pressing one of the arrow keys moves the cursor one position in the direction of the arrow. Pressing the PgUp key moves backward in the file by a full screen; pressing the PgDn key moves forward in the file by a full screen.

M-b -- Move cursor back one word
M-f -- Move cursor forward one word
C-a -- Move cursor to beginning of current line
C-e -- Move cursor to end of current line
   
M-< -- Move to beginning of file
M-> -- Move to end of file
M-x goto-line -- Prompt for a line number and move to it

3.2.2.2 Erasing Text

J -- Join next line to current line
DEL -- Delete one character backward
C-d -- Delete one character forward
M-d -- Delete one word forward
M-DEL -- Delete one word backward
C-a C-k C-k -- Delete current line
C-k -- Delete from cursor to end of line

3.2.2.3 Searching for (and Replacing) Text

C-s ABC -- Search for next occurrence of "ABC"
C-r ABC -- Search for previous occurrence of "ABC"
C-M-% regexp ABC -- Replace all text matching the regular expression regexp with ABC

3.2.2.4 Undoing Commands

C-x u -- Undo last command

3.2.2.5 Saving Files and Exiting Emacs

C-x C-s -- Save the current file without leaving (do this periodically)
C-x C-s C-x C-c -- Replace old file and exit Emacs
C-x C-c -- Exit Emacs. If the file has been modified and not subsequently saved, Emacs will prompt to save the file.