Introduction to Emacs
Andy Harris
(originally appeared in Install, Configure, and
Customize Slackware Linux
–Prima Tech 2000.
Reprinted with Permission.)
In this
chapter, we willyou examine Emacs, a powerful and popular
text editor. We willYou look at many
of the features available in Emacs, and explore how you can use it to enhance
your text development efforts whether you are working on simple text documents,
managing a web server, or writing complex programs.
For
those raised on GUI interfaces, it might seem strange to even need a text
editor. After
all, Star Office and some of the other
commercial materials available can do all that a text editor can and much more.
Modern word processors can
handle text with formatting, graphics, and a host of special features. All these things would factors make a
program which simply edits text seem to be out
of date, or even a little quaint.
Text
editing was originally a very large part of computing, and it has never really
gone away. Most
computer programs still ultimately start out as text files, no matter how
elaborate they will beare when compiled. WeYou might use fancy editors for writing code
or for designing web pages, but eventually it all comes back to text editing. Computer users find themselves
constantly needing to edit text files.
Text
editors really come into their own when you find yourself needing to access
your computer remotely, or when you do not have access to all the graphical
features of your editor. This
is especially common when you are using your computer as a web server or when
you are doing some kind of programming.
Emacs
is not the only choice of text editor in the linuxLinux world. A number of other editors are
usually included with linuxLinux or unixUnix installs. These other editors, especially
vi (Visual Editor) are also extremely popular. Linux people are prone to have
'religious wars' about which editor they prefer, but it probably makes sense to
know more than one. Golfers
have a lot of clubs in their bag, because every shot is a little bit different.
If you do a lot of text editing,
you should know a number of editors, because some jobs require a very powerful
editor such as Emacs, and some don't.
If you think you are going to be needing some high-powered text editing,
it makes a lot of sense to add Emacs to your list of skills.
Emacs
is especially useful if you will find yourself writing HTML pages
or programs in languages likesuch as C, Java, or Perl. We willIn this chapter, you learn
examine how it can help you to efficiently write, test, and debug
programs. We willYou also look at some of its exceptionally
powerful features, including facilities for searching and replacing text, features that aid in writing,
editing and running macros, modes that let you interact with the linuxLinux
environment without leaving your editor, and customization features that let you
configure Emacs however you wish.
It is
not my goal to turn you into an Emacs fanatic, feeling somehow superior to
those unfortunate souls doomed to an eternity with vi or (shudder) pico. I simply want to introduce you to a program
that can radically change the way you do certain jobs. Emacs is not a simple application. It has a
learning curve, and it will requires some effort to
master. If
you are willing to spend some time with it, you will probably find that it
profoundly improves the way you do some of your work.
It
will make my job a lot easier if I startI’ll begin by
describing some of the assumptions and conventions about Emacs. Emacs assumes the same things about its
users that linuxLinux does in general: Emacs users are smart, they don't mind
learning things, but they want excellent performance, solid reliability, and
oh, yeah, they want it to be free. In all these ways, Emacs is very much in the
same spirit as Linux itself. Although
Emacs is available in many forms, including some with GUIs, it is at its heart
a text-based program. The assumption is that the user might want to use menus occaisonallyoccasionally for
obscure commands, but will usually uses keyboard
commands.
We
willDo
not assume any particular graphics shell, because they are largely irrelevant
when you are doing text editing. In fact, one of the primary
reasons you may be interested in Emacs is because it gives you such tremendous
power in text mode. This
can be very handy if you are using your computer remotely through telnet. If your computer will be
functioning as a web server, many of your clients will never see your graphic
shell, but will only have access through telnet, which is a text-only
interface. Emacs is quite often run
from a remote computer, or even from a dumb terminal. For this reason, we cannot make any assumptions about how the
user is connected, how the keyboard is mapped, or even what kinds of keys the
user has on the keyboard. This
would seem to be a huge barrier, but Emacs has some clever solutions to this
issue, as you will see soon.

This
is shows how Emacs looks when run from a Telnet session. Notice that it is text-only.

Here
is the same file being edited under a graphical shell.
Emacs
is heavily reliant on keyboard commands for control. Keyboard commands can be
difficult to memorize, but this tends to not be as big a barrier as you might
think. First of all, most of the common
commands have reasonably easy mnemonics, such as 'control-f' for 'forward a
character' and 'control-b' for 'back a character'. Secondly, Emacs has a menu
system, which you can use when you cannot remember a control sequence. Thirdly, you will find that using keyboard
commands can be self-reinforcing, because they are so much quicker than using a
mouse or a menu. If you are a touch
typist, your hands will never need to leave the keyboard, even when you are sending
complex commands to your editor.
Finally, you will find that you won't really ever need to make yourself
a set of 'Emacs flash cards' to learn your command sequences, because the ones
you use frequently will just etch themselves into your brain and your fingers.
After you've had enough practice, you won't even have to think about a
particular sequence, because it will just be automatic.
Caution
Before we get much farther here, there are a few is an important
keystrokes you should know in case you get into
trouble. Emacs has a habit of opening
windows on you, and occasionally giving you a strange window or prompt that you
don’t know what to do with. When you
find yourself stuck, try ‘control-g’.
This is the ‘get me out of here’ command. It is used much like the escape key is often used in other
programs.
Many of
the Emacs commands utilize the 'control' key.
As I mentioned before, Emacs is designed to work on a variety of
different keyboards. Fortunately,
almost every keyboard made has a control key.
(Even my ancient 'first love', the TRS-80 model I, had a control
key. I was the envy of my neighborhood
with that awesome 16K of memory!!
Sigh....). In any case, it is reasonable to assume a control key. In
Emacs documentation, a control sequence is marked with a capital C followed by
a dash, so Control + g would look like this: C-g
It is
not surprising that all the most common commands are control commands. The commands that are used for
cursor movement, cutting and pasting, and all the other things that you do the
most often are all simple control combinations. Of course, with a standard keyboard, you still have a limited
number of control combinations. Emacs
has a lot of commands, more than can be called with the control key and a
single character, so Emacs uses a number of other tricks for controlling the
program. In addition to the normal
control sequences, there are special multi-key sequences. For example, all the file management
commands start with C-x.
Finding (opening) a file is C-x
C-f, and saving a
file is C-x C-s.
Even
these multi-key sequences are not enough to control the many commands that
Emacs supports. In addition to the
control sequences, Emacs supports a second control key, called the 'meta'
key. Here's where things get just a
little strange. Most keyboards have a
secondary control key, but they aren't all called the same thing. The keyboards
in the Intel tradition have an 'Alt' key, Macintoshes have a 'Flower' key, and
Sun workstations have a 'Diamond' key.
All of these keys have a common purpose, to invoke another set of
control sequences. The problem is that
Emacs cannot always recognize these keys.
If, for example, the user is running Emacs from a windows-based telnet
program, the alt key is mapped to the local telnet client, and will not be sent
across the telnet connection. To avoid this kind of problem, Emacs has multiple
ways of reading the meta key. In a linuxLinux install,
you will probably be able to use the 'alt' key as your meta key. If that does not work, you can use the
escape key instead.
Caution
One important thing about using the escape key as a meta
key: It does not act exactly like the
control key. When you activate a
control sequence, you press the control key down and hold it down as you press
another key. If you are using 'alt' as
your meta key, you can do pretty much the same thing. If you are using the escape key, it must be pressed and released
before you press the next key.
In any
case, the meta key is used for a lot of commands, and in the Emacs
documentation, it often looks like this:
M-b
stands
for
Meta
key and b
One
very interesting characteristic of meta sequences is that they tend to amplify
control sequences. For example, C-f
means 'go forward one character', and M-f means 'go forward one word.' Likewise, C-y means
to yank (or paste) whatever was last cut.
M-y means to pull an earlier cut and
paste it. I'll explain these commands
later, but the main point here is that control commands and meta commands are
often related, and the meta command is usually a more powerful version of the
control command.
There's
a couple of other ways to get to commands in the Emacs environment. One of the most powerful is by knowing a
command's name. For example, I can
never remember what the command sequence for 'interactively spell the current
buffer' is. For me, it is easier to
remember the name of the command, ispell-buffer. When you can remember the name of a command, you can invoke it directly,
with M-x. So, to start the spell checker, I
type 'M-x ispell-buffer.'
This will work even for commands that do not have a keystroke combination bound
to them.
In
addition to all these techniques, Emacs has a set of menus, which contain all
the most useful commands. In the GUI
world, online menus are not anything shocking, but they are very unusual in
text-based programs in the unixUnix tradition. Most unixUnix editors do not have them.

