Emacs - "the God editor"

Why Emacs?

Emacs is quite simply the best editor for use on Unix-based operating systems. It's powerful and extremely versatile, but remains refreshingly user-friendly. Emacs was developed long, long ago by GNU software project founder Richard Stallman because he had "no interest in learning to use vi or ed" (see "The GNU Project"). For this (and many other reasons) Unix users everywhere owe Stallman a supreme debt of gratitude. Vi may be sleek and efficient but its command/insert mode interface is (for me, anyway) the most awkward, unintuitive editing environment imaginable. Combine that with no mouse support, the inability to simultaneously edit multiple files, and the lack of online help, and any fool can plainly see that Emacs is the superior choice (I can plainly see that!). Granted Emacs has a few superfluous features (was the built-in Rogerian therapist really necessary?), but these give Emacs character and can always be omitted from the application at compile-time. So remember, don't be scared off by mindless vi afficionados who condescendingly refer to Emacs as "a way of life." What the hell does that mean anyway?! Emacs is just a text editor - nothing more, nothing less. If you want an easy-to-use editor that has just about everything you could possibly want in an editor then Emacs is for you!

How to Learn Emacs

Okay, so I've convinced you that Emacs is the editor of champions and now you want to learn how to use it. First things first - start up Emacs by typing "emacs" at the Unix command prompt. If you're using X-Window, the Emacs window should pop up, otherwise your terminal will turn into an Emacs session. Now type "C-h t" ("Ctrl-h" followed by "t") or select "Emacs tutorial" from the Help menu in X-Window to start up the tutorial. Now the fun begins! Start reading through the tutorial and trying out the commands it describes. A pen and paper comes in handy here to write down the basic commands covered in the tutorial.

