Introduction

Introduction

The Emacs text editor is undoubtedly the most feature-rich text editor available for the Linux platform. As such, it is impossible to comprehensively cover all the features of Emacs in a single chapter. Therefore, my intent here is to describe the basic features and provide suggestions as to where you might find more information.

Emacs comes in both character-based (see Figure 22-1) and GUI-based implementations (see Figure 22-2). Both implementations make use of pull-down menus and a dizzying array of shortcut key combinations to control functionality.

Figure 22-1. emacs

graphics/22fig01.gif

Figure 22-2. xemacs

graphics/22fig02.gif

Perhaps the biggest distinction between Emacs and vi is that, unlike Emacs, vi doesn't make use of separate modes for inserting text and performing edit functions.

Instead, it makes extensive use of control keys (almost always the <Ctrl> and <Esc> keys are used, although it can be bound to other keys as well). These control keys are used in two ways:

1.       It is used the same way as the shift key in that you hold them down while pressing another key to specify some editing function. Key sequences of this type will be written

<Ctrl>+functionkey

2.       It is used as a heads-up signal to the editor that the next key sequence will be a control key. There are only two

main "heads-up" signals, <Ctrl>+c and <Ctrl>+x. Key sequences of this type will be noted as follows:

<Ctrl>+c functionkey

<Ctrl>+x functionkey

graphics/tip.gif

A good rule of thumb in Emacs is that if the <Ctrl>+somekey sequence does something on a small scale, <Esc>+somekey will do the same thing on a larger scale. For example, <Ctrl>+f moves forward one character, <Esc>+f moves forward one word.

 

If you see two keys mentioned together but not joined with a + sign, it means that you should type those two keys in the order they are written down:

<Press me first> <Press me second>

If you see two key sequences separated by commas, as in

<Ctrl>+c somekey, <otherkey>

it means that there are two different ways of doing the same thing.

Buffers

It is important to note that when you open a file for editing in Emacs (or any other editor) the changes you make do not immediately affect that file. Instead, all of your work is done in a temporary copy of the file called a buffer. If you choose to save your work, the contents of the buffer are written to the original file (or possibly some other file). Of course, if you decide you don't like the changes you have made, there's no law that says you have to save them. You can exit with no changes (<Ctrl>+x <Ctrl>+c) or read the unaltered original file back in and start over (<Ctrl>+x <Ctrl>+f). Also, note that buffers are used for more than just holding copies of files. For example, when you delete a section of text, it is stored in a special buffer called the kill ring from which it can be retrieved if you change your mind later.

Finally, the point is the Emacs term for the current location of the cursor. You will see this term frequently in on-line documentation.

Editing Modes

Emacs will have different features depending on the type of file you are editing. For example, in files with a '.c' extension you will be able to perform searches for C preprocessor definitions.

Getting Help

This chapter contains information about the basic editing functions of Emacs, but it is by no means comprehensive. In all likelihood your Linux distribution came with a number of Emacs-related documents, typically found under a directory called "howto" or "HOWTO". (See the entries on find and locate for details on how to find the specific location in your system.) In addition, Emacs itself incorporates a fairly extensive help mechanism.

<Ctrl>+h

Enter the on-line help system type

<Ctrl>+h t

To start the on-line tutorial

<Ctrl>+h i

Browse the info manuals

 

graphics/tip.gif

The <Ctrl>+g key sequence is the usual mechanism for interrupting a function in Emacs. It doesn't work in every single instance, but it's worth keeping in mind.

 

Emacs also provides descriptions of the individual command key sequences. To access the description for a particular key sequence, you must first type

<Ctrl>+h k

A "Describe Key:" prompt will appear at the bottom of your screen. Type in the key sequence you are curious about to get a brief description.

Starting and Stopping

Emacs can be invoked from the command line either as a character-based or X-Windows-based editor. If you have X Windows set up on your system, you'll probably want to go with the X version, just because it looks "cooler." If your system is running across a crowded network, it's more polite to use the character version of Emacs, as X applications generate a lot of network traffic.

emacs, xemacs

Start the emacs editor

emacs file, xemacs file

Start editing the specified file with the emacs editor

File-->Exit Emacs, <Ctrl>+x <Ctrl>+c

Exit emacs without saving

Cursor Movement

Small Cursor Movement

The key sequences in this section describe how to move your cursor in the smallest increments.

<Up Arrow>, <Ctrl>+P

Move up

<Down Arrow>, <Ctrl>+N

Move down

<Backspace>

Erase previous character

<Left Arrow>, <Ctrl>+b

Move left, but don't erase

<Right Arrow>, <Ctrl>+f

Move right, but don't erase

Medium Cursor Movement

The key sequences in this section describe how to move your cursor in midrange (word and paragraph) increments.

