Learn the Essential Modes and Editing Features of Emacs Get Going with This Famous Open Source Editor
Total Page:16
File Type:pdf, Size:1020Kb
Emacs editing environment, Part 2: Learn the essential modes and editing features of Emacs Get going with this famous open source editor Skill Level: Introductory Michael Stutz ([email protected]) Author Consultant 10 Apr 2007 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. Section 1. Before you start Learn what to expect from this tutorial, and how to get the most out of it. About this series The Emacs editing environment is a favorite of UNIX® developers. It's known around the world as the king of editors, but many users find it has a bit of a learning curve. The Emacs environment doesn't seem intuitive at first glance, and it doesn't work like other editors and word processors. But learning Emacs doesn't have to be difficult. Once you get going, you'll see how intuitive it is and become more comfortable with it after each use. This tutorial series shows you the way, taking you from the basics of Emacs, such as its features, philosophy, key-command layout, and methods for editing text, through many of its powerful editing features. After completing this series, you'll be able to comfortably use Emacs for everyday editing, be well on your way to Emacs proficiency, and have a good feel for many of Learn the essential modes and editing features of Emacs © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 31 developerWorks® ibm.com/developerWorks the advanced capabilities of Emacs. About this tutorial This tutorial, the second in a series, demonstrates how to use some of the vital features for text editing and introduces the concept of modes, showing you what to use them for, how to invoke them, and which popular modes you're liable to use in your normal editing. It also describes a special mode for defining abbreviations as useful shorthand and explains how to use some of the text-editing features that work regardless of mode -- including important text manipulation commands, the search and replace facility, and the built-in spell checker. Objectives The primary objective of this tutorial is to take users who are already familiar with the basics of the Emacs editor, such as its manner of keyboard input and the paradigm of buffers, and illustrate some of its essential but more intermediate features, including editing modes, incremental search, and other important Emacs text manipulation commands and facilities. After completing this tutorial, you will have a firm knowledge of the editing modes of Emacs and how to utilize these various text-manipulation features in Emacs. Prerequisites The only prerequisite for this tutorial is that you already have a basic understanding of Emacs, which you can gain by taking the first tutorial in this series. Although this tutorial is written for all levels of UNIX expertise, it's helpful if you have at least a rudimentary understanding of the UNIX filesystem: • Files • Directories • Permissions • Filesystem hierarchy System requirements This tutorial requires a user account on any UNIX-based system that has a recent copy of Emacs installed. There are several varieties of Emacs; the original and most popular is GNU Emacs, which is published online by the GNU Project (see Resources). Learn the essential modes and editing features of Emacs Page 2 of 31 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® You should have a recent copy of GNU Emacs -- one that is at version 20 or greater. Versions 20 and 21 are the most commonly available, and development snapshots of version 22 are also available. This tutorial works with any of these versions for Emacs. If your system is running something older, it's time to upgrade. To know what version of Emacs you have running, use the GNU-style --version flag: $ emacs --version GNU Emacs 22.0.91.1 Copyright (C) 2006 Free Software Foundation, Inc. GNU Emacs comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of Emacs under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING. $ Section 2. Editing modes Emacs is classified as a modeless editor, meaning that unlike editors such as vi, there is no special command mode for running editor commands or insert mode for inserting text into the buffer -- as you saw in the previous tutorial in this series, both commands and text insertion can be done at any time. However, Emacs does have its own kind of editing modes, which are functions that extend its capabilities or change the way some features work. Modes are generally written for editing a certain type or class of data, such as regular documents (written in any of the Indo-European languages), source code in a particular computer programming language (C, Fortran, Lisp, and so forth), text formatted a certain way (outlines, e-mail messages, usenet articles, character-based illustrations, and so forth) or in a markup language (Hypertext Markup Language (HTML), Nroff, TeX, and Extensible Markup Language (XML)). There's even a mode for editing non-text (binary) data. Additionally, many special modes for other kinds of data and system processes are accessible through Emacs, including network connections and Internet Relay Chat (IRC), shell sessions, and the UNIX filesystem itself. Modes are classified as either major or minor. The major mode dictates the main editing behavior and is applied only to that buffer in the current editing session. Every buffer always has one, and only one, major mode active at any one time. Although a buffer can only have one major mode active at any time, you can switch between major modes whenever you like. Specialized major modes offer extra functionality and aids (such as context highlighting and colorization) that can help when you're editing certain kinds of documents, but you're not required to select a particular mode to edit a certain kind of file or document -- a C program source code file might be edited in any mode for editing text just as it might be done in the special mode available for editing C programs. Learn the essential modes and editing features of Emacs © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 31 developerWorks® ibm.com/developerWorks Minor modes normally offer some feature or capability not associated with any particular major mode. Think of them as toggles that control features: Calling a minor mode by its function name turns that minor mode on or off, and you can have many minor modes turned on at any one time. Minor modes include Overwrite mode (described in the first tutorial in this series), RCS mode for managing files checked into the revision control system (RCS), and Auto Fill mode to handle automatic word wrapping. All these minor modes and many others like them can be active at any one time. This section of the tutorial shows you what you need to know to use the Emacs modes successfully: • How to know which modes are active • How to get a description of the current modes and their capabilities • How to invoke a mode • Which modes you should know about See which modes are active As described in the first tutorial in this series, the highlighted bar near the bottom of the Emacs window, called the mode line, tells you all about the current buffer -- including which modes are currently active. The current modes are indicated in parentheses toward the right side of the mode line. The abbreviated name of the major mode is listed first, followed by the abbreviated names of any minor modes. When you start Emacs with no files, you're in the scratch buffer. By default, this buffer is opened with Lisp Interaction mode, which is a special mode for the evaluation of Lisp code. See for yourself by starting Emacs in the usual way, and look at what's written in the mode line. Any time you change modes, you'll see it reflected in the mode line. Try it now: Press the Ins key to turn on Overwrite mode, and notice how the mode line changes. (The Ins key is bound to the overwrite-mode function). Press Ins again to turn off Overwrite mode. When a minor mode is enabled, it's normally indicated inside the parentheses, right after the major mode. However, not all minor modes have this indicator -- some minor modes are self-evident, such as Tool Bar mode, which displays the graphical tool bar at the top of the Emacs frame. Other minor modes in newer versions of Emacs are so minor (and always on) that it would only clutter the display to have them all shown; for example, the purpose of the Unify 8859 On Encoding minor mode is to provide an encoding unification for the various ISO 8859 character sets, which is useful for internationalization. Learn the essential modes and editing features of Emacs Page 4 of 31 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® Additionally, some modes offer extra indicators that appear in the mode line. Line Number mode, for instance, is indicated by an L followed by the line number in the buffer where the cursor is currently located. Get a description of the current mode Use the describe-mode function, which is bound to C-h m, to get a description of the current mode.