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.
September 11, 2007 at 4:38 am
What about:
(set-variable ‘scroll-step 1)
September 11, 2007 at 5:13 am
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.
September 11, 2007 at 9:10 am
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
September 11, 2007 at 9:43 am
Works like a champ! Thanks.
September 11, 2007 at 11:37 am
I just use
(global-set-key [S-up] (lambda () (interactive) (scroll-down 2)))
(global-set-key [S-down] (lambda () (interactive) (scroll-up 2)))
September 11, 2007 at 11:40 am
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 …
September 11, 2007 at 11:37 pm
OK new version uploaded. This is getting pretty close now.
September 12, 2007 at 3:38 am
Beautiful Mind… It’s nice
Good work.
July 12, 2008 at 8:02 pm
Love Adam’s smooth-scrolling script. Once I got it downloaded and installed, it worked perfectly. However, it does not appear to be available from his website. Google to the rescue!
http://www.google.com/url?sa=t&ct=clnk&cd=1&url=http%3A%2F%2F209.85.141.104%2Fsearch%3Fq%3Dcache%3AFlpvO2VoptoJ%3Awww.adamspiers.org%2Felisp%2Fsmooth-scrolling.el%2Bsite%3Aadamspiers.org%2Bemacs%2Bscrolling%26hl%3Den%26ct%3Dclnk%26cd%3D1%26gl%3Dus%26client%3Dfirefox-a&ei=lA15SOmmGJyksQOVpvCNDw&usg=AFQjCNE25cWFiHQemrccbR7a21OmxH5ITA&sig2=ixs_hzrQL6htl8aYoZGseA
August 4, 2008 at 2:49 pm
Umm, John Mark Schofield’s link doesn’t seem to be working either, and Google doesn’t seem to be coming to my rescue…
April 8, 2009 at 12:36 am
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?
April 26, 2009 at 11:34 am
[…] 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 […]
July 10, 2009 at 1:58 pm
Sorry, but it seems that isn’t working properly. Cursor still jumps sometimes.
November 25, 2009 at 1:24 pm
You’re right. Sometimes…
But, for example together with ecb, it does not work at all.
July 30, 2009 at 2:38 pm
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”.
July 30, 2009 at 2:58 pm
I found this snippet on http://www.emacswiki.org/cgi-bin/wiki?SmoothScrolling which does exactly what I was describing, no smooth-scrolling necessary:
(add-hook ‘post-command-hook
(lambda ()
(recenter ‘(“don’t redraw”))))
July 30, 2009 at 3:07 pm
Actually, centered-cursor-mode does the same thing, only better: http://www.emacswiki.org/emacs/centered-cursor-mode.el
November 25, 2009 at 3:55 pm
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
January 2, 2012 at 10:07 pm
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 ! đŸ™‚
March 10, 2015 at 6:10 am
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.