<Esc> f

Move forward one word

<Esc> b

Move backward one word

<Esc> e

Move forward one sentence

<Esc> a

Move backward one sentence

<Esc> }

Move forward one paragraph

<Esc> {

Move backward one paragraph

Large Cursor Movement

The key sequences in this section describe how to move your cursor in large (page and document) increments.

<Esc> >

Go to end of buffer

<Esc> <

Go to beginning of buffer

<Esc> v, <Page Up>

Scroll up one screen

<Ctrl> v, <Page Down>

Scroll down one screen

Opening and Saving Files

The commands and key sequences in this section describe how to open and close buffers. Note that these commands work on your current buffer only; for information on navigating between buffers, see the section "Editing Multiple Buffers."

<Ctrl>+x <Ctrl>+f

Open a new file

<Ctrl>+x <Ctrl>+s

Save the current buffer

<Ctrl>+x <Ctrl>+w

Save the current buffer to some (possibly new) file name

Deleting Text

Emacs provides mechanisms for deleting text in any increment you like, from individual characters to the entire buffer. When you delete text in Emacs using one of the commands that follow, it is stored in a special buffer called the kill ring. If you change your mind about something you deleted, you can use the <Ctrl>+y (yank) command to get it back again.

<Ctrl>+d

Delete character under the cursor

<Del>

Delete previous character

<Esc>+d

Delete current word

<Esc><Del>

Delete previous word

<Ctrl>+k

Delete (kill) to the end of current line

<Esc>+k

Delete (kill) to end of current buffer

<Ctrl>+y

Yank deleted text out of the kill ring and back on screen

By default, the kill ring stores your last 30 deletions. You can modify it to store more than that, but 30 really is a perfectly reasonable upper limit. The 1st one is retrieved using <Ctrl>+y. The 2nd through 30th deletions are retrieved by pressing <Esc>+y in succession.

Miscellaneous But Still Important

<Ctrl>+x u

Undo

<Ctrl>+l

Redraw the screen with the current line in the center

Marking Text

Before you can perform copy, move, or delete operations on random blocks of text, you must first specify the boundaries of that text. For xemacs users this is accomplished by the click-and-drag operation familiar to most computer users. In non-GUI environments the process is somewhat different. Move your cursor to the beginning of the region you wish to mark and press

<Ctrl>+<Space>

This tells Emacs that you're marking a block of text. Next, move your cursor to the end of the region you wish to mark. If you're doing it right, the text you pass over should become shaded. Once you've reached the other boundary of the region, you can proceed with the copy, cut, or move operation.

Alternatively, you can choose to mark an entire paragraph by using the following command:

<Esc>+h

To mark the current page, you use

<Ctrl>+x <Ctrl>+p

To mark or the entire buffer, you use

<Ctrl>+x h

Deleting Text

Marked text can be deleted by using the <Ctrl>+w or <Del> keys.

Moving Text

To move text, first delete the marked region using <Ctrl>+w. Move your cursor to the new location and press <Ctrl>+Y to yank it back out of the buffer.

Copying Text

To copy text, first mark the region and then choose the Copy item from the edit menu. Next, move your cursor to where you want the copy to go and press <Ctrl>+y.

Searching

Incremental Search

The simplest form of searching in Emacs is the incremental search. To enter incremental search mode, press

<Ctrl>+s

and then type in the string you want to search for. Note that Emacs begins matching characters as soon as you start to type. Don't worry about this, just keep typing until you've got the full pattern specified. Emacs will take you to the first instance of that pattern in the buffer. If the first instance isn't what you want, continue the search by pressing <Ctrl>+s again. If you go too far, you can search backward for the same pattern by pressing <Ctrl>+r. To cancel a search, press <Ctrl>+g.

Simple Searches

Simple searches are similar to incremental searches in that they match against the search pattern you typed in regardless of whether it is a substring or standalone word. You may prefer simple searches to incremental searches if all the bouncing around that occurs as you type in your incremental search gets on your nerves. Simple searches wait until you're done typing before they start looking for the pattern. To perform a simple search, type

<Ctrl>+s <Return>

and then type in your pattern. When you're finished typing in the pattern, press return again and the search begins. As with incremental searches, you can find the next instance of the pattern with <Ctrl>+s, reverse the direction of the search with <Ctrl>+r, and terminate the search with <Ctrl>+g.

Word Search

The incremental search can be somewhat frustrating in that it matches quite promiscuously. For example, an incremental search on the string "hat" would match against "that," "what," and all sorts of other words that you probably weren't interested in. In situations where your incremental search string is turning up all sorts of unwanted matches, you might want to try a word search.

Word searches will match only against the exact string you typed in, delimited by white space. It will not return strings in which your search pattern is a substring. Continuing with our preceding example, only the word "hat" would be returned by a word search, not "what", "that", or "chat". To perform a word search, use

<Ctrl>+s <Return> <Ctrl>+w

Search and Replace

The simplest way to start a search and replace routine is to press

<Esc> %

Upon doing so, Emacs will prompt you with the following string:

Query replace:

Type in the string you want to be replaced (let's pretend it's asdf), and press <Enter>. Emacs will continue the prompt

Query replace: asdf with:

Type in the string you want to replace it with, and press enter. Emacs will search from the current position of the cursor to the end of the buffer, stopping on each instance of the search pattern. If you want a particular instance replaced, press the <Spacebar> or the <Y> key. If not, press <Del> or the <N> key. To interrupt the search press <Ctrl>+g.

If you want to replace every single instance of a given string with some other string without being prompted (a dangerous proposition), you can do so by pressing the following key command:

<Esc> x replace-string <Return>

Just to clarify, you must type in the actual phrase "replace-string", not the string you want replaced. Emacs will prompt you for the string you want to replace, but as soon as you press <Enter> every instance of the pattern is replaced without prompting. Use with caution.

Editing Multiple Buffers

It is possible and frequently quite handy to edit two or more buffers at the same time. They can even share the same screen. To open a new buffer (without getting rid of your current one), type,

<Ctrl>+x <Ctrl>+f

and specify the name of the file you wish to edit. If the file you specify doesn't exist, Emacs will create a new file by that name. Note that you are now looking at the new file. To return to the original buffer, type

<Ctrl>+x b

and type the filename or, in this case, simply accept the default. You may also type the first few letters of the name of the buffer and press the <Tab> key to invoke Emacs' filename completion feature.

Within each new buffer, the save and exit features work exactly the same way as in the original. This is as you might expect, given that you are simply editing a new file. To delete the current buffer, type

<Ctrl>+x k

To display a list of the currently active buffers, type

<Ctrl>+x <Ctrl>+b

To interactively save some of the active buffers, type

<Ctrl>+x s

Multiple Windows

It is also possible to display more than one buffer on screen at once. This is accomplished by means of an Emacs window, which is conceptually similar to an X window, but predates it by some years. To split your current window horizontally into two windows, type

<Ctrl>+x 2

To split your current window vertically into two windows, type

<Ctrl>+x 3

You can can have as many split-open windows as you like, but for obvious reasons having more than a couple of windows open is impractical for all, but the largest monitors. If you find that you have been overenthusiastic and wish to return to a one-window state, type

<Ctrl>+x 1

To delete only your current window, type

<Ctrl>+x 0

Note that deleting a window does not destroy the contents of the buffer associated with that window. They can still be accessed by using the <Ctrl>+x b commands just described.

To move between the windows, you can either click on the one you're interested in with your mouse or type

<Ctrl>+x o

as many times as necessary to get you where you want to go. Multiple windows tend towards itty-bittiness quite rapidly. To scroll within an individual window, use the following key sequences:

<Ctrl>+x <

Scroll left

<Ctrl>+x >

Scroll right

You can adjust the size of the current window as follows:

<Ctrl>+x ^

Make the current window taller

<Ctrl>+x shrink-window

Make the current window shorter

<Ctrl>+x }

