Emacs buffers are associated with one so-called major mode and one or
more minor modes. Major modes typically denote a type of file, and are
associated with special command sequences and syntax highlighting just
for that file type. Minor modes change the behavior of an associated
major mode in small ways. For example, the major mode for editing text
is called text-mode, while the one for editing HTML is called
html-mode. Both text and html modes have a minor mode called
refill-mode that automatically formats paragraphs of text as you type
(similar to the behavior you expect from a word processor). You can
switch modes by just typing M-x modename
, so M-x html-mode
would
switch you into html-mode. Emacs can usually figure out the mode to
use by the file extension, so if you visit (load with C-x C-f
) a file
ending in .html or .htm, html-mode will be selected for you, and if
you visit a file ending in .txt, text mode will be selected.
As an example, say you want to create a new text document. You type
C-x C-f foo.txt
and Emacs creates an empty buffer for you named
"foo.txt". Notice the modeline displays "(Text)" to indicate the major
mode. Now type M-x refi
and hit the Tab key. Emacs will complete
"refill-mode". Hit enter, now the modeline displays "(Text Refill)" to
indicate the major and minor modes in effect. Experiment with refill
mode by typing some text and watch as Emacs wraps the lines for
you. Now go back and add or delete some text and notice how the
paragraph re-formats itself. Modes toggle on and off, so a second M-x
refill-mode
will turn refilling off. Type some text and notice Emacs
no longer wraps lines or formats paragraphs for you. The mode for
buffers not associated with any special file type is called
fundamental-mode. This is the mode your scratch buffer is put in at
startup.
There are lots of Emacs major modes, many of which are useful for
programmers. There is c-mode, perl-mode, and c++-mode, for
example. Each mode has its own key bindings and syntax highlighting
rules. You can see the details of the currently selected major mode by
typing C-h m
. To enable syntax highlighting on a source-code file,
you can type M-x global-font-lock-mode
(on some operating systems,
packaged versions of Emacs will enable font lock [syntax highlighting]
for you automatically).
As an example of special key bindings, in html-mode, the key sequence
C-c C-c h
will insert a properly formatted hyperlink into the text,
prompting you for the URL first. In other modes this key sequence will
have no effect.
Here are some of the most useful mode commands:
M-x modename | Toggle the given major or minor mode |
C-h m | Display help on the current major mode, including any special key bindings in effect |
M-q | Used in text-mode to reformat a paragraph of text manually |
M-x global-font-lock-mode | Toggle syntax highlighting |