ALINK="#FF0000">

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


EMACSulation

By Eric Marsden


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.

Why is the word abbreviate so long?

Time saving

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 TAB. This behaviour was the inspiration for the readline and history libraries, which are used in several shells and commandline interpreters.

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 LAAS to Laboratoire d'Analyse et d'Architecture des Systèmes. 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).

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 M-x abbrev-mode), or you can expand them on demand by saying C-x a e 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:

    
    ;; if there is an abbrev file, read it in
    (if (file-exists-p abbrev-file-name)
       (read-abbrev-file))

Defining an abbrev

To create an abbrev definition, type the abbreviation (LAAS in the example above) in a buffer, say C-x a i g, then enter the text you would like it to expand to in the minibuffer. This slightly arcane sequence creates a global abbrev, which will apply in all modes. Try it out by entering the abbreviation and saying C-x a e (e for expand). Emacs also allows you to create abbreviations which will be active only in a specific mode by saying C-x a i l instead (in a buffer which is already in the appropriate mode). M-x list-abbrevs displays a list of all currently defined abbrevs.

Mail abbrevs

Since the dawn of time, Unix mail programs have used the ~/.mailrc 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 To: and Cc: fields of any email you compose in Emacs. Here is an example of the ~/.mailrc alias syntax:

    alias dsssl        dssslist@mulberrytech.com
    alias cohorts      rabah jarboui almeida behnia
    alias bond         "James Bond <bond@guerilla.net>"

There are other more sophisticated addressbook systems around, such as Jamie Zawinski's BBDB, 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 ~/.emacs:

    
    ;; unnecessary if you use XEmacs
    (add-hook 'message-setup-hook 'mail-abbrevs-setup)

Dynamic abbrevs

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 dynamic abbrevs, 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 M-/, and Emacs will try to complete the variable name.

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 (ie the text you have typed isn't a unique prefix), pressing M-/ cycles though the successive possibilities. Saying SPC M-/ lets you complete phrases which contain several words.

Diehard vi users might be interested to read the tribulations of a user who tried to implement a limited version of dabbrevs in vi.

Completion

The Completion package, by Jim Salem, is similar in function to dynamic abbrevs, but uses a different keybinding (M-RET) 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 ~/.emacs:


    (require 'completion)
    (initialize-completions)

Hippie Expand

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.

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:

Hippie-expand is not active by default, so you need to bind it to a key. Here's what I use:


    (define-key global-map (read-kbd-macro "M-RET") 'hippie-expand)

Go forth and save keystrokes.

Feedback

Glenn Barry sent me a comment on the EMACSulation on gnuclient/gnuserv:

Just read and enjoyed your article on gnuserv/gnuclient in the Linux Gazette.

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 "gnuclient -nw" ... makes working from home a breeze (even over low speed (28.8) links).

Note you do have to rlogin to the system running the emacs w/gnuserv, as the gnuclient -nw 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.

The -nw 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.

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.

Next time ...

Next month I'll look at skeleton insertion and templating mechanisms in Emacs. Don't hesitate to contact me at <emarsden@mail.dotcom.fr> with comments, corrections or suggestions (what's your favorite couldn't-do-without Emacs extension package?). C-u 1000 M-x hail-emacs !

PS: 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 Linux Gazette.


EMACSulation #1, February 1998
EMACSulation #2, March 1998
EMACSulation #3, April 1998
EMACSulation #4, June 1998
EMACSulation #5, August 1998


Copyright © 1999, Eric Marsden
Published in Issue 36 of Linux Gazette, January 1999


[ TABLE OF CONTENTS ] [ FRONT PAGE ]  Back  Next