A word to the wise - try to wean yourself off of the arrow keys and onto the Emacs cursor movement keystrokes for moving around the screen. Unfortunately the Emacs key mappings are a little cumbersome and somewhat unnatural. C-f, C-b, C-n, and C-p move you right (forward), left (backward), down (next), and up (previous). (I prefer a geometrically-based keystroke layout, like the computer games I used to play on my old Apple II, e.g. i, j, k, l for up, left, down and right - much more intuitive. Even vi is less awkward than Emacs in this regard - h, j, k, l in command mode - but when I type "h" I expect an "h" to appear on the screen - I don't expect the cursor to move one space to the left!) Of course you can always redefine the key mappings, but the csh and tcsh shells also use them for command line manipulation in Emacs mode. So it's a good idea to get used to them - unless you want to write your own shell!

After you've finished going through the tutorial you should have a sheet listing basic Emacs commands and corresponding key mappings. What's that? You didn't bother to write them down? That's okay, here's my Emacs crib sheet (HTML) and the official Free Software Foundation Emacs reference card (postscript) with almost all the commands you'll ever need. Now all you have to do is use Emacs as much as possible and don't let your crib sheet out of sight until you memorize the most commonly used commands. It shouldn't take long provided that you consciously resist the urge to fire up pico whenever you want to edit something. Before long you'll be Emacs-ing like a seasoned veteran.

Customizing Emacs

Alright, now you're familiar with the basic commands that Emacs has to offer. But what about performing actions for which there are no built-in commands? That may require a little work on your part, but before you start hacking, it's always a good idea to check the documentation to see if a suitable command already exists. Type "C-h a" ("a" for "apropos") followed by a keyword which describes the type of action you want to perform, e.g. "delete", to search the Emacs command library. A list of relevant Emacs commands should appear in a separate window. Move the cursor onto the word "Command" beneath the Emacs command you want help on, and hit <Enter>. If you can't find a command that does what you want, you probably need to define a macro to get the job done. Macros are great, especially if you want to repeatedly execute a series of commands without the tedium of typing in each command individually. To record a macro, type "C-x (" and perform the sequence of commands you want to store in the macro, followed by "C-x )" to finish recording. To repeat the series of commands you just assigned to the macro, type "C-x e". To repeatedly call the most recently defined macro, give it a numeric argument by typing "C-#" where # is the number of times you wish to call the macro, followed by "C-x e". This works fine if you just have one macro to call at any point during your Emacs session, but if you need to have several macros defined simultaneously you'll need to assign a unique name and key binding to each one. You can assign a name to the macro you most recently defined with the aptly named "M-x name-last-kbd-macro" command (M is the keyboard-dependent Meta key, sometimes Alt). "M-x global-set-key" associates a named macro with the key binding of your choice. For the rest of your Emacs session (or until you use "M-x global-unset-key") the keystroke(s) you specified will execute the associated macro. Cool!

But wait, there's more... After programming several macros during an Emacs session, you'll undoubtedly want to save them for use in future sessions. Do this by opening up the ".emacs" initialization file in your home directory, and using "C-- 1 M-x insert-kbd-macro" (C-- 1 means Ctrl-minus 1) followed by the name of one of your user-defined macros to write the appropriate lines of Lisp code to the file. For non-Lisp programmers, this code can be fairly incomprehensible, so it's a good idea to insert a comment below the code for macro by typing two semi-colons, followed by a short description of the macro and its associated key binding. At startup Emacs will execute the instructions in the ".emacs" file, there by restoring all the macros you've saved there.

Programming in Emacs

Emacs really starts to shine when it's used as a programming environment. The text editor is a programmer's most important tool, and Emacs has many convenient features designed to "increase your productivity and enhance your programming experience" (or something like that). To start off, you can use "font-lock" mode to highlight (or "fontify") text with various font faces according to the syntax of the text, e.g. comments, names of user-defined functions, reserved keywords, etc. The easiest way to do this is to include these lines in your .emacs file. Now all your programs look "pretty"!

As for more substantial features, most depend on the particular language mode you're using. In Fortran mode, for example, there are handy features which allow you to comment-out ("C-c ;") or indent ("M-C-\") a region of your code defined between a mark (set with "C-SPC" or "C-@") and the cursor point. The command "C-x n n" allows you to "narrow-to-region", i.e. restrict editing to the currently defined region in that buffer (in Fortran mode, "C-x n d" narrows to the region defined by the subroutine in which the cursor point is located). This is especially useful for selectively replacing text with the "M-x replace-string" command. "C-x n w" removes narrowing and restores the rest of your buffer to visibility.

Finally, and perhaps most importantly, the "M-x compile" command allows you to specify a shell command (such as "g77" or "make") which compiles the code in the current buffer, and pipes the output the "*compilation*" buffer which appears in an adjacent window. "C-x `" then allows you to find the next error message in the "*compilation*" buffer and move to the source code that caused it. Talk about your time-saving features!

Emacs extras

Well that pretty much wraps up this brief introduction to Emacs. Needless to say, there are many, many more powerful commands which I haven't mentioned, but which are documented in the built-in help facility. Before I go, however, there are a few frivolous features that I just have to draw your attention to. The first is the completely useless, but utterly mesmerizing towers of hanoi diversion. Just type "M-x hanoi" and prepare to spent the next few hours of your life entranced by this fascinating puzzle with no solution. Note that on today's machines, the animation is almost too fast to follow with the human eye!

Next up is "M-x gomoku", a mind-numbing SOS-type game between you and Emacs. Not quite as engaging as "Minesweeper" perhaps, but just as addictive, I assure you. If you prefer more cerebral amusements, give "M-x dunnet" a try. It's a text-based adventure game in the grand old tradition of Zork. I haven't finished it yet, so if you manage to get through the whole thing, let me know how it turns out!

Finally, when your brain is completely fried after staring at the Emacs window too long...when hours of unsuccessful debugging have you tettering on the brink of insanity...relax with some soothing psychotherapy at the skilled hands of Emacs' built-in Rogerian therapist, "M-x doctor". Based on the famous Eliza program, the Emacs *doctor* is guaranteed to psychoanalyze all your troubles away...

Emacs Resources

Just remember - Emacs is like Othello: "a moment to learn, a lifetime to master."

Back to Main
Linux Page
Last modified: Tue Mar 30 13:17:02 2004