15.1 Scrolling

If a window is too small to display all the text in its buffer, it displays only a portion of it. Scrolling commands change which portion of the buffer is displayed.

Scrolling forward or up advances the portion of the buffer displayed in the window; equivalently, it moves the buffer text upwards relative to the window. Scrolling backward or down displays an earlier portion of the buffer, and moves the text downwards relative to the window.

In Emacs, scrolling up or down refers to the direction that the text moves in the window, not the direction that the window moves relative to the text. This terminology was adopted by Emacs before the modern meaning of “scrolling up” and “scrolling down” became widespread. Hence, the strange result that PageDown scrolls up in the Emacs sense.

The portion of a buffer displayed in a window always contains point. If you move point past the bottom or top of the window, scrolling occurs automatically to bring it back onscreen (see Automatic Scrolling). You can also scroll explicitly with these commands:

C-v
PageDown
next

Scroll forward by nearly a full window (scroll-up-command).

M-v
PageUp
prior

Scroll backward (scroll-down-command).

C-v (scroll-up-command) scrolls forward by nearly the whole window height. The effect is to take the two lines at the bottom of the window and put them at the top, followed by lines that were not previously visible. If point was in the text that scrolled off the top, it ends up on the window’s new topmost line. The PageDown (or next) key is equivalent to C-v.

M-v (scroll-down-command) scrolls backward in a similar way. The PageUp (or prior) key is equivalent to M-v.

The number of lines of overlap left by these scroll commands is controlled by the variable next-screen-context-lines, whose default value is 2. You can supply the commands with a numeric prefix argument, n, to scroll by n lines; Emacs attempts to leave point unchanged, so that the text and point move up or down together. C-v with a negative argument is like M-v and vice versa.

By default, these commands signal an error (by beeping or flashing the screen) if no more scrolling is possible, because the window has reached the beginning or end of the buffer. If you change the variable scroll-error-top-bottom to t, these commands move point to the farthest possible position. If point is already there, the commands signal an error.

Some users like scroll commands to keep point at the same screen position, so that scrolling back to the same screen conveniently returns point to its original position. You can enable this behavior via the variable scroll-preserve-screen-position. If the value is t, Emacs adjusts point to keep the cursor at the same screen position whenever a scroll command moves it off-window, rather than moving it to the topmost or bottommost line. With any other non-nil value, Emacs adjusts point this way even if the scroll command leaves point in the window. This variable affects all the scroll commands documented in this section, as well as scrolling with the mouse wheel (see Mouse Commands for Editing); in general, it affects any command that has a non-nil scroll-command property. See Property Lists in The Emacs Lisp Reference Manual. The same property also causes Emacs not to exit incremental search when one of these commands is invoked and isearch-allow-scroll is non-nil (see Not Exiting Incremental Search).

Sometimes, particularly when you hold down keys such as C-v and M-v, activating keyboard auto-repeat, Emacs fails to keep up with the rapid rate of scrolling requested; the display doesn’t update and Emacs can become unresponsive to input for quite a long time. You can counter this sluggishness by setting the variable fast-but-imprecise-scrolling to a non-nil value. This instructs the scrolling commands not to fontify (see Font Lock mode) any unfontified text they scroll over, instead to assume it has the default face. This can cause Emacs to scroll to somewhat wrong buffer positions when the faces in use are not all the same size, even with single (i.e., without auto-repeat) scrolling operations.

As an alternative to setting fast-but-imprecise-scrolling you might prefer to enable jit-lock deferred fontification (see Font Lock mode). To do this, customize jit-lock-defer-time to a small positive number such as 0.25, or even 0.1 if you type quickly. This gives you less jerky scrolling when you hold down C-v, but the window contents after any action which scrolls into a fresh portion of the buffer will be momentarily unfontified.

Finally, a third alternative to these variables is redisplay-skip-fontification-on-input. If this variable is non-nil, skip some fontifications if there’s input pending. This usually does not affect the display because redisplay is completely skipped anyway if input was pending, but it can make scrolling smoother by avoiding unnecessary fontification.

The commands M-x scroll-up and M-x scroll-down behave similarly to scroll-up-command and scroll-down-command, except they do not obey scroll-error-top-bottom. Prior to Emacs 24, these were the default commands for scrolling up and down. The commands M-x scroll-up-line and M-x scroll-down-line scroll the current window by one line at a time. If you intend to use any of these commands, you might want to give them key bindings (see Rebinding Keys in Your Init File).