GNU Emacs Introduction
1.
Introduction
What is GNU Emacs?
In short Emacs is an extensible, customizable,
self-documenting real-time display editor, which says
everything and nothing. At its core it is an interpreter
for Emacs Lisp, an expression-oriented language. All
functions are written in this language and a user can dive
into that or not, just as he wishes. If he does he will
find a well-documented structure of functions he can
customise and extend, which makes Emacs a flexible tool for
all editing tasks. But it is not limited to that: Reading
news or mail, chatting over IRC, organising tasks and
projects or using it as development environment with support
for external debuggers and compilers in many different
languages.
The original version of Emacs is nowadays called GNU Emacs
as it is maintained by the GNU project, unlike some forks
that appeared in the long history and some are still
existant today. The most prominent is XEmacs, which is not
the X version but a totally independent project. GNU Emacs
has support for X and text mode itself to clarify a popular
misunderstanding.
By the way, the name Emacs is derived from Editor MACroS
despite other rumours and interpretations. And we should an
initial warning: Emacs is really old in computer terms, but
still a powerful tool, and there is nothing like it out
there. So prepare yourself for a completely usability
experience and a lot of functionality, which might seem
overwhelming at first. Try to use the subset you need and
extend your knowledge as you see fit.
Scope of this document
This guide describes the basic concepts of GNU Emacs itself
and the specialties for a Gentoo installation to manage it
and the possibly installed extensions packets. A complete
documentation is not possible and better done by the
official manual (which is not complete either) and various
third-party documents, see further reading suggestions at
the bottom. We are sure you can use every possible task and
Emacs as a search term in your favourite search engine to
find a solution in this powerful beast.
Most intrinsic properties described here are also valid for
XEmacs (to be found as app-editors/xemacs in the
repository), but in-Gentoo managment differs in some
details, so do not wonder if the described behaviour does
not occur with XEmacs.
2.
Installation and setup
Pre-installation considerations
So you decided to install GNU Emacs on your system. There
are several choices you can make, depending on your needs:
All versions of app-editors/emacs can be installed in
parallel, switching is achieved by the user through the
emacs Eselect module. All are officially supported, although
only version 21 and above are able to handle external
extionsion packages or optional Emacs support activated with
USE=emacs for some packages. Please note that some packages
need version 22 or higher but those cases are rare and you
will note on emerge (the process will stop very early with a
meaningful message).
As a new major version of GNU Emacs brings a lot of changes,
an upgrade should not be done automatically by the package
manager just because there is a new version. Emacs 23 for
example can be emerged but the active version stays 22 until
the user chooses otherwise.
Apart from that it comes in handy to have a small and lean
Emacs (18 for example) for quick editing sessions over a
remote connection at hand. There are several editors which
support Emacs key bindings (so called micro-Emacsen) but
don't have the full power, a
list
is provided by the Emacs team.
Some readers might have noticed already
the app-editors/emacs-cvs package which is a live
version of GNU Emacs. This means you get the tip of the
current development, freshly served to your computer. We
spare you the glory details as you should use it only if you
are curious and willing to deal with the typical breakage
that appears during development.
Installing
First, you must install Emacs. TODO: Be more verbose
Code Listing 2.1: Installation command, just have a peek |
# emerge --pretend --verbose app-editors/emacs
|
| Flag |
Description |
Notes |
| alsa |
Determine if ALSA should be used |
Emacs autodetects ALSA. That test is removed by the ebuild if
the flag is not set. This is for cases where ALSA is installed but
the user does not wish support for it in Emacs. |
| gif, png, jpeg, tiff, xpm |
Add support for every individual image format |
If xpm support is disabled, all logos, icons etc. in
Emacs are displayed in grayscale. |
| gtk, motif, Xaw3d |
Different windowing toolkits (menu bar etc.) |
When The Gimp Toolkit+ (gtk) toolkit is activated
along with alternative ones, GTK+ is chosen. This is in
sync with upstream's wishes. |
| gzip-el |
Zip up all el files. |
This is normally not relevant for the end user if he
has no disk space problems. |
| hesiod |
Use the Hesiod name service system. |
|
| kerberos |
Support for the Kerberos network authentication protocol. |
|
| leim |
Extended methods for input encodings (Emacs 21 only). |
|
| nls |
This makes use of gettext, an easy way to provide translations
of strings (Emacs 21 only). |
|
| sendmail |
Build with support for mail transfer agent (Emacs 21 only). |
|
| sound |
Control the availability of sound support. |
|
| source |
Install the C source files and make them available in the
internal documentation system of GNU Emacs. |
|
| toolkit-scroll-bars |
Instead of the internal scroll bars, the ones from the windowing
toolkit are used. |
You will lose some functionality (split windows by clicking on
the scroll bar for example). |
| X |
Let Emacs use an X session if available. Text mode can always
be forced. |
|
You could unpack the Emacs sources and run ./configure
--help to see the optional support. If you don't use the X
or gtk flags, you will rely on the keyboard to use Emacs.
Otherwise, emacs -nw is the terminal mode.
Eselect
All Emacs versions can be installed
alongside each other on the same system without collisions.
Calling /usr/bin/emacs redirects (by a symbolic link) to
the Emacs version you chose to be started. Switching the
default Emacs is done through the emacs eselect module, which is
presented in the following example.
Code Listing 2.2: Eselect |
# emerge app-admin/eselect
# eselect emacs list
Available Emacs symlink targets:
[1] emacs-21 *
[2] emacs-22
# eselect emacs set 2
Switching emacs to emacs-22 ...
Switching ctags to exuberant-ctags ...
Switching etags to etags-emacs-22 ...
# eselect emacs list
Available Emacs symlink targets:
[1] emacs-21
[2] emacs-22 *
|
Note:
All installed precompiled Emacs support files may be
incompatible with the version you switched to. If you
experience problems, please
use app-admin/emacs-updater (described in the next
section).
|
app-admin/emacs-updater
Initially this tool was intended as a short-term migration
tool for some internal changes, but is now a mean to handle
up- and downgrades of Emacs cleanly. First you need to
install it.
Code Listing 2.3: Installing app-admin/emacs-updater |
# emerge app-admin/emacs-updater
|
Without any arguments /usr/sbin/emacs-updater will
look for site-files (we will come of that to the exact
definition later) in a obsolete location and recompile all
Emacs support files of additional packages that were not
built with the current active Emacs version. That way a up-
or downgrade of Emacs is easily possible if the format of
the compiled files is differing (which is the case between
21 and anything up). After switching the active version
with the Eselect module you can run emacs-updater afterwards
and will not experience strange run-time errors.
An unconditional rebuild of all Emacs support files is also
possible with
Code Listing 2.4: Installing app-admin/emacs-updater |
# /usr/sbin/emacs-updater --action all
|
Basic editing commands
After installing Emacs itself we will describe some basics
in editing which is quite different from anything you might
know. TODO: What's a mode? Describe some concepts
Client/Server
TODO: Describe emacs-daemon
Emacs can work as a server for emacsclient. To use this type M-x
server-start in a running Emacs session. Then it's emacsclient
file1 file2 file3 to edit your files or mail.
Built-in tutorial
There is a tutorial for Emacs available.
Code Listing 2.5: Start of tutorial |
M-x help-with-tutorial
|
That is Meta then x. The Meta key is escape or possibly Alt. Next
you need to type help and use tab twice to see the completions.
3.
Additional packages
Separate package or USE=emacs?
Installation
Some types of files have modes created for them. If you edit
Python or PHP code, this piece of information is for you. This
is a convenient way to install elisp files (.el and
byte-compiled .elc) thanks to the Emacs team! This is done as
root. Note the # prompt. TODO: Describe canonical
site-init loading
Code Listing 3.1: Installation of extension packages |
# emerge --pretend --verbose app-emacs/python-mode
# emerge --pretend --verbose app-emacs/php-mode
Cscope can work with Emacs very conveniently.
# echo "dev-util/cscope emacs" >> /etc/portage/package.use
That will append the quoted text to the file "/etc/portage/package.use".
# emerge dev-util/cscope
|
Loading on start-up
4.
Basics
Macros
An introduction to Emacs would not be complete without a section
on macros. This is "Editor MACroS". In a buffer, type
control-x, ( to begin. Then type what you want in the macro.
You are recording keystrokes. Now type control-x, ) to finish
the macro. Naming the macro is next with M-x
name-last-kbd-macro. You could insert-kbd-macro into a .emacs
file if you find it very useful. Perhaps you want to use that
macro 10 times in a row. This is awkward, but control-u, 10,
M-x mymacro.
Emacs Lisp
You could use the lisp interpreter to make lists or do some
simple math. Type these in and then type control-x, control-e
with the cursor at the end of them. Watch the minibuffer at the
bottom of the screen.
Code Listing 4.1: Emacs Lisp code example |
(list 'a 'b 'c)
(+ 1 2 3)
(* (* 2 2) (/ 22 7))
(message "%s" "This is an introduction to using Emacs in Gentoo.")
|
Understanding how to navigate the cursor over lists is quite
useful. With the cursor pointed on a [, (, or {, use M-C-f to
go to the end. Use M-C-b to go to the beginning of the list.
That is escape, control-f and escape, control-b
respectively.
You could load your elisp.
Code Listing 4.2: Load an elisp file |
M-x load-file
|
Now, you may want to clean up your code or make replacements in a
function. Click and drag or use control-space to set a mark.
Code Listing 4.3: Activate visual mark indicator |
M-x transient-mark-mode
|
That will toggle the highlighting of selected text. Now, M-% will
prompt you to search and replace or query-replace. Also,
global-font-lock-mode will toggle the syntax highlighting;
indent-region will nicely indent nested code blocks or HTML. A quick
comment comes from M-; or escape-semicolon.
Invoking Processes
Emacs can start subprocesses, like a shell or a debugger or even find and grep.
Code Listing 4.4: Start some connectors |
M-x shell
M-x gdb
M-x find-grep
|
Figure 4.1: Search in Emacs |
 |
Note: Emacs can be used on compressed text in the gzip and bzip2 formats by invoking decompressors. |
Emacs and Bash Equivalents
Bash has some keystrokes in common with Emacs. You can
search your shell history with control-r but not control-s.
If you try control-s in bash, it will hide your typing. You
can type control-q to see input to bash again. To cancel a
search, type control-g. Just like Emacs, you can kill text
several times with control-k and get it back with
control-y. You can then use escape-y repetitions. You can
suspend Emacs with control-z. Try the jobs command to see
what is suspended. Then type fg or fg 1 to get back to
Emacs. Delete a whole word with escape-backspace or
escape-d. Undo with control-/ and transpose two mistyped
characters with control-t.
Complete Text
Now is where some completion becomes useful. There is M-/ to complete a string and also some elisp that does much more.
Code Listing 4.5: Start-up of hippie-expand |
M-x hippie-expand
|
This has try-expand-list, try-expand-line,
try-complete-file-name and many other things. It will cycle
through trying to match what is immediately on the left of the
cursor. It is much nicer to bind this to a key.
Code Listing 4.6: Start the global-set-key macro |
M-x global-set-key
|
This could be persistent in a .emacs file.
Code Listing 4.7: Example fort the contents of the .emacs file |
$ echo "(global-set-key [(f2)] 'hippie-expand)" >> ~/.emacs.d/init.el
|
Note: man man has this tip: (global-set-key [(f1)] (lambda () (interactive) (manual-entry (current-word)))) |
Remove Comments
One common thing for Gentoo users is to strip comments from
configuration files. This is convenient in Emacs with flush-lines
Code Listing 4.8: Start the flush-lines macro |
M-x flush-lines
^#
That will match lines beginning with a # which is a comment in most /etc configuration files.
|
5.
Useful functions in GNU Emacs
Dired
Lets start by looking at some files.
Code Listing 5.1: Open a file with Emacs |
Control-x, control-f, enter
|
Now you have dired, the directory editor. When you press enter on
directories, you traverse the file structure. It is possible to
quickly back up the tree with ^, shift-6. Over the course of editing,
you may open many files.
Code Listing 5.2: Switch buffers |
Control-x, control-b
|
Now you may wish to do replacements in many files.
Code Listing 5.3: Command completion in action |
M-x dired<TAB><TAB>
M-x command-apropos
|
Now you see what is available. Here is one strategy.
Code Listing 5.4: Code Sample |
M-x find-grep-dired
%-m c$
M-x dired-do-query-replace-regexp
|
That will split your screen and show the buffers that are open. When you want to split the screen, use control-x followed by a number.
Code Listing 5.5: Code Sample |
C-x 0
C-x 1
C-x 2
C-x 3
|
Speedbar for Debugging and Navigation
If you're coding C, and you use gcc -g for debugging
symbols, Emacs can split your screen and follow the current line
of source code. If you use Emacs with X try speedbar.
Code Listing 5.6: Code Sample |
M-x speedbar
|
Now watch variables change just like any other development
environment. You could also browse directories in speedbar or do
other things by right-clicking.
Ediff
Since Gentoo respects your /etc/ configuration files, you may
like ediff for file foo and ._cfg0000_foo.
Code Listing 5.7: Code Sample |
You could run M-x find-dired with an argument like this: -name \._cfg*
Control-x, control-f, control-a, control-k, /etc, enter.
M-x ediff
|
6.
Sample .emacs
TODO: Describe the settings and add the canonical loading
Code Listing 6.1: Code Sample |
$ cat ~/.emacs.d/init.el
(prefer-coding-system 'utf-8)
(setq transient-mark-mode t)
(show-paren-mode t)
(setq column-number-mode t)
(global-set-key [(f5)] (lambda() (interactive) (woman (current-word))))
(global-set-key [(f6)] 'hippie-expand)
(global-set-key [(f7)] 'replace-regexp)
(global-set-key [(f8)] 'flush-lines)
(global-set-key [(f9)] 'dired-do-delete)
# cat /root/.emacs.d/init.el
(load "/home/username/.emacs.d/init.el")
|
7.
Further reading
| Resource |
Comment |
|
Richard M. Stallman;
GNU Emacs Manual; 16th edition (for version 22);
ISBN 1-882114-86-8; GNU Press
|
The official handbook of Emacs, also shipped with the editor.
This covers the usage of Emacs, not the programming or deep internals.
|
|
Robert J. Chassell;
An Introduction to Programming in Emacs Lisp;
ISBN 1-882114-56-6; GNU Press
|
A primer on Elisp programming (very basic, but very good).
|
|
Various authors;
GNU Emacs Lisp Reference Manual
|
All information needed for Elisp programming, dense and extensive,
but not for beginners.
|
|
Craig A. Finseth;
The Craft of Text Editing:
Emacs for the Modern World; ISBN 0-387-97616-7; Springer-Verlag
|
Background information about user interfaces and the ergonomics of
text editing.
|
The contents of this document are licensed under the Creative Commons -
Attribution / Share Alike license.
|