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:  20061 views
Comments:  

Text manipulation

In this section, learn some of the special commands and key bindings for editing text, most of which work regardless of the current major mode.

Indent and fill text

Regions can be automatically indented in different ways. The indent-rigidly function, which you run by typing C-x C-i, indents all lines in the region to the right by one space.

Try it:

  1. Type C-Space to set the mark at the bottom of the buffer.

  2. Move point to the beginning of the line that reads, "Little lamb, I'll tell thee," to mark the last 10 lines in the buffer as the region.

  3. Type C-x C-i to indent the region one space.

  4. Type C-x C-i again to indent the region another space.

Just as you can run indent-rigidly multiple times, you can give a certain number of spaces to indent by preceding it with C-u followed by a number; use a negative number to move the region toward the left.

Try it:

  1. Indent the region by 10 more spaces: Type C-u 10 C-x C-i.

  2. Take the indentation back five spaces: Type C-u -5 C-x C-i.

After this, your buffer should look like Figure 4.


Figure 4. Inserting rigid indentation
Inserting rigid indentation

The indent-rigidly function is also bound to the C-x Tab.

To fill the region, justify the text with a ragged right margin and run the fill-region function. A similar function, fill-paragraph, applies to the current paragraph. It has a keyboard equivalent of M-q.

Try it: Type M-x fill-region. Notice that once the region is filled, point is moved to just after the region.

Like any of the Emacs formatting commands, the commands for indentation and filling can be undone with the undo function, which was described in the first tutorial in this series. Try it once now to undo the region fill: Type C-_ once.

Just as you can add or remove vertical space with indentations, you can also take out any existing horizontal space. To do this, use the delete-horizontal-space function, M-\ (that's the Meta key followed by the \ key). It removes all leading whitespace between the first nonspace character anywhere to the left of point and the first nonspace character to the right of point, no matter where point happens to be in the space.

Go ahead and try it. Take out all the leading spaces in the buffer by moving point to the space on each line and typing M-\ before moving to the next line with spaces.

To add a line of vertical space, you can always press Enter, but doing so also moves point. To add vertical space without moving point, run the open-line function, which is bound to C-o.

Try it: Move down eight lines from the top of the buffer so that point is at the beginning of the line reading "Little lamb, I'll tell thee," and type C-o. Now your buffer should look like Figure 5.


Figure 5. Remove horizontal space and insert vertical space
Remove horizontal space and insert vertical             space

Transpose text

Emacs has commands for transposition, which allow you to exchange the characters, words, or lines immediately after point with those immediately before point. These are good commands for making quick corrections in text.

Use C-t, the transpose-chars function, to transpose the single character before point and the single character after point; use M-t, the transpose-words function, to transpose the single words before and after point.

Try it now:

  1. Move point to the o in For and then type C-t to transpose the o and F characters.

  2. Type C-t again to transpose the r and F characters.

  3. Move point back to the F and type C-t once to move the F back one character.

  4. Move point back to the F again and type C-t once more to move the F back to the beginning of the line.

  5. Type M-t to transpose the words For and he.

  6. Type M-t again to transpose the words For and calls.

  7. Move point between he and For and type M-t again to transpose them.

  8. Move point between calls and For and type M-t again to transpose them.

  9. Type M-t one last time to transpose calls and he.

The transpose-lines function, C-x C-t, transposes the line at point and the line before it.

Try it: Move point to the beginning of the line that begins "I a child" and type C-x C-t.

Note that you can also precede any of these commands with a numeric value by using the universal argument, C-u: C-u 2 C-x C-t transposes the line at point with the line two lines ahead of point.

Another useful command, delete-indentation, joins the line at point with the previous line, separated by a space character. It's bound to M-^, which you type by pressing and holding Meta and using the Shift key to type a caret (^). An alternate, C-1 M-^, joins the current line with the line that follows it.

Convert case

There are several Emacs functions for converting case. The uppercase-word function (bound to M-u) converts to uppercase letters of all the text beginning from point forward to the end of the word. Similarly, downcase-word (bound to M-l) converts everything from point to the end of the word to lowercase letters.

Try these on the buffer:

  1. Move point to the line that begins with "For he calls" and type M-c M-c M-l M-c M-l M-c so that the line is properly capitalized.

  2. Move point down to the line that begins with "We are called" and type M-u M-u M-u M-u M-u M-u so that the entire line is converted to uppercase letters.

  3. Type C-a to move point to the beginning of the line and type C-c C-l C-l C-l C-l C-c C-l so that the line is properly capitalized.

You can operate on words before point by preceding any of these commands with the negative-argument function, M--, which you type by pressing and holding the Meta key and then pressing a hyphen character (-). You can apply the case-changing commands to the region by using the downcase-region and upcase-region functions, which are bound to C-x C-l and C-x C-u, respectively.

Summary of text manipulation commands

Table 3 lists the various text manipulation commands you've just learned and describes their meanings.


Table 3. Emacs text manipulation commands
BindingCommand or functionDescription
C-x C-i, C-x Tab indent-rigidly This command indents lines in the region (or at point).
fill-region This command fills all paragraphs in the region.
M-q fill-paragraph This command fills the single paragraph at point.
M-\ delete-horizontal-space This command removes any horizontal space to the right and left of point.
C-o open-line This command opens a new line of vertical space below point, without moving point.
C-t transpose-chars This command transposes the single characters to the right and left of point.
M-t transpose-words This command transposes the single words to the right and left of point.
C-x C-t transpose-lines This command transposes the line at point with the line before it.
M-^ delete-indentation This command joins the line at point with the previous line. Preface with C-1 to join the line at point with the next line.
M-u uppercase-word This command converts the text at point to the end of the word to uppercase letters.
M-l downcase-word This command converts the text at point to the end of the word to lowercase letters.
C-x C-l downcase-region This command converts the region to lowercase letters.
C-x C-u upcase-region This command converts the region to uppercase letters.

4 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