[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B. Emacs Quickstart

Emacs is a colossus: It is a plain text editor looking back on a long history, with a version number in the twentieth, a confusing abundance of functions and a stone age internal programming language called Emacs LISP. There are some expansions of the abbreviation "Emacs", e.g., Eight Megs And Constantly Swapping, or Escape-Meta-Alt-Control-Shift, making fun of its memory appetite or hotkey combinations.

The complexity of Emacs may deter the novice. But once you made your first steps and learned your first control sequences, you will realize how big the benefit of using Emacs is and how excellent support you can get by its various plug-ins. These packages make Emacs into a highly efficient tool for Java programming, LaTeX type setting or HTML editing.

Break the barrier now! Dare to dive into Emacs!

This short tutorial is meant to guide you in your first steps and give you all the knowledge to beat Notepad users in speed and safety.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.1 Starting Emacs, File Commands

Emacs exists in different layouts, we use xemacs. Just start it from the shell, optionally supply a file name. E.g.,

$ xemacs assignments/prop.tut &

You are probably used to the reappearing menu pattern from GUIs: File, Edit, View, Help. Emacs has a structure that looks similar in the beginning, but turns out to be badly organized--flooding you with a lot of irrelevant functions, but hiding the most basic editing commands like cut & paste from you. In Emacs you better not use the menus, but learn the control key sequences instead. This actually is to your benefit: You will find it much faster after some time.

The Emacs community has a special tradition how to name the key modifiers Control and Alternate:

Meta-x, i.e., Alternate-x or Escape-x

Here is a list of the basic file commands:

C-x C-f
Open... (Find file)
C-x C-s
C-x C-w
Save as... (Write file)
C-x s
Save all...
C-x k
Close... (Kill buffer)
C-x C-c
Exit (Close emacs)

Most interactive commands like "find file" or "write file" prompt for input in the so-called mini buffer, the last line of the emacs window. The minibuffer supports edit commands like cut & paste and, what is very comfortable, the file name completion known from Unix shells: Just press the tab key to complete a filename, and space to get a list of possible completions, from which you can select one with the mouse. Navigating through your file structure with the keyboard and completion is much faster than double-clicking through file dialogs. The minibuffer also facilitates a history: repeated up brings back the last inputs. Minibuffer commands:

Filename completion
List completions
History invokation

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.2 Editing Commands

Cursor movement and basic editing is almost identical to Windows editors. Here is a list of useful deleting commands:

Delete (to the right)
Delete word
Delete to the end of the line (Kill characters)

Essential for editing is cut & paste. All deleting commands that delete more than one character are indeed cut-commands. They accumulate the killed characters in the cut buffer until you move the cursor with other commands. If you start cutting more text then, the cut buffer will be emptied before accumulating the new characters. Another way to cut is to mark text and killing the selection, region in Emacs terminology. Here are the keys for cut & paste:

Start selection
Paste (yank)

Usually you will cut text by a sequence of C-k commands or by C-space, marking a region and C-w and then paste it back with C-y.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.2.1 Find and Replace

Find... (Search)
C-s C-s
Find again

Find and replace always start at the cursor position. If you want to move to the next occurence in a search, just hit C-s again.

After entering text and replacement you are prompted at each occurrence whether to replace or not. You have the options yes, no, ! to replace all and C-g to abort. (Abort and undo work in all contexts.)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.2.2 Indentation

Many Emacs plug-ins (modes in Emacs terminology) provide special indentation modules for the supported programming language. You invoke these modules by:

Indent line
Indent region

If you do not have a special Emacs mode for you file syntax, you have to indent manually. To indent a region, mark it and indent it with C-x tab. The region then becomes inactive, but you can reactivate it with M-C-z and indent it for one more column. A better way is to supply an argument to the indentation command and indent the region as far as you want in one step. You can provide arguments to commands by C-u <arg>. Examples:

C-u 4 C-x tab
Indent the region by four columns.
C-u -2 C-x tab
Unindent the region by two columns.

A good default indentation is provided in the "indented text mode". You can switch to it by invoking the command `indented-text-mode', which has no key sequence binding. Commands without a key sequence bound to them are launched by M-x <command>. Completion with tab also works for commands, therefore type:

M-x indented tab

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.3 Editing Several Files

Emacs is designed for convenient editing of several files at a time. It has a clean philosophy of "multi editing", by distinguishing buffers, frames and windows.

Data structure to hold a file to edit or a process to interact.

An Emacs "window" (in standard terminology) with title and menu bar. It contains one or more windows (in Emacs terminology).

A horizontal part of the frame (usually the whole free area or half of it) with a status bar displaying file name etc. A window displays one buffer. Several windows can display the same buffer.

Here is some commands to change the buffer displayed in the current window:

Switch back to the previously displayed buffer.
C-x b
Switch to entered buffer (completion with tab works)
C-x C-b
Display buffer list.
C-x k
Kill buffer.

You will find that you often need M-C-l. An alternative to C-x b is C-x C-f, since the latter does not load the file again if it already occupies one buffer. Window and frame commands:

C-x 2
Split window into two
C-x 1
Unsplit window (keep this)
C-x 0
Unsplit window (keep others)
Switch window
C-x 5 2
New frame
C-x 5 0
Delete frame

All these commands do not affect the buffers. Deleting a window does not kill the buffer.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.4 Modes and Customization

This is where the big potential of Emacs is present, and here it gets complicated. We focus on some very basic issues.

An Emacs mode is a plug-in written in Emacs LISP that controls the behaviour of the buffer it is attached to. Usually it provides indentation, syntax highlighting and hotkeys for compiling and jumping to error locations. Sophisticated modes turn Emacs into a full-fledged IDE (integrated development environment). Emacs decides on base of the file extension which editing mode is loaded.

Useful modes for editing Tutch files are the indented text, line and column number mode. They can be used in parallel and are invoked by

M-x indented-text-mode return
M-x line-number-mode return
M-x column-number-mode return

The indented text mode is one of the major modes, which are exclusive. The other two are minor modes which can be used as add-ons onto any major mode. Keys for the indented text mode are:

Indent line like the previous one.
C-e C-j
Start new line with indentation (use instead of return).

You can configure Emacs to load these modes automatically when loading `.tut'-files. Personal customization of Emacs is put into the file `~/.emacs'. Here are the necessary lines to add:

(command-execute 'line-number-mode)   
(command-execute 'column-number-mode)

(setq auto-mode-alist 
    '(("\\.tut$" . indented-text-mode)  
      ("\\.req$" . indented-text-mode))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B.5 More Editing Commands

Here is a random list of some useful commands:

Justify paragraph.
Transpose words.
M-- M-l
Make word lower case.
M-- M-u
Make word all upper case.
M-- M-c
Make word capital.

Here are more deleting commands:

Backspace word
Delete paragraph
Delete spaces
Delete spaces, leave one
Join two lines

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Andreas Abel on October, 24 2002 using texi2html