"Linux Gazette...making Linux just a little more fun!"


Basic Emacs

By Paul Anderson


Emacs is, by nature, a very difficult program to use. Few people can even figure out how to exit it, let alone use it. I won't cover configuring emacs, as that is a whole art unto itself, one which I have yet to master.

You probably already have emacs installed, I'll assume you do. At the command prompt, type:

emacs

Emacs will start up with a scratch buffer, which isn't really meant for anything other than scratch notes. So, we must bring emacs up with a filename on the command-line. Before we do that, we must exit emacs. Hit C-x C-c(hold down control, then press x, then press c), and it'll exit. Now, let's bring it up with a filename:

emacs bork.txt

The screen will look something like this:

Buffers Files Tools Edit Search Mule Help






















----:---F1  bork.txt          (Text)--L1--All-----------------------------------
(New file)

Now, let's look at the bottom status line. It displays the filename we're working on, informs that it's using the Text mode(more on emacs modes later in this doc) , that we're on line 1, and it's display all of the file. As an example of what it will display while editing a file with information in it, here's what's on the status bar on my screen:

----:**-F1  emacs.html        (HTML)--L59--70%----------------------------------

The two asterisks show that file has been changed since I last saved, I'm editing emacs.html, emacs is using it's HTML mode, I'm on line 59 and 70% of file is displayed on the screen. Now, type some text in. You'll notice the asterisks and line number. Now, let's save your masterpeice! Hit C-x C-s(that's hold down control, press x then s), and at the bottom it will say:

Wrote /home/paul/bork.txt

You've just saved your work! Let's exit emacs and bring it back up with our text file, and you can see for certain that the file has been saved. That covers the basics you need to get around with emacs, now on to....


Special Modes

Emacs has a built-in LISP interpreter, making it so that emacs can be programmed to do various tasks. This allows it to handle HTML, SGML, shell scripts, C code, texinfo source, TeX source, etc. more appropriately. The classic thing to do with programmable calculators has always been to write games for them - guess what one of the classic things to do with a programmable text editor like emacs is. Emacs has a LISP-based version of the classic pseudo-AI program, Eliza. In this case, it's designed to act as a psychoanalyst. Now this part can get a bit tricky, as the official key used to run these modes is named 'meta'. PCs don't have a true-blue meta key, so it's often mapped to one of the alt keys, or a control key. Hit M-x, trying first the left alt, then right alt and same for controls, you'll know when you've hit the right one when the bottom line displays M-x with the cursor beside it. Now, type doctor and hit enter. The following text will appear on your screen:

I am the psychotherapist.  Please, describe your problems.  Each time
you are finished talking, type RET twice.

Go ahead, chatter with doc for a bit. It can be entertaining...

Back so soon? Well, it does get a wee bit boring after a while... Now that you're back, we're gonna write some C code to show the benefit of using emacs. I want you bring up emacs, and edit ~/.emacs

Put the following in it:

     (add-hook 'c-mode-common-hook
               '(lambda () (c-toggle-auto-state 1)))

This may, at first glance, look like gibberish. It's actually LISP code, at seeing this you now understand why some derisevly state that LISP really stands for Lots of Irritating Superfluous Parentheses. Fortunately, you don't need to know LISP right now - though you will have to learn it to do much configuring with emacs. Save the file, and start emacs editing a file named foo.c

Type the following:

#include <stdio.h>

main(){printf("\nHello.\n");}

Doesn't look like what's here, does it? Notice how emacs automagically indents the code properly and indicates to you that the braces are matched? If you don't program in C, you won't realize just how neat this is. Beleive me, if you do much coding, it's a godsend!

Emacs has similar modes for HTML, SGML, even plaintext. It can read e-mail, usenet news and browse the web. Emacs includes everything, including the kitchen sink. Browse the docs, and use it, and with time you will begin to use emacs to it's full capacity.

May the source be with you,
--Paul Anderson, paul@geeky1.ebtech.net


Copyright © 1998, Paul Anderson
Published in Issue 35 of Linux Gazette, December 1998


[ TABLE OF CONTENTS ] [ FRONT PAGE ]  Back  Next