Emacs editing environment, Part 2: Learn theessential modes and editing features of EmacsGet going with this famous open source editor
Skill Level: Introductory
Michael Stutz (email@example.com)AuthorConsultant
10 Apr 2007
One of the powerhouses of UNIX computing, the open source Emacs editor is alarge, complex application that does everything from edit text to function as acomplete development environment. This tutorial, the second in a series, introducesyou to some of the essential concept of modes, shows you some of the powerful textmanipulation functions available, and teaches you how to use the built-in search,replace, and spellcheck facilities of Emacs.
Section 1. Before you start
Learn what to expect from this tutorial, and how to get the most out of it.
About this series
The Emacs editing environment is a favorite of UNIX developers. It's known aroundthe world as the king of editors, but many users find it has a bit of a learning curve.The Emacs environment doesn't seem intuitive at first glance, and it doesn't worklike other editors and word processors. But learning Emacs doesn't have to bedifficult. Once you get going, you'll see how intuitive it is and become morecomfortable with it after each use. This tutorial series shows you the way, taking youfrom the basics of Emacs, such as its features, philosophy, key-command layout,and methods for editing text, through many of its powerful editing features.
After completing this series, you'll be able to comfortably use Emacs for everydayediting, be well on your way to Emacs proficiency, and have a good feel for many of
Learn the essential modes and editing features of Emacs Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 31
the advanced capabilities of Emacs.
About this tutorial
This tutorial, the second in a series, demonstrates how to use some of the vitalfeatures for text editing and introduces the concept of modes, showing you what touse them for, how to invoke them, and which popular modes you're liable to use inyour normal editing. It also describes a special mode for defining abbreviations asuseful shorthand and explains how to use some of the text-editing features that workregardless of mode -- including important text manipulation commands, the searchand replace facility, and the built-in spell checker.
The primary objective of this tutorial is to take users who are already familiar with thebasics of the Emacs editor, such as its manner of keyboard input and the paradigmof buffers, and illustrate some of its essential but more intermediate features,including editing modes, incremental search, and other important Emacs textmanipulation commands and facilities.
After completing this tutorial, you will have a firm knowledge of the editing modes ofEmacs and how to utilize these various text-manipulation features in Emacs.
The only prerequisite for this tutorial is that you already have a basic understandingof Emacs, which you can gain by taking the first tutorial in this series.
Although this tutorial is written for all levels of UNIX expertise, it's helpful if you haveat least a rudimentary understanding of the UNIX filesystem:
This tutorial requires a user account on any UNIX-based system that has a recentcopy of Emacs installed.
There are several varieties of Emacs; the original and most popular is GNU Emacs,which is published online by the GNU Project (see Resources).
Learn the essential modes and editing features of EmacsPage 2 of 31 Copyright IBM Corporation 1994, 2008. All rights reserved.
You should have a recent copy of GNU Emacs -- one that is at version 20 or greater.Versions 20 and 21 are the most commonly available, and development snapshotsof version 22 are also available. This tutorial works with any of these versions forEmacs. If your system is running something older, it's time to upgrade.
To know what version of Emacs you have running, use the GNU-style --versionflag:
$ emacs --versionGNU Emacs 184.108.40.206Copyright (C) 2006 Free Software Foundation, Inc.GNU Emacs comes with ABSOLUTELY NO WARRANTY.You may redistribute copies of Emacsunder the terms of the GNU General Public License.For more information about these matters, see the file named COPYING.$
Section 2. Editing modes
Emacs is classified as a modeless editor, meaning that unlike editors such as vi,there is no special command mode for running editor commands or insert mode forinserting text into the buffer -- as you saw in the previous tutorial in this series, bothcommands and text insertion can be done at any time.
However, Emacs does have its own kind of editing modes, which are functions thatextend its capabilities or change the way some features work. Modes are generallywritten for editing a certain type or class of data, such as regular documents (writtenin any of the Indo-European languages), source code in a particular computerprogramming language (C, Fortran, Lisp, and so forth), text formatted a certain way(outlines, e-mail messages, usenet articles, character-based illustrations, and soforth) or in a markup language (Hypertext Markup Language (HTML), Nroff, TeX,and Extensible Markup Language (XML)). There's even a mode for editing non-text(binary) data. Additionally, many special modes for other kinds of data and systemprocesses are accessible through Emacs, including network connections andInternet Relay Chat (IRC), shell sessions, and the UNIX filesystem itself.
Modes are classified as either major or minor. The major mode dictates the mainediting behavior and is applied only to that buffer in the current editing session.Every buffer always has one, and only one, major mode active at any one time.
Although a buffer can only have one major mode active at any time, you can switchbetween major modes whenever you like. Specialized major modes offer extrafunctionality and aids (such as context highlighting and colorization) that can helpwhen you're editing certain kinds of documents, but you're not required to select aparticular mode to edit a certain kind of file or document -- a C program source codefile might be edited in any mode for editing text just as it might be done in the specialmode available for editing C programs.
Learn the essential modes and editing features of Emacs Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 31
Minor modes normally offer some feature or capability not associated with anyparticular major mode. Think of them as toggles that control features: Calling a minormode by its function name turns that minor mode on or off, and you can have manyminor modes turned on at any one time.
Minor modes include Overwrite mode (described in the first tutorial in this series),RCS mode for managing files checked into the revision control system (RCS), andAuto Fill mode to handle automatic word wrapping. All these minor modes and manyothers like them can be active at any one time.
This section of the tutorial shows you what you need to know to use the Emacsmodes successfully:
How to know which modes are active
How to get a description of the current modes and their capabilities
How to invoke a mode
Which modes you should know about
See which modes are active
As described in the first tutorial in this series, the highlighted bar near the bottom ofthe Emacs window, called the mode line, tells you all about the current buffer --including which modes are currently active. The current modes are indicated inparentheses toward the right side of the mode line. The abbreviated name of themajor mode is listed first, followed by the abbreviated names of any minor modes.
When you start Emacs with no files, you're in the scratch buffer. By default, thisbuffer is opened with Lisp Interaction mode, which is a special mode for theevaluation of Lisp code.
See for yourself by starting Emacs in the usual way, and look at what's written in themode line.
Any time you change modes, you'll see it reflected in the mode line. Try it now:Press the Ins key to turn on Overwrite mode, and notice how the mode linechanges. (The Ins key is bound to the overwrite-mode function).
Press Ins again to turn off Overwrite mode.
When a minor mode is enabled, it's normally indicated inside the parentheses, rightafter the major mode. However, not all minor modes have this indicator -- someminor modes are self-evident, such as Tool Bar mode, which displays the graphicaltool bar at the top of the Emacs frame. Other minor modes in newer versions ofEmacs are so minor (and always on) that it would only clutter the display to havethem all shown; for example, the purpose of the Unify 8859 On Encoding minormode is to provide an encoding unification for the various ISO 8859 character sets,which is useful for internationalization.
Learn the essential modes and editing features of EmacsPage 4 of 31 Copyright IBM Corporation 1994, 2008. All rights reserved.
Additionally, some modes offer extra indicators that appear in the mode line. LineNumber mode, for instance, is indicated by an L followed by t