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 for inserting text into the buffer -- as you saw in the previous tutorial in this series, both commands and text insertion can be done at any time.
However, Emacs does have its own kind of editing modes, which are functions that extend its capabilities or change the way some features work. Modes are generally written for editing a certain type or class of data, such as regular documents (written in any of the Indo-European languages), source code in a particular computer programming language (C, Fortran, Lisp, and so forth), text formatted a certain way (outlines, e-mail messages, usenet articles, character-based illustrations, and so forth) 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 system processes are accessible through Emacs, including network connections and Internet Relay Chat (IRC), shell sessions, and the UNIX filesystem itself.
Modes are classified as either major or minor. The major mode dictates the main editing 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 switch between major modes whenever you like. Specialized major modes offer extra functionality and aids (such as context highlighting and colorization) that can help when you're editing certain kinds of documents, but you're not required to select a particular mode to edit a certain kind of file or document -- a C program source code file might be edited in any mode for editing text just as it might be done in the special mode available for editing C programs.
Minor modes normally offer some feature or capability not associated with any particular major mode. Think of them as toggles that control features: Calling a minor mode by its function name turns that minor mode on or off, and you can have many minor 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), and Auto Fill mode to handle automatic word wrapping. All these minor modes and many others like them can be active at any one time.
This section of the tutorial shows you what you need to know to use the Emacs modes 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
As described in the first tutorial in this series, the highlighted bar near the bottom of the Emacs window, called the mode line, tells you all about the current buffer -- including which modes are currently active. The current modes are indicated in parentheses toward the right side of the mode line. The abbreviated name of the major 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, this buffer is opened with Lisp Interaction mode, which is a special mode for the evaluation of Lisp code.
See for yourself by starting Emacs in the usual way, and look at what's written in the mode 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
line changes. (The Ins key is bound to the
Press Ins again to turn off Overwrite mode.
When a minor mode is enabled, it's normally indicated inside the parentheses, right after the major mode. However, not all minor modes have this indicator -- some minor modes are self-evident, such as Tool Bar mode, which displays the graphical tool bar at the top of the Emacs frame. Other minor modes in newer versions of Emacs are so minor (and always on) that it would only clutter the display to have them all shown; for example, the purpose of the Unify 8859 On Encoding minor mode is to provide an encoding unification for the various ISO 8859 character sets, which is useful for internationalization.
Additionally, some modes offer extra indicators that appear in the mode line. Line Number mode, for instance, is indicated by an L followed by the line number in the buffer where the cursor is currently located.
describe-mode function, which is bound to
C-h m, to get a description of the current mode. When
you run this function, a new help buffer opens that lists all the key bindings
that are special to the current major mode in the buffer you typed it in
followed by any bindings that apply to any of the minor modes that are on.
Try it now by typing
C-h m, as in
Figure 1. List the key bindings applicable to current Emacs modes
As you can see, one of the special bindings for Lisp Interaction mode is the Tab key. In this mode, Tab doesn't move the cursor to the next tab stop as you'd expect on a typewriter or in a word-processor application; as the mode description says, it indents the current line of Lisp code. Because you haven't written any Lisp code in this buffer, Tab does nothing -- try it and see.
Although the scratch buffer is usually set to Lisp Interaction mode, that's not
the default Emacs mode. To find out what is, switch to a new buffer: Type
C-x b and give the name lamb.txt as the name of the
buffer. Get rid of the help buffer by typing
You'll see that your new lamb.txt buffer has a new major mode, Fundamental. This is the default mode for Emacs buffers. It's the most plain and simple of all Emacs modes, with the fewest special key bindings and settings. Here, the Tab key works as you'd expect it to.
Go ahead and get a description of the modes by typing
You'll see the following for the description of Fundamental mode:
Fundamental mode: Major mode not specialized for anything in particular. Other major modes are defined by comparison with this one.
C-x 1 again to get rid of the help window.
When you open the contents of an existing file in a new buffer, Emacs selects a mode for you based on the file type. If you open a file containing C program source code, C mode will be major mode; and, if you open a file containing text in a spoken language such as English, Emacs sets Text mode as the major mode.
You can always change the mode, and you can configure all these settings so that certain files or certain types of buffers always open in a particular mode. Table 1 describes popular Emacs modes and gives their function names.
Table 1. Popular Emacs modes
||Major||This mode is the default Emacs mode with minimal settings and bindings.|
||Major||This mode is the basic mode for editing text.|
||Minor||This mode is for making and using abbreviations (see Abbrev mode).|
||Minor||This mode is for automatic word wrap and filling long lines and paragraphs.|
||Minor||This mode is for overwriting any existing text in a buffer instead of inserting text at point. It's bound by default to the Ins key.|
||Major||This mode is for editing C program source code.|
||Minor||This mode is for displaying the current line number.|
||Major||This mode is for editing and compiling Lisp code.|
||Major||This mode is a special variation of the Text mode where the paragraph-movement commands work for paragraphs whose first lines are indented, and not just for paragraphs separated by blank lines.|
||Major||This mode is for editing TeX documents.|
||Major||This special mode provides the key bindings of the WordStar editor.|
Emacs modes are functions. To invoke one, you type
M-x and then give the name of the mode.
Try invoking Text mode in your new buffer now: Type
M-x text-mode and press Enter. You'll
immediately see the change in the mode line, where Fundamental is replaced by
Text mode is a basic mode with few changes from the Fundamental mode, so its benefits are subtle; but it's a good base for editing text in spoken languages, such as English. Many of the special modes for editing certain kinds of text or documents from TeX mode to Outline mode are based on Text mode.
Try typing some text in your new buffer, continuing the theme of William Blake from the last tutorial:
Little lamb, who made thee? Dost thou know who made thee?
In Text mode, the Tab key is defined to indent paragraphs relative to the previous indentations of the paragraph, if any; if the previous paragraph isn't indented, pressing Tab inserts a literal tab character, moving point to the next tab stop.
To see how Text mode handles tabs, type a few lines with tabs and spaces:
Type two spaces on the next new line in your buffer, type the text
Gave thee life, and baid thee feed, and press Enter to end this new paragraph.
Press Tab -- notice how point is lined up with the line (paragraph) above it -- type
By the stream and o'er the mead;, and press Enter.
Type a new line with no indentation:
Gave thee something of delight,and press Enter
Press Tab. Notice how you're shifted out many spaces -- a literal tab character has been inserted. Type the line
Softest something, woolly, bright;and press Enter.
Gave thee such a tender voice,and press Enter.
Press Tab. Another tab character is inserted. Type the line
Making all the vales rejoice?
Your buffer should now look like Figure 2.
Figure 2. Type tabs in Text mode
Table 2 shows the key bindings that are set by Text mode.
Table 2. Text mode key bindings
|Key||Description or function|
|Esc||Prefix for |
|Esc Tab, M-Tab||
|Esc S, M-S||
|Esc s, M-s||