Make the current window wider

<Ctrl>+x {

Make the current window skinnier

UNIX Shells within Emacs

Another amazingly handy feature of Emacs is the ability to run a UNIX shell within one of your buffers. This feature alone is enough to rattle even the most deeply entrenched vi partisan. To invoke a UNIX shell within Emacs, type

<Esc>+x shell

You can also bypass the shell and run individual UNIX commands directly by typing

<Esc> !

which will prompt you for a shell command and display the results in your current window.

C Mode

As mentioned earlier, Emacs automatically goes into different modes depending on the extension of the file that you are editing. There are way too many modes to go into detail on all of them, but I wanted to touch briefly on some of the features of C mode.

<Esc> <Ctrl>+a

Move to the top of the current function

<Esc> <Ctrl>+e

Move to the bottom of the current function

<Esc> <Ctrl>+q

Indent the current function

<Esc> a

Move to the beginning of the current statement

<Esc> e

Move to the end of the current statement

Finally, and perhaps most usefully, you can use the <Ctrl>+c <Ctrl>+u key sequence to jump to the c preprocessor conditional, which affects the code under your cursor. This is particularly handy in a Linux environment where even the blandest statements must be coated in a thick gooey mess of #ifdefs to ensure portability.

 



Linux Desk Reference
Linux Desk Reference (2nd Edition)
ISBN: 0130619892
EAN: 2147483647
Year: 2000
Pages: 174
Authors: Scott Hawkins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net