old-www/LDP/LG/issue36/marsden.html

298 lines
12 KiB
HTML

<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<title>EMACSulation LG #36</title>
<link rev="made" href="mailto:emarsden@mail.dotcom.fr">
<meta name="keywords" content="emacs, abbrev, expansions">
<meta name="author" content="Eric Marsden">
<meta name="generator" content="Emacs">
<meta name="description" content="Emacs abbreviation mechanisms save you typing">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#A000A0"
ALINK="#FF0000">
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">EMACSulation</font></H1>
<H4>By <a href="mailto:emarsden@mail.dotcom.fr">Eric Marsden</a></H4>
</center>
<P> <HR> <P>
<blockquote><small>
This column is devoted to getting more out of Emacs, text editor
extraordinaire. Each issue I plan to present an Emacs extension
which can improve your productivity, make the sun shine more brightly
and the grass greener.
</small></blockquote>
<hr noshade>
<blockquote><font face="Helvetica">
Why is the word abbreviate so long?
</font></blockquote>
<h1>Time saving</h1>
<p> You've probably noticed that Emacs goes to a fair bit of trouble to
save you typing. The minibuffer offers a history mechanism which allows
you to recall and edit previous commands, and many minibuffer entry
prompts try to complete whatever you're typing when you hit
<tt>TAB</tt>. This behaviour was the inspiration for the readline and
history libraries, which are used in several shells and commandline
interpreters.
<p> This column is dedicated to another of these keystroke-saving features
in Emacs: the abbreviation facility. Do you get sick of typing in
repetitive phrases such as your company's name, or your phone number?
Abbreviations are here to save your fingers. For example, you could ask
Emacs to expand <strong>LAAS</strong> to <strong>Laboratoire d'Analyse
et d'Architecture des Syst&egrave;mes</strong>. The expansion happens
once you type a non word-constituent character after the abbreviation
(a space, for instance, though the exact definition of a word
separation depends on the mode you are using).
<p> This is the Emacs abbrev mechanism. You can either use a minor mode
called abbrev-mode, which will cause abbrevs to expand automatically
(you enable the minor-mode by saying <tt>M-x abbrev-mode</tt>), or you
can expand them on demand by saying <tt>C-x a e</tt> with the cursor
positioned after the abbreviation. Your abbreviations can be saved to a
file when you quit Emacs and reloaded automatically when you launch it:
</p>
<table border="0" bgColor="#E0E0E0" width="100%">
<tr><td>
<pre class="programlisting">
<font color="red">
;; if there is an abbrev file, read it in</font>
(if (file-exists-p abbrev-file-name)
(read-abbrev-file))</pre>
</td></tr></table>
<h2>Defining an abbrev</h2>
<p> To create an abbrev definition, type the abbreviation
(<strong>LAAS</strong> in the example above) in a buffer, say <tt>C-x a
i g</tt>, then enter the text you would like it to expand to in the
minibuffer. This slightly arcane sequence creates a <em>global
abbrev</em>, which will apply in all modes. Try it out by entering the
abbreviation and saying <tt>C-x a e</tt> (<tt>e</tt> for
<em>expand</em>). Emacs also allows you to create abbreviations which
will be active only in a specific mode by saying <tt>C-x a i l</tt>
instead (in a buffer which is already in the appropriate mode). <tt>M-x
list-abbrevs</tt> displays a list of all currently defined abbrevs.
<h2>Mail abbrevs</h2>
<p> Since the dawn of time, Unix mail programs have used the
<tt>~/.mailrc</tt> file to allow users to create their own email
aliases. The mail-abbrevs mechanism reads in the contents of this file
and defines abbreviations which will be expanded in the
<strong>To:</strong> and <strong>Cc:</strong> fields of any email you
compose in Emacs. Here is an example of the <tt>~/.mailrc</tt> alias
syntax: </p>
<pre>
alias dsssl dssslist@mulberrytech.com
alias cohorts rabah jarboui almeida behnia
alias bond "James Bond &lt;bond@guerilla.net&gt;"
</pre>
<p> There are other more sophisticated addressbook systems around, such as
Jamie Zawinski's <a
href="http://pw2.netcom.com/~simmonmt/bbdb/">BBDB</a>, but they
won't allow you to share aliases with other mailers. You can have
mail-abbrev minor mode activated whenever you compose an email in
Emacs using the following line in your <tt>~/.emacs</tt>: </p>
<table border="0" bgColor="#E0E0E0" width="90%">
<tr><td>
<pre class="programlisting">
<font color="red">
;; unnecessary if you use XEmacs</font>
(add-hook 'message-setup-hook 'mail-abbrevs-setup)</pre>
</td></tr>
</table>
<h2>Dynamic abbrevs</h2>
<p> The standard abbreviation facility requires you explicitly to register
your abbrevs, which is fine for things you type every week, but is a
hassle for expressions which only occur in one document. Emacs also
supports <em>dynamic abbrevs</em>, which try to guess the word you are
currently typing from the surrounding text. This is very useful for
programming in languages which encourage VeryLongVariableNames: you
only need type the variable name once, after which it suffices to type
the first few letters followed by <tt>M-/</tt>, and Emacs will try to
complete the variable name.
<p> To be very precise, dabbrev searches for the least distant word of
which the word under the cursor is a prefix, starting by examining
words in the current buffer before the cursor position, then words
after the cursor, and finally in all the other buffers in your Emacs.
If there are several possible expansions (<i>ie</i> the text you have
typed isn't a unique prefix), pressing <tt>M-/</tt> cycles though the
successive possibilities. Saying <tt>SPC M-/</tt> lets you complete
phrases which contain several words.
<p> Diehard vi users might be interested to read the <a
href="http://www.eskimo.com/~seldon/dabbrev-vi.txt">tribulations of a
user</a> who tried to implement a limited version of dabbrevs in vi.
<h2>Completion</h2>
<p> The Completion package, by Jim Salem, is similar in function to dynamic
abbrevs, but uses a different keybinding (<tt>M-RET</tt>) and a subtly
different algorithm. Rather than searching for a completion which is
close in the buffer, it starts by searching through words which you
have typed in recently (falling back to searching open buffers if this
fails). The history of recently used words is saved automatically when
you quit Emacs. To enable completion (you can use it instead of, or as
well as, dabbrevs), put the following in your <tt>~/.emacs</tt>: </p>
<table border="0" bgColor="#E0E0E0" width="90%">
<tr><td>
<pre class="programlisting">
(require 'completion)
(initialize-completions)</pre>
</td></tr>
</table>
<h2>Hippie Expand</h2>
<p> Filename completion in the minibuffer is a truly wonderful keystroke
saver, and you might find yourself wishing you could use it when
entering a filename in a regular buffer. Wish no longer: this is one of
the features offered by the fabulous hippie-expand package.
<p> Hippie-expand, by Anders Holst, is a singing and dancing abbrev
mechanism, which is capable of many different types of dynamic abbrevs.
It can expand according to:
<ul>
<li> file name: if you type <tt>/usr/X</tt> then hit the
expansion key, it will expand to <tt>/usr/X11R6/</tt>;
<li> exact line match: searches for a line in the buffer which has the
current line as a prefix;
<li> the contents of the current buffer, and other buffers on failure, just
like dabbrev;
<li> the contents of the kill-ring (which is where Emacs stores text that
you have <em>killed</em>, or ``cut'' in MacOS terminology, in
a circular buffer). Rather than typing <tt>M-y</tt> to cycle through
positions in the kill-ring, you can hippie-expand on the first word in
the killed text.
</ul>
<p> Hippie-expand is not active by default, so you need to bind it to a
key. Here's what I use: </p>
<table border="0" bgColor="#E0E0E0" width="90%">
<tr><td>
<pre class="programlisting">
(define-key global-map (read-kbd-macro "M-RET") 'hippie-expand)</pre>
</td></tr>
</table>
<p> Go forth and save keystrokes.
<h2>Feedback</h2>
<p> <a href="mailto:gtb@Eng.Sun.COM">Glenn Barry</a> sent me a comment on
the EMACSulation on gnuclient/gnuserv:
<blockquote>
<! b69454 >
<table bgColor="#BBDDFF" width="80%" border="0">
<tr><td>
Just read and enjoyed your article on gnuserv/gnuclient in the
Linux Gazette.
<p> But you forgot the use of gnuserv/gnuclient that makes it incredibly
useful; one can access their full running emacs session by logging-in
via a tty remotely (rlogin/telnet) and running &quot;gnuclient -nw&quot; ...
makes working from home a breeze (even over low speed (28.8) links).
<p> Note you do have to rlogin to the system running the emacs
w/gnuserv, as the <tt>gnuclient -nw</tt> does not work over the net
(at least that's what the man page says). It took me awhile to
figure this out so it would be nice to make sure folks know about
this great capability.
</td></tr>
</table>
</blockquote>
<p> The <tt>-nw</tt> switch asks Emacs to start up in console mode, which
makes it much more useable over a slow connection than using a remote
display with X11. Note that XEmacs is able to use ANSI colors on the
console or in an xterm, while GNU Emacs currently can't do color but
does offer a text-mode menubar.
<p> Glenn also gave an illustration of the power of ffap: he has customized
it to recognize Sun bug numbers under the cursor and dispatch a
dynamically generated URL to a web front end for their bug tracking
system.
<h2>Next time ...</h2>
<p> Next month I'll look at skeleton insertion and templating mechanisms
in Emacs. Don't hesitate to contact me at
<tt>&lt;emarsden@mail.dotcom.fr&gt;</tt> with comments, corrections or
suggestions (what's <em>your</em> favorite couldn't-do-without Emacs
extension package?). <code>C-u 1000 M-x hail-emacs</code> !
<p> <strong>PS</strong>: Emacs isn't in any way limited to Linux, since
implementations exist for many other operating systems (and some
systems which only halfway operate). However, as one of the leading
bits of free software, one of the most powerful, complex and
customizable, I feel it has its place in the <i>Linux Gazette</i>.
<!--===================================================================-->
<P> <HR> <P>
<A HREF="../issue25/marsden.html">EMACSulation #1, February 1998</A><BR>
<A HREF="../issue26/marsden.html">EMACSulation #2, March 1998</A><BR>
<A HREF="../issue27/marsden.html">EMACSulation #3, April 1998</A><BR>
<A HREF="../issue29/marsden.html">EMACSulation #4, June 1998</A><BR>
<A HREF="../issue31/marsden.html">EMACSulation #5, August 1998</A>
<!--===================================================================-->
<P> <hr> <P>
<center><H5>Copyright &copy; 1999, Eric Marsden <BR>
Published in Issue 36 of <i>Linux Gazette</i>, January 1999</H5></center>
<!--===================================================================-->
<P> <hr> <P>
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="./defurne1.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="./defurne2.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P>
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->