Next Previous Contents

3. Emacs Modes

Emacs modes are different behaviors and features which you can turn on or off (or customize, of course) for use in different circumstances. Modes are what make one editor (Emacs) equally useful for writing documentation, programming in a variety of languages (C, C++, Perl, Python, Java, and many more), creating a home page, sending E-Mail, reading Usenet news, keeping track of your appointments, and even playing games.

Emacs modes are simply libraries of Lisp code that extend, modify, or enhance Emacs is some way.

3.1 Major vs. Minor Modes

There are fundamentally two types of modes available: Major and Minor. The distinction isn't the easiest thing to grasp until you've worked with a few of them off and on, but let's give it a shot.

Only one major mode can be active at a given time. Many minor modes can be active at a given time. Major modes tend to be language or task-specific, while minor modes are smaller and less specific utilities that cut across many tasks.

Sounds kind of abstract, so let's try an example. There's a mode that I use quite often when I'm writing plain old text files. It's called text-mode. This mode was designed for writing free form text like a README file. It understands how to identify words and paragraphs and generally makes sure that it does what I expect when I use the normal navigation keystrokes.

When I'm writing text for human consumption, I typically want it to look good. It should be properly word-wrapped to a reasonable value and so on. To enable word wrapping I just turn on the auto-fill minor mode. This mode tries to do the Right Thing when I'm typing along and hit the end of the line. The fact that it is a minor mode means that it can work with several different major modes. My notion of the ``Right Thing'' to do when I hit the end of the line is different when I'm in text-mode than it is when I'm in java-mode for example. I don't want my Java code to be word-wrapped as if was English text. But I do want the blocks of comments in my Java code to be word wrapped! auto-fill mode is smart enough to figure that out.

The authors of various Emacs modes have done a great job of making sure that things that should work as minor modes are minor modes.

If you look back at that ASCII sketch of an Emacs screen, you'll notice that the mode line identifies the mode(s) that Emacs is in. In that case it was in a mode called ``Lisp Interaction'' which is the default mode. It's really only useful if you're going to be writing Lisp code. (But since most of Emacs is written in Lisp, why not?)

3.2 Programming Modes

First and foremost, Emacs was designed by a programmer for programmers. There are high-quality modes available for almost every popular programming language you can think of (and even some not so popular ones). I only briefly describe a few of them here.

Most programming modes share some common characteristics. Usually, they'll do some or all of the following:

In addition, there are some non-language specific modes that help out with tasks that are common to programming in many languages. Things like interfacing to your version control software, automatically adding comments to your code, creating Makefiles, updating Change Logs and so on.

When you add all these modes together and consider the maturity and stability of the Emacs code, it compares quite nicely to commercially marketed Integrated Development Environments (IDEs) for languages like C++ and Java. And, of course, it's free.

C/C++/Java

Because the syntax of C, C++, and Java are quite similar, there is one Emacs mode which handles all three languages (as well as Objective-C and IDL). It's a very mature and complete package and it included in the Emacs distribution. This mode is called either cc-mode or CC Mode.

For more details or to download a newer version, visit http://cc-mode.sourceforge.net/.

Perl

There are actually two modes for editing Perl code in Emacs. The first is called perl-mode (as you would expect) and the second is cperl-mode. I don't have a good grasp of this history and why there are two modes (the docs don't say), but it would appear that perl-mode was the original mode for editing Perl code in Emacs. It seems to have fewer features than cperl-mode and is lacking the ability to recognize some of Perl's fancier language constructs.

Personally, I use and recommend cperl-mode which seems to be quite actively maintained and has just about every feature I could ever want. You can find the latest release here: ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs.

But don't take my word for it. Try them both and pick the one that best meets your needs.

Python

Python (another very popular scripting language) has an Emacs mode available for it as well. As far as I can tell, it is not distributed with GNU Emacs but it distributed with XEmacs. It works quite well in both editors, though.

