2.7.1. Emacs
Emacs is a highly customizable editor-indeed, it has been customized to the point where it is more like an operating system than an editor! Many developers and sysadmins do in fact spend practically all their time working inside Emacs, leaving it only to log out.
It is impossible even to summarize everything Emacs can do here, but here are some of the features of interest to developers:
-
Very powerful editor, allowing search-and-replace on both strings and regular expressions (patterns), jumping to start/end of block expression, etc, etc.
-
Pull-down menus and online help.
-
Language-dependent syntax highlighting and indentation.
-
Completely customizable.
-
You can compile and debug programs within Emacs.
-
On a compilation error, you can jump to the offending line of source code.
-
Friendly-ish front-end to the
info
program used for reading GNU hypertext documentation, including the documentation on Emacs itself.
-
Friendly front-end to
gdb
, allowing you to look at the source code as you step through your program.
And doubtless many more that have been overlooked.
Once it is installed, start it up and do
C-h t
to read an Emacs tutorial-that means hold down
control
, press
h
, let go of
control
, and then press
t
. (Alternatively, you can use the mouse to select
from the
menu.)
Although Emacs does have menus, it is well worth learning the key bindings, as it is much quicker when you are editing something to press a couple of keys than to try to find the mouse and then click on the right place. And, when you are talking to seasoned Emacs users, you will find they often casually throw around expressions like “M-x replace-s RET foo RET bar RET” so it is useful to know what they mean. And in any case, Emacs has far too many useful functions for them to all fit on the menu bars.
Fortunately, it is quite easy to pick up the key-bindings, as they are displayed next to the menu item. My advice is to use the menu item for, say, opening a file until you understand how it works and feel confident with it, then try doing C-x C-f. When you are happy with that, move on to another menu command.
If you cannot remember what a particular combination of keys does, select
from the
menu and type it in-Emacs will tell you what it does. You can also use the
menu item to find out all the commands which contain a particular word in them, with the key binding next to it.
By the way, the expression above means hold down the
Meta
key, press
x
, release the
Meta
key, type
replace-s
(short for
replace-string
-another feature of Emacs is that you can abbreviate commands), press the
return
key, type
foo
(the string you want replaced), press the
return
key, type bar (the string you want to replace
foo
with) and press
return
again. Emacs will then do the search-and-replace operation you have just requested.
If you are wondering what on earth
Meta
is, it is a special key that many UNIX® workstations have. Unfortunately, PC’s do not have one, so it is usually
alt
(or if you are unlucky, the
escape
key).
Oh, and to get out of Emacs, do
C-x C-c
(that means hold down the
control
key, press
x
, press
c
and release the
control
key). If you have any unsaved files open, Emacs will ask you if you want to save them. (Ignore the bit in the documentation where it says
C-z
is the usual way to leave Emacs-that leaves Emacs hanging around in the background, and is only really useful if you are on a system which does not have virtual terminals).
2.7.2. Configuring Emacs
Emacs does many wonderful things; some of them are built in, some of them need to be configured.
Instead of using a proprietary macro language for configuration, Emacs uses a version of Lisp specially adapted for editors, known as Emacs Lisp. Working with Emacs Lisp can be quite helpful if you want to go on and learn something like Common Lisp. Emacs Lisp has many features of Common Lisp, although it is considerably smaller (and thus easier to master).
However, there is no need to actually know any Lisp to get started with configuring Emacs, as I have included a sample
.emacs
, which should be enough to get you started. Just copy it into your home directory and restart Emacs if it is already running; it will read the commands from the file and (hopefully) give you a useful basic setup.
2.7.4. Extending the Range of Languages Emacs Understands
Now, this is all very well if you only want to program in the languages already catered for in
.emacs
(C, C++, Perl, Lisp and Scheme), but what happens if a new language called "whizbang" comes out, full of exciting features?
The first thing to do is find out if whizbang comes with any files that tell Emacs about the language. These usually end in
.el
, short for "Emacs Lisp". For example, if whizbang is a FreeBSD port, we can locate these files by doing
% find /usr/ports/lang/whizbang -name "*.el" -print
and install them by copying them into the Emacs site Lisp directory. On FreeBSD, this is
/usr/local/shared/emacs/site-lisp
.
So for example, if the output from the find command was
/usr/ports/lang/whizbang/work/misc/whizbang.el
# cp /usr/ports/lang/whizbang/work/misc/whizbang.el /usr/local/shared/emacs/site-lisp
Next, we need to decide what extension whizbang source files have. Let us say for the sake of argument that they all end in
.wiz
. We need to add an entry to our
.emacs
to make sure Emacs will be able to use the information in
whizbang.el
.
Find the auto-mode-alist entry in
.emacs
and add a line for whizbang, such as:
...
("\\.lsp$" . lisp-mode)
("\\.wiz$" . whizbang-mode)
("\\.scm$" . scheme-mode)
...
This means that Emacs will automatically go into
whizbang-mode
when you edit a file ending in
.wiz
.
Just below this, you will find the font-lock-auto-mode-list entry. Add
whizbang-mode
to it like so:
;; Auto font lock mode
(defvar font-lock-auto-mode-list
(list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'whizbang-mode 'lisp-mode 'perl-mode 'scheme-mode)
"List of modes to always start in font-lock-mode")
This means that Emacs will always enable
font-lock-mode
(ie syntax highlighting) when editing a
.wiz
file.
And that is all that is needed. If there is anything else you want done automatically when you open up
.wiz
, you can add a
whizbang-mode hook
(see
my-scheme-mode-hook
for a simple example that adds
auto-indent
).