Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Emacs editing environment, Part 2: Learn the essential modes and editing features of Emacs

Get going with this famous open source editor

Michael Stutz, Author, Consultant
Photo of Michael Stutz
Michael Stutz is author of The Linux Cookbook, which he also designed and typeset using only open source software. His research interests include digital publishing and the future of the book. He has used various UNIX operating systems for 20 years.

Summary:  One of the powerhouses of UNIX® computing, the open source Emacs editor is a large, complex application that does everything from edit text to function as a complete development environment. This tutorial, the second in a series, introduces you to some of the essential concept of modes, shows you some of the powerful text manipulation functions available, and teaches you how to use the built-in search, replace, and spellcheck facilities of Emacs.

View more content in this series

Date:  10 Apr 2007
Level:  Introductory

Activity:  20025 views
Comments:  

Abbrev mode

An Emacs abbrev is a special word defined by a particular string. When an abbrev is typed in a buffer (and Abbrev mode is turned on), the abbrev is expanded or replaced by the string it's defined by.

Abbrev mode (a minor mode) allows you to make quick shorthand for long strings or phrases, but you can probably think of other ways that you can use it, too.

Define an abbrev

The easy way to add abbrevs is to run one of the inverse-add abbrev functions, inverse-add-global-abbrev or inverse-add-local-abbrev. These functions let you define a word in the buffer as an abbrev; the first applies the abbrev to any buffer you open in your current Emacs session, and the second defines the abbrev only to buffers with the same major mode as the current buffer. The latter is useful for defining abbrevs that are only appropriate to certain modes, such as for long variable names in a buffer containing program source code.

Try defining an abbrev so that it works across all buffers:

  1. On the next new line, type an abbreviated word, li, so that point is at the end of the word (after the i).

  2. Run the inverse-add-global-abbrev function by typing C-x a i g.

  3. Define your abbrev where prompted in the minibuffer: Type Little lamb and press Enter.

Notice that the abbrev you type in the buffer is replaced by its definition and point moved to the beginning of the definition.

Now move point to a new line and make a new abbrev the way you just did, by typing an x (abbrevs aren't case-sensitive) and using the string He is to define it.

You've defined two abbrevs. However, as you can see from the mode line, Abbrev mode is off. Turn it on: Type M-x abbrev-mode. Erase those two lines with the definitions you just made and then type the code from Listing 1.


Listing 1. Sample lines with abbrevs
                    
Li, I'll tell thee,
Li, I'll tell thee:
x called by thy name,
For he calls himself a lamb.
x meek, and x mild;

The abbrevs for li and x expand as you type them so, when you're done, the buffer looks like Figure 3.


Figure 3. Abbrevs expanded in an Emacs buffer
Emacs buffer showing expanded abbrevs

This example showed how to define abbrevs that work in all buffers. To define an abbrev so that it only works in buffers set to the current mode, use C-x a i l instead.

Use a word as an abbrev definition

You can also define abbrevs for a single word in the buffer. This is particularly useful when you're writing program source code and you've just typed a long variable.

To define an abbrev for a word, use C-x a g when point is after the word. Once you've done that, you're prompted in the minibuffer for the abbrev to replace that word when Abbrev mode is on.

Try it now:

  1. Type He became a little child on a new line.

  2. When point is immediately after the word child, type C-x a g.

  3. Type c in the minibuffer and press Enter.

End the current line with a period and then type a few more lines to watch the expansion occur:

.
We are called by his name.
I a c, and thou a lamb,

Notice that Emacs recognizes the c followed by a comma as an abbrev, but not the c in called.

Likewise, to define an abbrev in such a way that it applies only to the current major mode, use C-x a l.

Erase an abbrev

To kill all the abbrevs you've defined in your session, use the kill-all-abbrevs function.

Try it: Type M-x kill-all-abbrevs.

Now none of the abbrevs you've defined (li, x, and c) expand to their definitions, in any buffer, regardless of the mode. Type two more lines to finish:

Little lamb, God bless thee!
Little lamb, God bless thee!

3 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=208259
TutorialTitle=Emacs editing environment, Part 2: Learn the essential modes and editing features of Emacs
publish-date=04102007
author1-email=stutz@dsl.org
author1-email-cc=mmccrary@us.ibm.com