2.5. Editing Tricks and Shortcuts


2.5. Editing Tricks and Shortcuts

Now that you've learned the basics of editing—moving the cursor to the right position, deleting, copying, and moving text—you can learn some tricks that make editing easier.

2.5.1 Fixing Transpositions

The most common typo involves the transposition of two letters, and most typos are noticed immediately after you make them. Pressing C-t transposes two letters, to put them in the right order:

Before C-t

After C-t

the best of timse, it

the best of times it


To transpose two letters, put the cursor on the second of the two letters to be transposed. Press C-t. (If you often transpose letters, word abbreviation mode, discussed in Chapter 3, cleans up typos automatically.)

You can also transpose two words, lines, paragraphs, or sentences. To transpose two words, put the cursor between the two words and press M-t. After Emacs has finished, the cursor follows the second of the two (transposed) words:

Before M-t

After M-t

one threetwo

one two three


Interestingly, Emacs moves words, but not punctuation. Let's say that two names are reversed:

Before M-t

After M-t

Charles, Dickens

Dickens, Charles


To transpose two lines, put the cursor anywhere on the second of the two and press C-x C-t. Emacs moves the second before the first:

Before C-x C-t

After C-x C-t

second line

first line

first line

second line

third line

third line


Table 2-9 summarizes the transposition commands.

Table 2-9. Transposition commands

Keystrokes

Command name

Action

C-t

transpose-chars

Transpose two letters.

M-t

transpose-words

Transpose two words.

C-x C-t

transpose-lines

Transpose two lines.

(none)

transpose-sentences

Transpose two sentences.

(none)

transpose-paragraphs

Transpose two paragraphs.


2.5.2 Changing Capitalization

Mistakes in capitalization are also common and annoying typing errors. Emacs has some special commands for fixing capitalization. To capitalize the first letter of any word, put the cursor on the first letter and press M-c. To put a word in lowercase, press M-l. To put a word in uppercase, press M-u. The key bindings here are mnemonic: Meta followed by c for capitalize, l for lowercase, and u for uppercase. Note that if the cursor is in the middle of a word, Emacs takes action only from the character under the cursor to the end of the word. You can easily use M-l to lowercase the second half of a word, and so on.

If you notice that the word you just typed is incorrect, you can use the same commands prefaced by Meta- (press and hold Meta followed by a hyphen). This corrects the previous word without moving the cursor. If the cursor is positioned in the middle of a word, using Meta- before a command causes it to work on the first part of the word (the part preceding the cursor), rather than the part following the cursor.

For example, starting with abcd efghij:

If you press:

You'll get:

Meta - u

abcdEFGHIJ

Meta - M-u

ABCDefghij

M-c

abcdEfghij

Meta - M-c

Abcdefghij


Table 2-10 summarizes the capitalization commands.

Table 2-10. Capitalization commands

Keystrokes

Command name

Action

M-c

capitalize-word

Capitalize first letter of word.

M-u

upcase-word

Uppercase word.

M-l

downcase-word

Lowercase word.

Meta - M-c

negative-argument; capitalize-word

Capitalize previous word.

Meta - M-u

negative-argument; upcase-word

Uppercase previous word.

Meta - M-l

negative-argument; downcase-word

Lowercase previous word.


2.5.3 Overwrite Mode

You may be used to typing over old text rather than having to delete it. There is a certain satisfaction in destroying some really bad text in this way. You can do this in Emacs, too, by entering a minor mode called overwrite mode. When you're in overwrite mode, any new text you type wipes out the text that's underneath. When you're not in overwrite mode (i.e., in normal Emacs), any new text you type is inserted at the cursor position and any subsequent text is pushed to the right. (Other software may refer to this as insert mode; because it is the way GNU Emacs normally behaves, it doesn't have a name here.)

To enter overwrite mode, press the Insert key.[6] Ovwrt should appear on the mode line. If this doesn't work (or if you don't have an Insert key), type M-x overwrite-mode Enter. You can turn off overwrite mode by typing M-x overwrite-mode Enter again. Using Emacs's command completion, simply type M-x ov and press Enter. This is enough of a unique string to tell Emacs you want to toggle overwrite mode. Completion, one of the best shortcuts in Emacs, is discussed further in Chapter 14.

[6] On a Mac keyboard, we found that the Help key, to the left of Home, toggles overwrite mode.