You can get python-mode from the official Python web site http://www.python.org/emacs/python-mode/.

Others

There are many many other editing modes available to help out programmers. Such modes help out with things like:

And much more. See the last section of this document for more information on finding other modes and add-ins.

3.3 Authoring

Fancy Emacs modes are not limited to just those who write code. Folks writing documentation (of any sorts) can also benefit from a wide selection of Emacs modes.

Spell-Checking (ispell mode)

Authors of many types of documents need to spell-check once in a while. If you have GNU ispell installed, you can type M-x ispell and spell-check the current buffer. If ispell finds words that it doesn't know, it prompts you with a list of possible replacements and lets you select one (or none) of them. It's functionally equivalent to the spell-checkers in many popular non-free software packages.

HTML (html-helper mode)

If you find yourself writing HTML files once in a while (or even a lot), you might want to try out html-helper-mode. It is available from http://www.santafe.edu/~nelson/tools/ as is the documentation and related stuff.

As its name suggests, html-helper-mode provides lots of things to help out those folks who still write HTML by hand--the old fashioned way.

TeX (tex-mode)

When you're writing documents in TeX, it's often helpful to get Emacs to add some color and highlight the backslashes, braces and other characters. tex-mode takes care of that for you.

Though I don't write much directly in TeX anymore, when I did this mode proved to be quite helpful in making my TeX source a bit more readable.

SGML (sgml-mode)

The document you're now reading was written in SGML (and probably converted to the format you're reading it in). sgml-mode provides all the basics for SGML documents: validation, highlighting, forward-tag, backward-tag, and much more. It is a standard part of Emacs.

3.4 Other Modes

Of course, there are lots of other handy modes to make life easier. Here's just a sampling of the popular ones:

Version Control (vc mode)

vc mode interfaces with most of the popular version control back-ends (RCS, SCCS, CVS) to make it very easy to check files in and out, manage releases and so on. It is a standard part of Emacs and is documented in the Emacs documentation.

Shell Mode

Why switch to another X window or virtual console just to run a few shell commands? Do it from within Emacs and save yourself the trouble. :-)

M-x shell will launch a shell within an Emacs buffer. You can do most things with this buffer that you could do with a normal shell prompt (except for running full screen programs like vi or pine) because Emacs is talking to your real shell behind the scenes.

This is a standard part of Emacs, too, so you'll find it documented in the Emacs docs.

Telnet and FTP

Why switch to another X window or virtual console just to run telnet or FTP? Do it from within Emacs and save yourself the trouble. (Notice the pattern yet?)

Just like running a shell inside of Emacs, you can telnet and ftp. Try M-x telnet or M-x ftp to experience it for yourself. See the documentation for all the gory details.

Man

Why switch to another X window or virtual console just to read a manual page? Do it from within Emacs and save yourself the trouble. (I promise. I'll stop.)

Just like running a shell inside of Emacs, you can read manual pages. Try M-x man to experience it for yourself. See the documentation for more.

Ange-FTP

To quote the ange-ftp documentation:

This package attempts to make accessing files and directories using FTP from within GNU Emacs as simple and transparent as possible. A subset of the common file-handling routines are extended to interact with FTP.

That means you can treat files on remote machines as if there were local. So if you need to edit a file on a different computer, just tell Emacs to open it (using a slightly different path syntax) and it takes care of all the details of logging in and retrieving the file. Then, when you save the file via C-x C-s, ange-ftp intercepts the save and writes the file back to the remote machine.

The slightly different path syntax goes like this... A file named ``myfile'', in a ``user'''s directory, on a machine named ``my.host.org'' can be opened by opening (C-x f) the file:

/user@my.host.org:~user/myfile

This, also, is a standard part of the Emacs distribution so you can find it documented in the Emacs documentation.

Thanks to Etienne Grossmann ( etienne@anonimo.isr.ist.utl.pt) for the example above.


Next Previous Contents