Controlling emacs

To issue commands to emacs, you use key combinations. In describing these, it is the convention to use C for the Ctrl key and M for the Meta key, which can be either Alt or Esc. For example, to save a file, you do Ctrl+x Ctrl+s; this is normally written as C-x C-s. If you are running the graphical form of emacs, you can do some of the most common actions (such as saving a file) by clicking menu items (File O Save).

Note that the commands here are the default ones. The emacs editor is totally configurable, which means that you can bind a particular keystroke to any command you want. For example, C-x C-f is bound to the command find-file, which you can also run with M-x find-file. You can break that binding and bind the command to a different keystroke. You can also bind a keystroke to a command that you find yourself using regularly that has no binding (or one that you find inconvenient). To make such a change permanent, you need to add a line to your .gnu-emacs-custom file.

The most important basic emacs commands are as follows:

■ C-x C-w: Write the current buffer to a file ("Save as'').

■ C-y: Yank (that is, copy) the last killed text.

Moving Around

If you are using emacs in a graphical session, the mouse works both for selecting text and for moving around the file. But you can also navigate with the keyboard using the following keystrokes:

■ M-xgoto-line: Move to a line number that you specify.

The commands for moving to the beginning and end of a sentence assume that sentences are separated by a dot and two spaces.


C-_ or C-x u will undo your last command or typing; emacs remembers everything you do, so you can do a sequence of undo commands.

Replacing Text

M-x replace-string will globally replace one string with another in the whole buffer or in the selection. You can also do a conditional replacement of text with M-% or M-x query-replace. You are prompted as to whether you want to make each change.


C-s starts an incremental search. What this means is that if you type C-s Li, for example, you see the next instance of Li highlighted in the text. If you type another letter (for example n), you will now be searching for Lin. If you press C-s again, you will move to the next instance of this new search string.

You can also do a non-incremental search by typing C-s followed by pressing Return. Whatever you now enter will be the search string and emacs will jump to the next occurrence of it. Regular expression searches are also possible. The command M-C-s starts a regular expression search. If you then type a regular expression, emacs searches for the next matching text in the buffer. (See also Chapter 10 for more on regular expressions.)

Making Corrections

M-c capitalizes the next word, and M-u makes the next word all caps. M-l makes the text lowercase. M-t switches the order of two words. M-x ispell-buffer checks the spelling of the entire buffer. You can check the spelling of a single word with M-x ispell-word.

Was this article helpful?

0 0

Post a comment