Smooth scrolling

A quick and sweet tip for one of those things surprisingly difficult to get right in emacs: buffer scrolling. If you find awkward the way emacs makes your buffers jump when you move the cursor near their begin or end, try putting this file in your load path and (require 'smooth-scrolling). You can control when scrolling starts by setting the variable smooth-scroll-margin: when the cursor is that far from the top or bottom borders, scrolling begins (default is 10 lines). Works like a charm here.

By the way, as Adam (the author of the code above) points out in the comments below, the problem is trickier than it seems. If you’re curious, this EmacsWiki page gives the details.

20 Responses to “Smooth scrolling”

  1. Tim Says:

    What about:

    (set-variable ‘scroll-step 1)

  2. Hitesh Says:

    The info page on scroll-step recommends that you set the value of scroll-conservatively to a large value instead of altering this to get smooth scrolling.

    (set-variable ‘scroll-conservatively 5)

    Seems to work well for me.

  3. Adam Spiers Says:

    Nope, neither of the above replies achieve the result I was aiming for when I wrote that code; in particular they don’t give you a scroll margin. It’s actually a more complicated business than you might think. For full details, see

    http://www.emacswiki.org/cgi-bin/wiki/SmoothScrolling

  4. Jez Says:

    Works like a champ! Thanks.

  5. pp Says:

    I just use

    (global-set-key [S-up] (lambda () (interactive) (scroll-down 2)))
    (global-set-key [S-down] (lambda () (interactive) (scroll-up 2)))

  6. Adam Spiers Says:

    A manual approach like that could complement my code nicely (for modes which don’t already bind S-up/down – I think I’d personally use a different binding). But it’s nice to have the computer do the right thing automatically most of the time đŸ™‚

    By the way, I am aware of one or two corner cases where scrolling isn’t as smooth as it could be – will get a fix out when I can …

  7. Adam Spiers Says:

    OK new version uploaded. This is getting pretty close now.

  8. reedmee Says:

    Beautiful Mind… It’s nice

    Good work.

  9. Angus77 Says:

    Umm, John Mark Schofield’s link doesn’t seem to be working either, and Google doesn’t seem to be coming to my rescue…

  10. Armchair Guy Says:

    This works great, thanks for the link. I got the script from the emacsWiki page. Exactly what I’ve been looking for.

    The one margin problem I still have with emacs is lack of sideways smooth scrolling *while selecting with the mouse*. This makes it very hard to highlight what I want if part of it goes off the edge of the window. I don’t like wrapping sentences. Any ideas?

  11. Interesting Emacs Links - 2009 Week 17 « A Curious Programmer Says:

    […] down C-n then the buffer frequently jumps as it re-centers. I find this very difficult to follow. This post explains how to fix it. As mentioned in the comments it is more difficult than it […]

  12. Reaper Says:

    Sorry, but it seems that isn’t working properly. Cursor still jumps sometimes.

  13. Hinrik Ă–rn SigurĂ°sson Says:

    Hi. Can this script be used to make the cursor always stay in the middle (except at beginning/end of file of course) ? If smooth-scroll-margin is set to half the window size, that is exactly what happens. I figured I could just set it to an arbitrarily high value so it would work with any window size, but apparently if smooth-scroll is larger than half the window size, emacs will only scroll at the edges, and by more than one line at a time, as if smooth-scrolling hadn’t been loaded at all.

    A workaround would be to make emacs change smooth-scroll-margin to half the window size every time a window is opened/resized, but I don’t know how to do that.

    To demonstrate the behavior I am looking for, try scrolling in vim after doing “:set scrolloff=1000”.

  14. Adam Spiers Says:

    For the benefit of google users, I thought it would be worth mentioning I’ve just extended my elisp hack to support forward/backwards isearches. It doesn’t make them particularly smooth, but it at least ensures that the cursor isn’t too near the top or bottom of the window.

    Here’s the link again:

    http://adamspiers.org/computing/elisp/smooth-scrolling.el

  15. Petr Glotov Says:

    I found this to be my best scroll tool:

    https://github.com/pglotov/EmacsScripts/blob/master/SmoothScroll.el

    It defines two functions and assigns them to C-. and C-, (although I thought more like “arrows” in the upper register). Very easy and smooth scrolling experience.

    Happy scrolling ! đŸ™‚

  16. era Says:

    The links to EmacsWiki seem to be broken (I guess they changed the link structure at some point). The SmoothScrolling page is now at http://www.emacswiki.org/emacs/SmoothScrolling but it’s a bit of a mess right now.


Leave a reply to Hitesh Cancel reply