Emacs: the Free Software IDE

“Many people waited a long time to have nice IDEs for Linux. While everyone else waited I just used Emacs.” —Thanyne Harbaugh, President, Provo Linux Users Group

Emacs is well known as an editor, but calling Emacs an editor is like calling the Queen Mary a boat. The source RPM for Emacs 21.1 is some 20MB—huge for an editor. You don't have to use all of it, but sometimes it's nice to know it's there.

Emacs is heavily customizable, which makes it very flexible. For example, I am writing this article with Emacs and will spell check it with Emacs. “Well”, I hear you saying, “if it's good for writing articles, it can't be very good for developing software.” That's where Emacs' flexibility and customization come in; you can use it for either or for something completely different. Want to see a shrink, play Tetris or manipulate dates in the Mayan calendar? Emacs.

Emacs' customization comes in packages, called modes. Major modes set Emacs up to do a particular sort of thing, such as the C mode to edit C and C++ source or the psgml mode (www.lysator.liu.se/projects/about_psgml.html) to edit the documentation after you've written your program. Minor customizations provide added facilities to major modes. Show-paren mode shows matching parentheses, and auto-fill mode allows you to enter filled text without explicitly entering line breaks. Most modes have variables to control their behavior, and you can modify them temporarily or permanently.

Most users store their customizations in ~/.emacs, called the “dot Emacs” file. For the curious, mine is on the Web at w3.trib.com/~ccurley/emacs.init.html. Others can be found at “the very unofficial .emacs home”, www.dotemacs.de.

This article covers GNU Emacs version 21.1 (www.fsf.org/software/emacs/emacs.html). XEmacs (www.xemacs.org) is also available on most Linux distributions and will probably do everything I describe here.

A Brief Program

What I will do to show off Emacs as an integrated development environment (IDE) is walk through the development of a short C program and show you how to do it. We'll write the infamous Hello World program known and loved by C programmers everywhere.

While this article shows C development, Emacs has support for other languages as well, including C++, assembly, Scheme, Java, Ada, IDL, Makefiles, Lisp (including Emacs Lisp, the language in which much of Emacs is written) and FORTRAN. And that's just what comes with Emacs. Add-on packages abound; there is even one for Forth.

In addition, you can use Emacs for many other types of files. When it comes time to document your program, you can use Emacs' psgml mode to make editing the documentation source much easier. There are modes for HTML and TeX as well.

Directory Manipulation in Emacs

The first thing we do is use Emacs' dired mode to create a directory to put the program in. In dired mode, we type a +, and then enter the name of the directory to create in the minibuffer at the bottom of the editor (Figure 1). Press Return and Emacs creates the directory for us.

Figure 1. Using Emacs' dired mode to create a directory for the program—we've shrunk the window down to save space in the magazine, but the essentials are there.

The next thing we do is enter that directory, then create the file hello.c with the usual keystrokes for visiting a file, Ctrl-X Ctrl-F. Type the filename, hello.c, into the minibuffer and press Return. We now see Emacs ready with a new buffer for our program.

Entering the Source

The first thing any C programmer should do upon opening a new file is enter a comment to say what the file does. So we press M-; (M- for meta, or Alt on most PC keyboards) and get a C++-style comment delimiter and the cursor ready to take our comment. The reason I get a C++ comment is because I have customized Emacs to put me into C++ mode for C. You will probably get a C-style comment delimiter, /* */, with the cursor in the middle ready for you to type in your comment. If you'd rather use C++ comments, do M-X C++-mode.

Then we add a timestamp, all defined by Emacs' timestamp minor mode. Again, this is something I have customized; you may not get it to work.

A C program has to have a main function, and so we type it in GNU style: the return type on one line, followed by the function declaration on the next. Notice as we type away that syntax highlighting takes effect (Figure 2).

Figure 2. Entering the function declaration for main in Emacs. Note the syntax highlighting and also the parentheses matching.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Emacs 22 reference card

Marc's picture

An Emacs 22 reference card - PDF generated from the refcard.tex TeX source file that comes in the Emacs distribution.

A GUD (GDB) tip

Marc's picture

You forgot to mention in regards to GUD (GDB) that C-x SPC will set a breakpoint on a given line.