2009-12-13

Scrolling It's an integral part of just about any graphical user interface,
including emacs. However, I always found that the default way scrolling works
in emacs left something to be desired. It puts the scroll bar on the left (!),
and when scrolling around, it does not scroll smoothly, but instead it seem to
do so in bursts. But, this being emacs, we can change it!

First, the position of the scroll bar. Presumably for historical reasons,
emacs puts the scroll bar on the left of the window, unlike most other
programs. We can easily change that, by putting the following in .emacs
(or ~/.emacs.d/init.el):

(set-scroll-bar-mode 'right)

Instead of right, you can also use left, or nil to hide the scroll bar
completely. You can also do this through the menu (Options / Show/Hide /
Scroll bar). Note that on X, when the cursor (point) reaches the end of the
document, the slider on the scroll bar may not be at the bottom; I understand
this is because of some disagreement between Emacs and the toolkit (GTK+ in
this case).

Now, what about the other issue, the non-smoothness when scrolling with the
cursor-keys or with C-n, C-p? Below are my settings for making scrolling a
bit smoother, and the explanation. Of course, these are just my personal
preferences.

The scroll-margin. This determines when scrolling should start; by
setting it to 0, emacs will start to scroll whenever you are entering the
top or bottom line of the window. You can also this to, say, 5 to let
scrolling start whenever you're getting closer than 5 lines from top or bottom

Then, scroll-conservatively determines how far the cursor is allowed to
be distanced from the center of the screen when scrolling start. The
default sets this to 0, which means that whenever you start scrolling, the
cursor jumps to the center of the screen. I find that quite annoying, so I
set it to some big number (the 'effective maximum' for that is
lines-in-window / 2, but you can put any bigger number there to avoid the
jumpiness)

scroll-preserve-screen-position tries to maintain the current screen
position when you scroll using Page-Up/Page-Down. I like that.

There are also the variables scroll-up-aggressively and
scroll-down-aggressively. Normally, they determine how far emacs will scroll
(up and down, respectively) when it does so. However, they don't make any
difference with a big scroll-conservatively like I am using. Still, if you
want to play with it, their values are fractions between 0.0 and 1.0
(inclusive); a value of 1 means that it will move a full screen when scrolling
starts, a value of 0.0 causes a move of only one single line.

16 comments:

Anonymous
said...

Having the scrollbar on the left makes more sense to me when working with text in a left-to-right language. It's easier to keep the scroll thumb in view while scanning down the left side of the text. I assume that's the reason for the default behavior

@Anonyous, @Larry Stewart: it's good emacs lets you customize the scroll bar then. I find it somewhat visually distracting to have the scroll bar on the left; and being used to have it on the right in other programs plays a role as well. There is no 'wrong' or 'right' here, just personal preference.

There is one issue with Emacs' scrolling that I have never been able to understand. When using the scrollbar, just to have a quick look of a file, it doesn't scroll smoothly at all. This is because the height of the scrollbar knob is relative to characters shown instead to lines.

In normal editors, the scroll knob size depends on how many lines are displayed, which is a number that remains constant for a fixed window (in emacs-parlance) height. Instead, Emacs sets the knob height according to the displayed characters relative to the file size. This means that the knob will appear very small if the visible lines are sparsely populated, while it will be very tall if the shown lines are dense.

The scroll bar is supposed to give feedback about the visible portion of the file and the rest of it.

I do not know of any workaround and I'm not sure if it has a solution. Has anyone else seen or experienced this issue?

Hi, does anyone know how to use the mouse to scroll in emacs in the terminal (-nw) ? I'd find it useful sometimes, for example to show my code to other people. The problem is that the mouse-wheel buttons move the point instead of the buffer.Thx !