Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [363]

By Root 1489 0
one page at a time, as in vi. In your .emacs file you might include the following lines:

(global-set-key "\C-f" 'scroll-up)

(global-set-key "\C-b" 'scroll-down)

Again, we have to issue a caveat: be careful not to redefine keys that have other important uses. (One way to find out is to use C-h k to tell you what a key does in the current mode. You should also consider that the key may have definitions in other modes.) In particular, you'll lose access to a lot of functions if you rebind the prefix keys that start commands, such as C-x and C-c.

You can create your own prefix keys, if you really want to extend your current mode with lots of new commands. Use something like:

(global-unset-key "\C-d")

(global-set-key "\C-d\C-f" 'my-function)

First, we must unbind the C-d key (which simply deletes the character under the cursor) in order to use it as a prefix for other keys. Now, pressing C-d C-f will execute my-function.

You may also prefer to use another mode besides Fundamental or Text for editing "vanilla" files. Indented Text mode, for example, automatically indents lines of text relative to the previous line so that it starts in the same column (as with the :set ai function in vi). To turn on this mode by default, use:

; Default mode for editing text

(setq default-major-mode 'indented-text-mode)

You should also rebind the Enter key to indent the next line of text:

(define-key indented-text-mode-map "\C-m" 'newline-and-indent)

Emacs also provides minor modes, which are modes you use along with major modes. For example, Overwrite mode is a minor mode that causes newly typed characters to overwrite the text in the buffer, instead of inserting it. To bind the key C-r to toggle Overwrite mode, use the following command:

; Toggle overwrite mode.

(global-set-key "\C-r" 'overwrite-mode)

Another minor mode is Autofill, which automatically wraps lines as you type them. That is, instead of pressing the Enter key at the end of each line of text, you may continue typing and Emacs automatically breaks the line for you. To enable Autofill mode, use the commands:

(setq text-mode-hook 'turn-on-auto-fill)

(setq fill-column 72)

This turns on Autofill mode whenever you enter Text mode (through the text-mode-hook function). It also sets the point at which to break lines at 72 characters.

Regular Expressions

Even a few regular expression tricks can vastly increase your power to search for text and alter it in bulk. Regular expressions were associated only with Unix tools and languages for a long time; now they are popping up in other environments, such as Microsoft's .NET. Only Unix, however, offers them in a wide variety of places, such as text editors and the grep command, where ordinary users can exploit them.

Let's suppose you're looking through a file that contains mail messages. You're on a bunch of mailing lists with names such as gyro-news and gyro-talk, so you're looking for Subject lines with gyro- in them. You can use your text editor or the grep command to search for:

^Subject:.*gyro-

This means "look for lines beginning with Subject:, followed by any number of any kind of character, followed by gyro-." The regular expression is made up of a number of parts, some reproducing the plain text you're looking for and others expressing general concepts such as "beginning of line." Figure 19-25 shows what the parts mean and how they fit together.

Just to give a hint of how powerful and sophisticated regular expressions can be, let's refine the one in Figure 19-25 for a narrower search. This time, we know that mailing

Figure 19-25. Simple regular expression

lists on gyros send out mail with Subject lines that begin with the name of the list in brackets, such as Subject: [gyro-news] or Subject: [gyro-talk]. We can search for precisely such lines, as follows:

^Subject: *\[gyro-[a-z]*\]

Figure 19-26 shows what the parts of this expression mean. We'll just mention a couple of interesting points here.

Figure 19-26. Regular expression with more parts

Brackets, like carets and asterisks,

Return Main Page Previous Page Next Page

®Online Book Reader