-->
Previous Table of Contents Next


Positioning the Cursor

When you edit text, you need to position the cursor where you want to insert additional text, delete text, correct mistakes, change words, or append text to the end of existing text. The commands you enter are called cursor-positioning commands.

The Arrow Keys

You can use the arrow keys on many—but not all—systems to position the cursor. It’s easy to see whether the arrow keys work: Start emacs with an existing file and see what effects the arrow keys have. You may also be able to use the <Page Up> and <Page Down> keys.

Enter the following command to create a new file called emacs-pract.3 that contains a list of the files and directories in the directory /usr. You can use this file to experiment with cursor-positioning commands.


ls /usr > emacs-pract.3

If you create the file with the <Ctrl-u><Esc><!> command sequence, you see the following message:


(Shell command completed with no output)


NOTE:  Don’t worry about the message; it doesn’t mean there’s a problem. Standard output was redirected to the file and, thus, emacs had no output to capture to a buffer.

When the file is created, start emacs with the emacs-pract.3 file (type emacs emacs-pract.3 and press <Return>). Now try using the arrow keys and the <Page Up> and <Page Down> keys (if they’re on your keyboard) to move around the editing buffer. If the keys work, you may want to use those keys for cursor positioning.

It may be that, although it appears that the cursor-positioning keys work, they’re introducing strange characters into the file. These characters are the codes the computer uses to represent the various keys instead of the characters themselves. If you see such characters, you have to use the various keyboard commands to position the cursor instead of using the keyboard keys.


TIP:  To clear the screen of spurious or unusual characters in emacs, press <Ctrl-l>.

Other Cursor-Movement Keys

You can position the cursor in emacs in other ways without using the arrow keys. You should become familiar with these methods in case you can’t or don’t want to use the arrow keys. This section also shows you some ways to position the cursor more efficiently than using the arrow keys.

When emacs was developed in 1975, many terminals didn’t have arrow keys; other keys were and still are used to position the cursor. It takes a little practice to get comfortable with these keys, but some experienced emacs users prefer these keys over the arrow keys. Here are some other keys that move the cursor:

  Press <Ctrl-f> to move the cursor to the right (“forward”) one position.
  Press <Ctrl-b> to move the cursor to the left (“back”) one position.
  Press <Ctrl-n> to move to the beginning of the next line, preserving your position in the line.
  Press <Ctrl-p> to move to the previous line, preserving your position in the line.
  Press <Ctrl-a> to move to the beginning of a line.
  Press <Ctrl-e> to move to the end of a line.

Some emacs commands allow you to position the cursor relative to words on a line. A word is defined as a sequence of characters separated from other characters by spaces or usual punctuation symbols, such as periods, question marks, commas, and hyphens. These commands are as follows:

  Press <Esc><f> to move forward one word.
  Press <Esc><b> to move backward one word.

The following example demonstrates some of these actions. Start emacs and open the emacs-pract.1 file by typing emacs emacs-pract.1 and pressing <Return>. Now use any of the cursor-positioning commands just described to move the cursor, indicated by an underline character, to the t in the word “data” on the third line of the file. The third line looks like this:


b. Sort sales data and print the results.

To move to the beginning of the next word, press <Esc><f>; the cursor is positioned under the a of the word “and” in the previous sentence. Press <Esc><f> to move to the p in “print.” To move to the beginning of the word “and,” press <Esc><b>; the cursor is positioned under the a in “and” again.


Previous Table of Contents Next