Here
is how the menus might look under a graphical shell.
If you
are running Emacs in some kind of graphical shell, you can use your mouse to
access the menus in the normal way. If
you are in a text mode, the mouse may not work correctly. In most setups, you can use the F10 key to invoke the online menus.
If that doesn't work, you can try M-`
(Note that ` is the backtick, not the apostrophe. It is often on the same key as the ~ tilde
character). If that doesn't work, you
can always call up a crude form of menus with this command:
M-x tmm-menubar.
In any
case, you should see a set of menu items pop up. In text mode, the menus will look unusual, because they will be
at the bottom of the screen, but after you play around, you will see that they
act pretty typically.
In figure 13-04, you will see Emacs run with a text
mode menu active. Note that the menu is
on the bottom, but all the same commands are available (Compare with figure
13-03). You can access this kind of
menu even when you are on a system without a GUI interface.

Example of text mode menu
Here is the
text-mode menu. Note that the menu is
on the bottom, but all the same commands are available. You can access this kind of menu even with
non-graphical systems or connections.
Emacs
is also unusual in the amount of online help it provides. Unix-style programs
are not generally known for excellent help systems, but Emacs has many kinds of
help, some of which are pretty useful.
All the help commands can be accessed by C-h, and the first one to learn is C-h ?, which will bring up a list of other help options. There are a number of choices here, but
we'll go through the most useful for beginners.
C-h t is a tutorial. This brings up a window with a text file in
it. The text file is instructions in how to get around in Emacs. If you haven't already done so, you should
take a break from reading, and run this tutorial. It will get you up and running quickly. You can't hurt anything, so just play around with it to get the
basic ideas. The rest of this chapter will make a lot more sense when you've
done a little playing on your own. You
will find that the tutorial takes you through many of the same topics as this
chapter, and touches on a few other interesting things.
If you
want help on some particular topic, you can invoke the apropos help feature
with
C-h a
This
will ask you for a particular keyword, and will search for any command name
that contains that word. It is kind of
like an index system in a more traditional help system. Like those indices, the results can
sometimes be exactly what you are looking for, or they can be overwhelming and
useless.
As you
have already seen, Emacs has a bewildering number of commands. You can use the C-h b command to describe the current key bindings. This will open up a
little window that will contain a list of all the keystroke commands Emacs
recognizes, and what commands will be invoked by that sequence.
C-h i invokes info mode, which is a
full-fledged, hypertext help system. It
has a lot of information. It can be
overwhelming, but it is as good a help system as you will find in the unixUnix editor
world.
help commands
|
Key
|
command
|
what
it does |
|
C-h ? |
help-for-help |
shows a list of the help commands |
|
C-h t |
help-with-tutorial |
runs the online tutorial |
|
C-h a |
command-apropos |
allows user to search for a command containing a
word |
|
C-h m |
describe-mode |
describes anything special about the current
setup |
|
C-h b |
describe-bindings |
gives a list of the current key commands |
|
C-h i |
info |
runs the special info help system |
|
M-` (F10) |
tmm-menubar |
opens the menu in text mode |
Well,
the point of all this was to edit some text, so let's get going. As complicated
as Emacs can be, editing text is very easy.
You just type it in, and it goes on the screen.
You
will usually want to be able to move around in your document, and Emacs gives
you a number of ways to do that. You
can probably use your arrow keys in the normal way, but you might want to learn
some other techniques as well. These
other techniques can be used without your hands ever leaving the main keyboard,
and they allow for some more flexible options.
The
easiest kind of movement is a character at a time. Use the arrows to go left, right, up, and down. You can also use control characters, because
the arrow commands are not available on every terminal. C-f is forward, C-b is back, C-p is previous line, and C-n is next line.
If it's
easier for you, remember that p is the last letter in 'up' and n is the last
letter in 'down.'
If you
want to move more quickly through a file, you can move a word at a time. To go forward a word, you can use M-f, and M-b goes back a word at a time. Notice the relationship between the control
combination and the meta combination.
To go
to the beginning of the current line, use C-a. C-e goes to the end of the current
line.
Emacs
has the ability to move in even more powerful ways. You can go forward or back in increments of a sentence, a
paragraph, or a
screen.
The following summarizes the various movement commands.
You can
also modify the movement commands by adding an argument to the command. Precede the command with C-u and a number to
make the command execute that number of times.
For example,
C-u 8 C-f
moves
forward eight characters
C-u 4 M-b
moves
back four words.
Movement
commands
|
Key |
command |
what it does |
|
C-f (right arrow) |
forward-char |
move forward one character |
|
C-b (left arrow) |
backward-char |
move backwards one character |
|
C-p (up arrow) |
previous-line |
move up one line |
|
C-n (down arrow) |
next-line |
move down one line |
|
M-f |
forward-word |
move forward one word |
|
M-b |
backward-word |
move backwards one word |
|
C-v |
scroll-up |
move cursor DOWN one screen in the document |
|
M-v |
scroll-down |
move cursor UP one screen in the document |
|
M-< |
beginning-of-buffer |
Go to the beginning of the document |
|
M-> |
end-of-buffer |
Go to the end of the document |
|
C-a |
beginning-of-line |
go to beginning of line. (surprise.) |
|
C-e |
end-of-line |
go to the end of the line |
It is
surprising how often the destructive commands come in play when creating text
files. Fortunately, Emacs provides a
number of ways to delete text.
For the
most part, you can use the normal delete and backspace keys to delete text a
character at a time, but these commands will not always work as you suspect
when you are accessing Emacs through a telnet session. Sometimes in this case
you will need to explicitly set up your telnet program to send delete and
backspace characters correctly. In any
case, you can always use C-d to delete the current
character. C-d acts like the typical delete key. It deletes the character directly under the cursor, moving all
following characters one character to the left. The backspace key deletes the character to the left of the
cursor. The characters deleted by using
the del key, the C-d command, or the backspace key are really gone, and cannot be
'yanked' back (more on the yank command in a few minutes.) They can be retrieved by the undo command (C-x u.)
Often
you will be manipulating larger units of text, and you will find yourself
wanting to delete words, sentences, lines, or paragraphs at a time. Emacs supports a number of commands for
doing exactly this:
***{Begin
Table]***
Deletion
commands
Key Command What
it Does
C-d (Del) delete-character Delete current
char
M-d kill-word Delete current word
BackSpace(Del) backward-delete-character Delete previous char
M-Backspace backward-kill-word Delete
previous word
C-k kill-line Delete rest of line
M-k kill-sentence Delete rest of
sentence
C-x Backspace backward-kill-sentence Delete previous sentence
***{End
Table]***
One
note about killing a line: A line
usually goes until it reaches a carriage return character. In Emacs, if you hit the C-k command, you will delete the entire line, but not the newline
character. If you hit C-k again, you will delete the newline character as well.
Sometimes,
you will want to delete things that do not exactly fit within the definitions
of the above commands. You can also
mark an entire region and kill it all in one command. There are a number of Emacs commands that operate on a region, so
it is a good skill to have.
Unfortunately, Emacs does not display in any obvious way which text is
in a region, so it can be a tricky concept to get used to.
Basically,
here's how it works. You're used to the
cursor, which is visible, and indicates where the next character you type will
go. In addition to this visible cursor, Emacs supports another cursor which is
not visible. This second cursor is
called the mark. To work with a region, you move the visible cursor until it is
at the beginning or end of some text you want to work with (for now, we'll just
assume we want to delete the text.) Use
the C-space or C-@ command to set the mark at the current cursor position. The two commands mean exactly the same
thing, but not all keyboards will
support C-space.
At the bottom of the screen, you will see a 'mark set' message.
Now you
can move the cursor to the other end of the text you wish to manipulate, and
you can activate a region command, such as C-w
kill-region). Everything between the
current cursor position and the mark will be deleted.
TIP
Sadly, Emacs does not put a visible cursor where the
mark is, but there is a terrific command for figuring out where the mark currently
is. It is C-x C-x, which means exchange-point-and-mark. What this command does is move the cursor to
wherever the mark was, and move the mark to where the cursor was. Use this
command twice in a row to see the beginning and the end of a region. It's a really good idea to do this before
you run region commands like kill-region, because some of the other commands
might move the mark without you knowing it has happened.
Deleting
things can be therapeutic, but often you will not want something simply
destroyed, but moved to another place in the document. Most text editors provide some sort of cut,
copy, and paste mechanism. Emacs
provides this capability, with a few twists.
Essentially,
all of the kill commands (the ones with 'kill' in their Emacs name) are cut
commands. What this means is if you delete a word, sentence, line, or
paragraph, that text is automatically stored in a buffer which you will be able
to access later. The only deletion
commands that do not have this behavior are the ones that work on only one
character at a time. In Emacs, pasting
is called 'yanking'. (The authors of Emacs apparently loved mnemonics so much
that they often changed the names of commands so they could make a keystroke
that matched!) The standard yank
command is C-y.
This command causes the last element that was cut to be pasted back into
the document at the current cursor position.
What
makes this interesting is the way that things are added to the kill
buffer. Imagine the following line:
one two
three four five
If we
were to go to the beginning of the line, and hit M-d, we would delete one word (the word 'one') and it would be put in
the kill buffer. Now, if we moved
somewhere else, and hit the C-y command, the contents of that
buffer (the word one) would be pasted.
That's not at all surprising, but here's a twist:
What if
we started with the same line, put our cursor at the beginning, and hit M-d 3 times? Clearly, the
line would now contain the values
four five
but the
real question is "what is in the buffer?" When we hit the yank command, we will see
one two
three
If you
do a series of identical kill commands, Emacs treats them all as one command,
and places them together. This is very
handy, because most of the time you will want to move a series of words, lines,
or sentences.
The
kill buffer of Emacs is flexible in another way. In many systems, there is only one buffer for copy and
paste. Emacs keeps track of the last
thirty deletions! (more or less. The
exact number can be changed by the user)
To yank back earlier text, start with the C-y command, then hit M-y until you see the specific text
you are looking for. If you keep hitting M-y, you will originally come back to
the last thing killed. Because of this
behavior, the kill buffer in Emacs is more accurately referred to as a kill
ring, because it can contain many items, and you can go around the ring to find
them.
No
doubt one of the reasons for Emacs' popularity is how much it adds to the
bizarre vocabulary of techies. It's fun
just to be able to mutter things about 'yanking from the kill ring.' Sometimes I love being a nerd...
TIP
Here's an interesting way to take advantage of the kill
ring: If you have some kind of text that is difficult to type and that you will
be using a lot in a document, you can type it once and kill it. Then, whenever you need that phrase, you can
yank it from the kill ring. This way you only have to type it once!
Insertion and deletion
commands
|
Key
|
command
|
what
it does |
|
C-d (Del) |
delete-character |
Delete current char |
|
BackSpace(Del) |
backward-delete-character |
Delete previous char |
|
M-Backspace |
backward-kill-word |
Delete previous word |
|
C-k |
kill-line |
Delete rest of line |
|
M-k |
kill-sentence |
Delete rest of sentence |
|
C-x Backspace |
backward-kill-sentence |
Delete previous sentence |
|
C-@ (C-SPACE) |
set-mark |
set a boundary for copying or deleting |
|
M-d |
kill-word |
remove next word, keep it in buffer |
|
C-w |
kill-region |
cut from mark to here, keep in buffer |
|
C-k |
kill-line |
cut from here to end of line, keep in buffer |
|
C-y |
yank |
paste last thing put in buffer |
|
M-y |
yank-pop |
paste previous buffer (C-y must happen first) |
Basic
file management is a critical part of any text editor. Emacs provides all the typical
functionality, but adds some special features, such as the ability to work with
multiple files at the same time, and the ability to have multiple windows open
at once, even in a text-based environment.
Of
course, you will rarely write text without it being stored somehow in a
file. Emacs provides a number of ways
to manipulate files, including features for managing multiple files at the same
time.
When
you are on the linuxLinux command line, you can open up Emacs with
a file by typing 'Emacs filename' If the file already exists, Emacs will start
with the file loaded in. If the file
does not already exist, you will get a blank screen associated with the
filename you chose.
If you
are already in Emacs, and you want to load in a file, you can use the
'find-file' command C-x f.
You
will be prompted for a file name. Enter
the name of the file, and it will be loaded if it exists, or you will get a
blank screen to write in if the file does not yet exist. If you want to import a file into your
current document, you can use C-x i to insert the file. The contents
of the file will be inserted wherever the cursor currently is.
Saving
a file is very simple. The main way to
do this is through the save-file command, C-x s. This command will save the file using
whatever name you have assigned. Note
that in Emacs, your files will always have a name, even if you have never saved
them before. If you want to save the
file to another name (like a save as command in Windows) you can use C-x w, which stands for 'write-file'.
By now
you might have noticed that Emacs sometimes changes its behavior. For example, if you use Emacs to edit an
HTML file, you will see an HTML menu appear.
If you use Emacs to edit a java or perl program, Emacs might put all the
comment characters in one color, and the keywords in another. Also, if you are in a programming language,
you might notice that Emacs automatically indents the code whenever you press a
semicolon (the end-of-line character for many programming languages). Emacs is capable of changing its behavior to
manage special forms of editing. This
is one of the things that makes it so endearing to high-end users. It keeps much of the same general
characteristics, but it customizes itself to be useful in many contexts. This is done through a technique called
'modes.' Emacs has two kinds of modes,
major and minor modes.
The
major modes are generally related to kinds of editing. There is a major mode for writing plain text
(... and it's called ... text mode!!!)
There are also major modes for html, c language programming, typesetting with
the Tex and LaTex languages, and even modes for reading email and playing games! We will generally stick to text mode in this
chapter, although it is certainly worth exploring the other modes. You will probably find some very
entertaining and useful modes in your implementation of Emacs. To find out which modes are installed in
your version of Emacs, use C-h p.
You will see a list of packages. If you select a package, you will see
the modes in that package. In my version, I found 34 packages, most with at
least a couple of modes. That should
keep you busy on a rainy day. Most of
the time, though, people only use a few standard modes. Most of the modes are related to very
specific programming languages or other kinds of specialized editing.
Once
you are in a particular mode, you will see its name on the bottom line of the
editor. You can get more information
about that mode by typing C-h m (describe-mode). This will give you an overview of the mode
you are in and any special key bindings that might be a part of this mode.
There
are two major ways to get into a specific mode. The first way is to simply open up a file. Emacs recognizes a large number of file
extensions and automatically opens up the correct mode most of the time. Occasionally, you will need to help Emacs
along, so you can go directly to a mode by typing it's name.
If you
want to experiment with an interesting mode, you can play around with something
called 'picture mode.' Invoke it with M-x picture-mode and use C-h m to see how picture mode
works. I won't spoil it for you, but
you'll see that it gives you an interesting way to do ASCII art!
The text
mode is actually a pretty boring mode.
It adds the ability to use the tab key, and very little else. Surprisingly, text mode does not
automatically involve word wrap. This
is controlled by a minor mode, as we will see shortly. However, the sparseness of text mode is a
feature, not a bug. The whole point of
working in plain text is to not have any surprises. Text mode is a very clean, very basic mode.
If you
are going to be doing any programming, Emacs is your editor. Programming is a
very demanding form of editing, and it has a number of very peculiar
needs. We do not have time in this
chapter to go through all the programming modes, but it is still worthwhile to
note what they have in common. The
programming modes feature automatic indentation, which can help make it easier
to find logic errors in your program.
They also have syntax coloring, which is a feature that changes the
color of text elements based on their meaning. With this feature, your code
becomes much easier to read and repair.
The programming modes have special features for checking to see that
special characters such as parentheses and brackets are matched up
correctly. If you've ever done any
programming in a language such as C, you know how useful that can be. The programming modes also have a compile
command, so you can test and run your program without leaving the editor. If you have an error in your program, there
are features which help you find the line number where the mistake is reported
to be. If you do some programming, Emacs can make your life a whole lot easier.
Many
people use linuxLinux because it is such an ideal operating
system for running web servers. Of
course, a web server is pretty boring without some web pages on it. The HTML mode turns Emacs into a reasonably
powerful HTML editor. It will not give
you WYSIWYG results, but it can considerably speed up 'hand-coding' of
HTML. For example, if you want to add a
hyperlink to an HTML document, and you are in HTML mode, you can just type C-c C-c h, and the following will be inserted into your document:
<a
href="http:"></a>
The
cursor will be positioned right after the colon, so you can just start entering
the url. Similar commands are available
for all the most common HTML tags. Use
of the HTML mode can considerably speed up the creation and modification of
HTML documents.
In
addition to the major modes, Emacs also supports a number of minor modes. These are a little bit different than their
big brothers. A document can only be in
one major mode at a time, but it could have a number of minor modes
activated. While major modes can
radically change the behavior of Emacs, minor modes generally are more subtle,
and just change a feature or two.
Perhaps
the most commonly used minor mode is the auto-fill mode. When you are writing plain text, such as a
readme file or an email message or something, you usually want word wrap of
some sort to occur. Emacs does not do
this as part of its default behavior.
The way Emacs is originally set up, when you get to the end of a line,
Emacs will generally let you keep on typing, but will put a / at the end of the
line, and put the next character on the next line. In this default mode, Emacs does not put any carriage returns in
the file, unless you press the 'enter' key.
This is important, because in certain kinds of editing, especially
programming, a carriage return in the wrong place could be a disaster.
Another
feature that is sometimes very important is having line numbers visible. This is especially true in programming when
compilers report a problem on a specific line.
There should be some easy way of knowing what line you are on, and there
should be an easy command to go directly to any given line. Emacs provides a line-number mode (M-x line-number-mode) and a goto line command (M-x
goto-line <number>)
The goto line command will work regardless of whether you are in line number
mode.
There
are a number of other minor modes available to you in Emacs. You can learn more
about them in the same way you learned about the major modes. Consult the documentation with C-h p to learn the packages, and when you are in a particular mode, use
C-h m to learn about its features. You can also find out more detailed
information about the specific modes by using the info browser (C-h i) and searching for a particular mode.
Emacs
is primarily a text-based program, but it has some of the behavior of its gui
cousins. You have no doubt seen the
Emacs screen split into two or more separate areas, with different things in
each. These separate areas are called windows. As you have been playing around
with Emacs, you have been opening windows all over the place. The help screen opens up a window, as do the
text mode menus. One of the most
bewildering thing about Emacs for beginners is the dozens of windows that seem
to keep popping out of nowhere. Emacs
can handle more than one file at a time in memory, and can even have one file
open in two different windows. This can
be a powerful but bewildering feature.
First, let's look at how we open
multiple windows without worrying about what goes inside them. Most of the windows commands are C-x
commands. C-x
2 breaks the current window into two windows stacked on top of each other. C-x 3 breaks the current window
into two windows side by side. C-x o (the letter o) moves you to the next window. C-x 0 kills the current window,
and C-x 1 makes the current window the only window.
In
figure 13-05, the Emacs screen is split into two windows with the C-x 2 command, then the top window was split again with
C-x 3. At this point,
all three windows are pointing to the same file and buffer, but they can point
to different things if we wish.

Example of an Emacs session split into three
windows
Here, the
Emacs screen is split into two windows with the C-x 2 command, then the top
window was split again with C-x 3. At
this point, all three windows are pointing to the same file and buffer, but
they can point to different things if we wish.
Generally,
the side-by-side placement of windows is not very satisfying, because you can't
read the text in either window. The
top-and-bottom approach is much better if you find you need two windows. You might not want the windows to be the
same size, so you can go into a window and invoke C-x ^ to make that window one line taller. Use the argument feature to make it much larger. For example C-u 10 C-x ^ will make the current window 10 lines taller, and will make the
other window 10 lines shorter. This
might be handy when, for example, you want a window pointing out at the command
line shell, or running the directory editor.
(We'll learn how to do these things later, I promise)
So far,
the windows you have been using have all pointed to exactly the same file. While this can be interesting, it has limited
utility. Emacs allows you to have more
than one file open at a time, and you can have different files in different
buffers. If you have help open, or one
of the other specialty modes, Emacs may have generated a buffer or two for
you. It is possible (even likely) that
you will have more buffers than windows.
Essentially,
the windows are what the user can see, and the buffers are all of the files
(and sometimes some other things) that Emacs currently has in memory. Whenever you use C-x f to open a file, you are also opening a buffer. It isn't at all unusual to have 5 or six
buffers open at once if you are working on a project that involves multiple
text files. The easiest way to work
with buffers is to invoke the buffer list through C-x C-b. This will give you a
list of the current buffers. You can
treat this list like a menu, using the arrow keys to move up and down the list,
and the enter key to choose a specific buffer.
That buffer will then be shown.
If you know the name of the buffer you want to go to (it is often the
same as the file name), you can use C-x
b to go to the
file directly. C-x b will default to the last buffer you used, so it is very handy if
you are going back and forth between only two buffers. In addition, the C-x b command will work on the buffer the cursor is currently in, so
you can set up a window set, go to each window, and use C-x b to set exactly what buffer you want to show in that window. This can be very handy in certain situations,
such as when you are writing shell scripts and you want a unixUnix shell
avaiable for testing the scripts.
File, Buffer, and window handling commands
|
Key |
command |
what it does |
|
C-x C-f |
find-file |
open a new or existing file |
|
C-x C-s |
save-buffer |
save this file with it's existing name |
|
C-x C-c |
save-buffers-kill-emacs |
exit emacs |
|
C-x o |
other-window |
move to another window (if more than one is visible) |
|
C-x 1 |
delete-other-windows |
make the current window the only window visible |
|
C-x 2 |
split-window-vertically |
makes another window (full width of the screen) |
|
C-x 3 |
split-window-horizontally |
makes another window (full height of the screen) |
|
M-x shell |
shell |
make a |
|
C-x b |
switch to buffer |
switch to a buffer (you’ll be prompted for a buffer name |
|
C-x C-b |
list-buffers |
displays a buffer menu. |
You
will be able to get by just fine with the editing commands you
already
know, but soon you will find yourself interested in some of the more powerful
editing tools supplied by Emacs. These
concepts take some practice to master, but you will find that the effort will
pay off many times in terms of improved efficiency.
Editors
have had search and replace features from the very beginning, yet people often
do not take advantage of them fully.
Clever use of search and replace can often save you a great deal of
effort when you find yourself working on some difficult editing problem. Emacs gives you a number of ways of
searching for text and replacing it.
C-s is an incremental-search-forward. What this means is you can type in a phrase, and Emacs will look
for partial matches as you type. To
explain this concept, I experimented on the chapter you are reading now by
starting with the cursor at the beginning of the buffer (M-<). I then invoked C-s, and started typing out the word
'relationship' (it occurs on line 288).
When I
typed r, the cursor was positioned at the ‘r’ in 'introduction', the first occurrence of
'r' in the document. When I typed 'e',
the cursor was moved to 'features', the first location of 're'. As I kept typing letters, the editor found
closer and closer words: 'religious', 'related', and finally
'relationship'. The advantage of an
incremental search is that you rarely have to type the entire word. You can
usually get just the first few letters and it will take you to the word you are
looking for.
CAUTION
Although the cursor appears to have moved when you are
doing a sequential search, it doesn't move permanently. To tell the computer you're done searching
and you want to do something at this point, you need to invoke one of the
cursor movement commands. I usually use
M-b so I get to the beginning of whatever word I was looking for in the text.
TIP
Incremental searches start from the current cursor
position. You can use this to limit searches to a smaller part of the document,
but if you intend to search the entire document, precede the search command
with a beginning-of-buffer (M-<) command. If you want to find other
occurrences of the word, just hit C-s again. C-s will continue looking for the
last search attempted.
Searching
is a really great way to get around in longer documents. You can use searches
on key words, section titles, or other unusual words or phrases to get to a
particular part of a long document. If you wish, you can search backwards
instead of forward. Just use C-r, for 'interactive'incremental-search-backward',
and you will be searching for previous occurrences of the pattern.
Emacs
has more traditional (non - incremental) searching techniques as well, but you
will probably not need them unless you are writing macros.
Searching
text is very useful, but the ability to replace the text can be even more
handy. This can be used to fix certain
common spelling errors, or to update a document easily.
***Begin
Sidebar***
***End
Sidebar***
Emacs has some variety in the types
of replace expressions it gives us. The
most common is query-replace-string, invoked by M-%. This command prompts you for a string to search for, and a
string to replace it with. It then goes
through the document looking for the search string, and whenever it finds it,
you are asked whether you want to replace the string, ignore it, or quit. There is a version (M-x replace-string) that does all the replacements without asking, but this is a
prescription for trouble, as it is very easy to make mistakes here. For example, if you decide to change all
instances of 'he' to 'she', then 'there' could become 'tshere'. Oops!
It's generally better to do the query form, so you can catch these
problems.
Emacs
also has support for regular expressions.
These are a special feature that has long been a part of the unixUnix
culture. Regular expressions allow you
to search for patterns, rather than specific strings. For example, the regular expression ^hello$ would only match on lines containing nothing but hello. \d\d\d-\d\d\d\d would match on any phone number,
and so on. Regular expression syntax
can be a difficult thing to learn, but if you use it enough, it can be an
incredibly powerful tool. If you
already know regular expressions from some other unixUnix tools, or
from perl programming, you won't have too much trouble figuring out how Emacs
uses them. Regular expressions are one
of those things that you could survive just fine without, but once you
understand them, you'll wonder how you ever got along without them.
Emacs
features a spelling checker that works in a couple of different ways. Both of them rely on the external program
'ispell', so if you do not have that installed, the spelling commands will not
work. To check the spelling of the current word, just move the cursor to the
beginning of that word and hit M-$.
If you want to check the spelling of the entire buffer, move to the
beginning of the buffer, and hit M-x
ispell-buffer. Ispell is an interactive spelling checker,
like you have probably used on more formal word processors. When it encounters a word that it cannot
find in its dictionary, it gives you a standard set of options, including
suggestions for replacement, ignoring the word, or adding it to a custom
dictionary.
Another
really great feature of Emacs is the undo command. This is invoked by C-x u, and it undoes the last
command. In fact, it can be called
multiple times to undo a number of commands.
This can really save you when you have made some kind of mistake in your
document.
Transposition
errors are among the most common errors in text editing. To fix such an error, move the cursor past
the two letters that need to be swapped, and hit C-t to transpose them. There
are variations of this command for transposing words (M-t) and for transposing lines (C-x C-t). These are most commonly used to quickly move
a word or line towards the end of a document.
Another
common editing mistake is capitalization issues. For some reason, If I have a word that starts with a th, and the
't' is supposed to be capitalized, I often end up accidentally capitalizing the
'h' as well. THis is how it usually
works.
To fix
this, Emacs has some commands to force an entire word to lowercase (M-l), force the word into all uppercase (M-u), or to force capitalization of only the first character (M-c). All of these commands
will move the cursor to the end of the word, so they can be used repeatedly to
modify a large amount of text quickly.
Additional
Editing commands
|
Key |
command |
what it does |
|
C-s |
isearch-forward |
prompts for a phrase, searches for it
interactively |
|
C-r |
isearch-backward |
prompts for a phrase, searches for previous instances interactively |
|
M-% |
query-replace |
do an interactive search and replace |
|
M-* |
query-replace-regexp |
do an interactive regular expression search and replace |
|
M-x replace-string |
replace-string |
replaces string without asking |
|
M-x ispell-buffer |
ispell-buffer |
run an interactive spell-checker |
|
C-x u |
advertised-undo |
undo last command |
|
C-t |
transpose-chars |
transpose the last two characters |
|
M-t |
transpose-words |
transpose the last two words |
|
C-x C-t |
transpose-lines |
transpose the last two lines |
|
M-l |
downcase-word |
convert current word to all lowercase |
|
M-u |
upcase-word |
convert current word to all uppercase |
|
M-c |
capitalize word |
convert first character of current word to capital, all other characters to lowercase |
Perhaps
the single skill that separates those who just use programs from the true power
users is the ability to create and use keyboard macros. Emacs contains features designed to make
macros reasonably easy to make, use, and modify. If you have never used keyboard macros before, the basic idea is
very simple. You turn on some kind of
recorder as you do a set of keystrokes, and the keystrokes are stored. At some point, you tell the program that you
are finished recording, and then you can play back the macro. This is really
great for doing repetitive jobs. If you find yourself hitting the same key
sequence over and over again, such as indenting every line two spaces (this one
comes up a lot in programming), it's time for a macro.
To
define a macro in Emacs, simply invoke the 'define macro' command with C-x (, type in your keystrokes, and finish with C-x ). Use the open parenthesis
to start the macro, and the close parenthesis to finish it. If you make a mistake, use the C-g key to get out of macro recording.
Although
Emacs makes it reasonably easy to create a keyboard macro, that doesn't mean it
is easy to make a macro that performs the way you want. It's easy to make mistakes that can cause
your macros to do strange things, so there are a few cautions worth noting.
It's a
good idea to think about exactly what you want a command to do in very general
terms, not just on the first line. If,
for example, you wanted to make a macro that would indent a line by two
characters. If you do the macro without
recording, it might go like this:
Indent two space macro
|
Command |
Meaning |
|
[space][space] |
insert
two spaces |
|
C-n |
down
a line |
|
C-b C-b |
back
two characters |
This
would work fine on lines that contain at least two characters, but what happens
when you try it on a blank line? You
should try it yourself and see, but it will not be the behavior you wish. The 'English' version of this macro should
be only slightly different, but that difference will improve the macro quite a
bit.
Improved Indent two space algorithm
|
Command |
Meaning |
|
|
insert
two spaces |
|
|
down
a line |
|
|
beginning
of current line |
When we
add the keystrokes, our macro definition will look like this:
Improved Indent two space macro
|
Command |
Meaning |
|
[space][space] |
insert
two spaces |
|
C-n |
down
a line |
|
C-a |
beginning
of current line |
Here's
why this version is a little bit better than the other one: Most of the time,
with our old algorithm, we were two spaces from the beginning of the line, so
back two characters was equivalent to going to the beginning of the line. However, we really meant 'go to the
beginning of the line, however many characters that is' Emacs supplies a
command for this, and that is the command we should use. Sometimes you will use
commands in macros that you might not use elsewhere, just to avoid this kind of
ambiguity. When you are recording
macros, you are much more likely to need those movement commands which dealt
with semantic objects, such as words, sentences, and lines.
Macros
are most useful when they are repeated.
For this reason, it's a very good idea to start a macro at a very
recognizable point, and to end the macro with the cursor at a similar
point. For example, my indent code was
careful to put the cursor at the beginning of the next line. This doesn't matter if I'm only going to use
the macro once, but most of the time, I have a whole group of lines I want to
indent.
It's
very common to start and end at the beginning of a line, sentence, or
paragraph. The basic rule is, if you
want to be able to repeat a macro, you should stop it at the same kind of
position you started it. If your macro
operates on words, it should start at the beginning of a word, and end at the
beginning of the next word. If it
operates on lines, it should start at the beginning of a line and end at the
beginning of the next, and so on.
If your
macros are designed to be repeated, it will be very tempting to use a
repetition parameter on it. For
example, to repeat the last keyboard macro 5 times, you would just type
C-u 5 C-x e
Macros
can use this parameter just like any other command, but it's a great idea to
save your file first, and to test the macro, or you could get some unexpected
consequences.
The
basic way to run a macro is with C-x e (call-last-kbd-macro). If you want to record more than one macro,
record a macro, then invoke
M-x name-last-kbd-macro.
This
command will prompt you for a name, and that name will be attached to that
macro. For example, look at the
following sequence:
TC Creating and naming a macro
|
Command |
Meaning |
|
C-x ( |
Start
recording a macro |
|
[space][space] |
Two
spaces |
|
C-n |
Down
a line |
|
C-a |
Beginning
of line |
|
C-x ) |
end
macro recording |
|
M-x
name-last-kbd-macro |
Name
that macro |
|
indent-2 |
The
new name |
|
C-u 10 M-x indent-2 |
Indent
the next ten lines |
This
code records a macro, renames it, and runs it 10 times, indenting the next ten
lines in the editor. Even if you record
another macro, you can invoke your new command simply by calling its name as an
M-x command. The command will only stay
named as long as this current session is running. If you want to keep it permanently, see the section on
customizing Emacs.
Keyboard
macros are so darn useful that Emacs now includes a specialized editor just for
working with them. After you have
recorded a macro, use C-x C-k to call up the macro editor. Then, you can type C-x e to have the editor bring up the last keyboard macro you defined,
or type M-x and the macro name if it is a
macro you have named. The editor has
several interesting features. Here's
how it looks:

The
macro editor manipulating the ‘delete two spaces’ macro
As you
can see, it is typical Emacs. Nothing
glamorous here, but it is very functional.
You can change the command name by replacing the text after
"Command: " and you can change the keyboard binding, so your macro is
attached to some specific keystroke. Be
careful about this, so you don't end up over-writing some existing command. I usually use C-c C-something, because this will always be available, but it still might
over-write something. If you want to
check to see if a keystroke is available, use C-h k to
determine what command if any is bound to the keystroke in question. Note that keystroke bindings can change when
you are in different modes, so you might have to re-assign a macro if you find
yourself in an unusual mode.
Because
indenting and deleting indentations come up so often in programming, I have
predefined the two macros you have seen here as C-c C-i for indent-2 and C-c C-d for del-2.
I have also modified my copy of Emacs so these macros always are loaded
whenever I start Emacs up, but that's another topic. (See the “customizing Emacs” section later in this document for
details)
Keyboard Macro commands
|
Key |
command |
what it does |
|
C-x ( |
start-kbd-macro |
begins recording a macro |
|
C-x ) |
end-kbd-macro |
stops recording the macro |
|
C-x e |
call-last-kbd-macro |
replay the last macro |
|
M-5 C-x e |
call-last-kbd-macro |
replay the macro 5 (or however many) times |
|
C-x C-k |
edit-kbd-macro |
prompts for a macro, loads it into an editor screen |
|
M-x rename-kbd-macro |
rename-kbd-macro |
lets you temporarily assign a new name to a macro |
|
M-x insert-kbd-macro |
insert-kbd-macro |
used to insert a macro definition into a lisp
file |
Your
text editing does not occur in some kind of a vacuum, but in the context of
some other sort of work. It is often
very handy to be able to communicate with your command line as you are editing. This is particularly handy if you want to
change permissions of files you are making, compile or run programs you are
writing, or looking around for a particular file to load up or import. Emacs allows a couple of forms of access to
the command line.
Emacs
has a terrific file management system built in, called Dired (which stands for
directory editor). Dired lets you look
at a particular directory, search for files using normal Emacs search commands,
mark a series of files for batch processing, and has an easy interface to some
of the most common commands, such as copying files, renaming them, and changing
file permissions.
There
are two main ways to set up dired. You
can invoke the M-x dired command, which is usually bound to
C-x d.
This will cause dired to open on the current directory. You can also use the C-x f command, but rather than specifying a file name, give it a
directory, and Emacs will open a dired window on that directory.
The
dired window often looks like this:

The
dired window pointing to a typical directory
As you
can see, it looks a lot like a normal unixUnix ls command,
but there are some important differences.
You can use C-n and C-p (and actually n and p,
without the control key) to move up and down the list of files. You can search using the C-s command, just like in a document. If you want to load up a file, just move to it and press the
enter key. If you have the appropriate
permission, the file will be loaded into a buffer for you. To move to a subdirectory, just move the
cursor to that directory and press enter, and a new dired will open pointing to
that directory.
You can
rename a file by putting the cursor on its name and hitting 'R' (it must be
capitalized) You will then be prompted for a new name. Likewise, 'C' allows you
to copy a file, and 'M' prompts you for a new permission set for the file. One of the nicest things about dired is the
ability to mark a series of files.
Let's say you wanted to change several files to 644 permission, so they
would be visible to the web server. We
can look at a directory file by file, and then hit the m key to mark any of the
files we will want to change permission on. When a whole series of files is
marked, we can then invoke the 'M' change permission command, and set all file
permissions to 644 with one command.
This is considerably easier than doing it by hand on the command line,
especially if there is no easy unixUnix wildcard
trick we can use to isolate our set of files.
You can also use the d key to mark files. Any file marked with the 'd' key is marked for deletion, but the
deletion won't actually happen until you force it with the 'x' key. You can unmark a marked file with the 'u'
key if you made a mistake.
Dired
provides even more functionality, but this should give you the basic idea. If you want more information, just go into
dired and call up the mode-specific help (C-h m) for
a list of commands.
Handy
as dired can be, it still cannot replace the good old Linux shell for some
things. You will still find times that
you would like to be able to run a command.
Of course, Emacs provides a number of ways you can do this.
If you
want to just run one command, you can use M-! run
shell-command. You will be prompted for
a command, and you can enter in whatever command you wish. The command will be executed under your
default shell, and the results will pop into a new buffer. For example, if you type M-! fortune(and of course fortune is installed on your system), you might
find a new buffer containing this gem of wisdom:
It pays to memorize the
collected poems of Edna St Vincent Millay backwards!!
I have
no idea what that means.
If you
wish, you can also use C-u M-! to automatically put the results
in the current buffer.
If you
wish, you can also run a full-blown linuxLinux shell in
another window. This is very handy if
you know you're going to be going to the shell a lot, like when you are writing
a shell script or compiling a java program and running it with
appletviewer. Just use M-x shell to
generate a shell. You'll get a new
window, and a copy of your default linuxLinux shell will
be running inside it.
This
new shell isn't exactly like the original shell, though. It has a few features that make it
exceptional. First of all, if you
invoke a command like ls that has a long output, the output is in an Emacs
window you can scroll and search in.
You can
invoke M-p and M-n to get at any of the commands you have already typed into the
shell. You can also use C-c C-o to clear the output of the previous command. This is a nice way to avoid cluttering up
the buffer.
Note
that the normal control sequences you might use with a program running in the
shell will not work the same. For
example, if you are running some program that is taking to long, in a
traditional shell you simply type C-c to end it. Likewise, you are sometimes required to use C-d to mark the end of a file, or a C-z to
suspend a process.
If you
need these commands, just proceed them with C-c. So, to stop a program, use C-c C-c, and to send a C-d, just type C-c C-d.
Figure 13-08 illustrates a typical use of
Emacs. I have the editor split in two,
and I have created a small Java program in the top window. The bottom window is a command line shell,
where I have compiled and run the program.

Running a
shell in a window
This is a
typical use of Emacs. I have the editor
split in two, and I have created a small Java program in the top window. The bottom window is a command line shell,
where I have compiled and run the program.
Once
you've been using Emacs for a while, you'll undoubtedly feel the need to tweak
it a little bit so it works exactly the way you like. There's a lot you can do
with Emacs. Some of the customization
can be quite involved, but there's a number of very simple things you can do to
make your version of Emacs fit your style a little better.
Emacs
provides a special configuration file called .Emacs, which is normally stored
in your main directory. This file might
start out empty, or it might already have some code in it which customizes your
version of Emacs. To modify your
version of Emacs, you can add code to this file. Emacs customizations are written in a dialect of lisp, a language
only a computer scientist could love.
We won't get into lisp programming here, but there are a number of very
interesting things you can do here.
CAUTION
Save a backup of your .Emacs file before messing around
with it. You can poke your eye out with
that thing. For example, you might
accidentally overwrite some important keystroke binding, and lose access to an
important function. You might also find
that setting an Emacs variable causes unexpected side effects. It’s much easier to fix these problems when
you have a copy of the original .Emacs file to compare with. This way, you can keep track of exactly
which changes you made.
You
might also find that you want to change some keystrokes around. This is most
commonly used when you have a command you use a lot which has an awkward
sequence, and one of the easier keystrokes is linked to a command you never
use. For example, I love keyboard
macros, but the command to play back the last macro C-x e is very awkward to do many times in succession. I almost never use the C-l (recenter) command. So, I added this line to my .Emacs file:
(global-set-key
"\C-l" 'call-last-kbd-macro)
Essentially,
you can use this as a template to map any particular command to any key. Of course, you need to be careful that you
don't overwrite some important command sequence like C-x. Note that I used “\C” to
mean control. If you want to map to the
meta key, use “\M”.
If you
have a command or two that you really like, you can permanently add them to
your Emacs implementation. Just define
the macros, make sure they are named correctly, and go to the buffer that
contains the .Emacs file (or load it up if necessary.) In that file, hit M-x insert-kbd-macro, then type the name of the macro you wish to add. This macro will now be available by name
whenever you start Emacs. If you want
to add a keystroke sequence to the command, you can use the global-set-key
trick. As an example, here's the code in my .Emacs file which adds the insert
and delete two spaces commands:
;;Andy's
custom macros
(defalias
'insert-2-spaces
(read-kbd-macro "2*SPC C-n C-a"))
;;map this
to C-c C-i when possible
(global-set-key
"\C-c\C-i" 'insert-2-spaces)
(defalias
'del-2-spaces
(read-kbd-macro "2*C-d C-n"))
;;map this
to C-c C-d when possible
(global-set-key
"\C-c\C-d" 'del-2-spaces)

A
portion of my .emacs file with the code to add my custom macros.
Again,
this is not intended as a tutorial in modifying Emacs, but you can see from
this example how much you can do if you're willing to study the code a little
bit.
You may
want to temporarily change the behavior of Emacs in a number of ways. You might want to change whether Emacs
automatically displays the date and time, or whether it uses normal or inverse
video. These and many of Emacs' other
behaviors are controlled by variables.
You can look at the info documentation (C-h i) to see which variables Emacs supports, and how you can change
them. To change a variable, you will
need to add a line to your .Emacs file that looks something like this:
(setq
display-time-and-date 1 Turns on date
display)
or
(setq display-time-and-date
nil Turns off date display)
You can
get a lot of mileage out of this simple trick, by digging through the online
documentation. You can change all kinds
of other things by manipulating variables, including whether Emacs makes a bell
sound or flashes on an error, whether matching parentheses will blink, whether
searches will be case-sensitive, and a lot more.
Finally,
as an example of the things you can do when you decide to go all out and learn
Emacs lisp, I'll show you a couple of other things I added to my .Emacs
file. These examples use a language
called lisp. Lisp is not terribly difficult to learn, but it is a very old
language, and it isn't as clean to work with as some of the newer
languages. In any case, here's some
simple examples:
The
following code asks the user for the title of a page, and generates a simple
HTML page with the title automatically placed with a centered heading. The page also contains my email and web
information so I don't have to add it to every page I write. Notice especially the way you can get input
from the user with the read-string command, and how you can print out a
multi-line string. This is probably my
favorite lisp program. Simple as it is,
I use it all the time.

The
HTML template lisp program
I use
this macro to automate in the creation of web pages that show
some external file. Since I teach
various forms of programming, I frequently want to be able to write an html
page that contains a link to a file, and then the source code of that file
within pre tags. This little program does that for me.

My
.emacs file with two interesting lisp functions visible
As you
can see, there is plenty to Emacs, and you've barely scratched the
surface. As a little bonus for getting
this far, I'm going to suggest you try a few other modes in Emacs. I think you'll find some of them a lot of
fun. Don't forget to use the C-h m command to find out mode-specific help!!
·
M-x
calendar
·
M-x
blackbox
·
M-x
dissociate
·
M-x
doctor
·
M-x
dunnet
·
M-x
gomoku
·
M-x
hanoi
·
M-x
life
·
M-x
solitaire
Still
pending
As you can see, Emacs provides a lot of power and
flexibility. In this chapter, you have
looked at the basic manipulation of Emacs and essential text editing. You have also examined some of the more powerful functions of
Emacs, including the search and replace functions, editing modes, and use of
multiple windows and buffers. You have
also looked briefly at how to customize Emacs to suit your own preferences, by
writing keyboard macros and by examining some simple Emacs lisp functions. Emacs is a very complex program, and it can take a
lot of time to learn everything it can do.
However,
you might find that once you spend the energy learning this program, it will
become one of your most important tools.
The humble
text editor can actually be one of the most powerful tools in computing. If you take advantage of Emac’s capabilities, it will
reward you by helping you maximize your computing skills.