Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 4: Basic Editing |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

## Moving Around in the Buffer

In Emacs there are many ways of moving around. This might make a new user a bit insecure (to be honest, it scared me!), especially if you read the Emacs reference manual, available from the info pages, or the online tutorial; these sources tell you the whole truth in one go and tell you about keybindings, which can be used from any kind of keyboard.

Don't panic! Moving around in Emacs today with a modern keyboard is as simple as it is in any other program. Simply use the arrow keys to move around. Furthermore, you can jump one page at a time with the page-up and page-down keys.

Note - If you get in a situation where the cursor keys don't work, alternative keybindings are available for the movement functions. These are Forward one character-- C-f (forward-char). Backward one character-- C-b (backward-char). Go to the previous line-- C-p (previous-line). Go to the next line-- C-n (next-line). Scroll forward one page-- C-v (scroll-up). This is equal to the key Page Down. Scroll backward one page-- M-v (scroll-down).

### Add a Blank Line at the End of the Buffer

The most basic example of Emacs configuration is the following. What should be done when you ask Emacs to take you to the next line and you already are located at the last line of the buffer? There are two possible and equally good answers to this question:

- Insert a blank line, and go to this line.

- Ring the bell to indicate that this is not possible.

Which solution is the better depends solely on your preferences, taste, state of mind, and so on. Therefore this can be configured in Emacs.

To select the setup where a newline is inserted, add the following into your .emacs file:

    (setq next-line-add-newlines t)

To select the setup where the bell rings, insert the following into your .emacs file:

    (setq next-line-add-newlines nil)

I'm telling you this now because GNU Emacs and XEmacs have different defaults for this option.

### Moving by Word, Line, Sentence, or Paragraph

Emacs has many functions for moving around. You do not need to learn every one of them right away, because the important point is that you know they exist. You can take one at a time, as you find that you need it.

Two functions exist for moving to the beginning or the end of the line. These are bound to C-a (beginning-of-line) and C-e (end-of-line). These are not very mnemonic, but don't let that scare you. When you have used Emacs a few days, these bindings will be located in your fingertips.

You can also move forward and backward by words. This is faster than moving by characters and, fortunately, it is very easy to remember its keybindings-- C-right or Ctrl+right arrow (forward-word) and C-left or Ctrl+left arrow (backward-word). A word is defined as a number of letters or numbers.

Functions also exist that move by sentences, paragraphs, and pages. The definitions of these three items are tricky, but fortunately they are much like what you might expect. There is one thing to note: For a sentence to end in the middle of the line, two spaces need to follow it. Pages are separated by Ctrl+L (to insert a page break, press C-q C-l. The functions are M-e (forward-sentence), M-a (backward-sentence), M-} (forward-paragraph), M-{ (backward-paragraph), C-x ] (forward-page), and C-x [ (backward-page).

Tip - To learn more about these functions and what exactly describes an end of a sentence, paragraph, or page, press C-h f (describe-function) and type the name of the function. You might however want to wait until you have read Hour 9, "Regular Expressions," because the delimiters are described by regular expressions.

In Figure 4.4, you can see where each of the functions listed previously will take you.

Note - Text within several figures throughout this book is excerpted from The Hitchhiker's Guide to the Galaxy by Douglas Adams.

Figure 4.4
Movement functions.

Tip - There are also two functions for going to the matching starting or ending brace called forward-sexp and backward-sexp. These functions are most useful when programming. It might be a good idea to bind these two functions to Meta-left arrow and Meta-right arrow, which is done by inserting the following Lisp code into your .emacs file: (global-set-key [(meta left)] 'backward-sexp) (global-set-key [(meta right)] 'forward-sexp) Note that this is done by default in XEmacs.

### Enhanced Page Up and Page Down Scrolling

When you scroll one page backward and then one page forward again, point does not always end up in the same position. This seems to be a bug in Emacs. Fortunately, a library exists, called pager, that offers an alternative implementation of this feature. It is located on this book's CD and an installation description is given in Appendix A, "Installing Functions and Packages from the CD."

This library contains another very useful feature, namely the capability to scroll one line at a time without changing the position of point in the window. That is, if point is located in the middle of the window, it will be located in the middle of the window after you have scrolled. It will, of course, be located on a different line! This can be seen in Figures 4.5 and 4.6.

Figure 4.5
Initial state before scrolling.

Figure 4.6
Here five lines have been scrolled since Figure 4.5.

Figure 4.5 is the initial state, and in Figure 4.6, the window has been scrolled five lines (with the command pager-row-down). Note how point was located in the middle of the window before and after the scroll command.

The previous commands can be very useful if point is located in the middle of the screen and you want it to be there, but you want to see the line below the last line shown in the window.

### Moving to a Line Specified by a Number

It is sometimes useful to be able to go to a line by giving its number. This can be done using the command goto-line, which is bound to M-g in XEmacs, and unbound to keys in GNU Emacs. If you want it on M-g in GNU Emacs, insert the following line into your .emacs file:

    (global-set-key [(meta g)] 'goto-line)

### Recentering the Window

If your window has been garbled or you want to place point in the center of the window, press C-l (recenter). This redraws the window and places point in the middle of the window without changing its position in the buffer. (That is, it locates point in front of the same word and on the same line, but this line is shown in a different location in the window.)

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 4: Basic Editing |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

© Copyright Macmillan USA. All rights reserved.