I used to set up the Emacs auto scrolling using the following lines, as someone online claimed this would result in “smooth scrolling”:
(setq scroll-margin 3
However, when the cursor moves near the top or the bottom of the frame (or the window), Emacs automatically recenters and place the cursor in the middle of the screen. This has been bugging on me for too long. My personal preference of the cursor is: when I move it up or down near the screen top or bottom, I would like to have Emacs JUST MOVE 1 LINE for me, so I can follow naturally the movement of the cursor. If I need to recenter the screen and place the cursor in the middle, I would do it myself by calling C-l. I don’t need Emacs to move the cursor for me!
Today, after some searching, I finally figured out that the annoying jumping around of the Emacs cursor is due to the Automatic Scrolling of Emacs.
The idea of Automatic Scrolling, according to http://ecs.victoria.ac.nz/cgi-bin/info2www?(emacs)Auto+Scrolling, is:
“Redisplay scrolls the buffer automatically when point moves out of the visible portion of the text. The purpose of automatic scrolling is to make point visible, but YOU CAN CUSTOMIZE MANY ASPECTS OF HOW THIS IS DONE.”
There are four variables to be customize for the Automatic Scrolling feature of Emacs:
- scroll-margin: restricts how close point can come to the top or bottom of a window
- scroll-conservatively: if you set it to number N, then if you move point just a little off the screen–less than N lines–then Emacs scrolls the text just far enough to bring point back on screen
- scroll-up-aggressively: a number (F, meaning fraction) between 0 and 1, and it specifies where on the screen to put point when scrolling upward. More precisely, when a window scrolls up because point is above the window start, the new start position is chosen to put point F part of the window height from the top
- scroll-down-aggressively: a number (F, meaning fraction) between 0 and 1, and it specifies where on the screen to put point when scrolling downward. More precisely, when a window scrolls down because point is below the window bottom, the new start position is chosen to put point F part of the window height from the bottom
Based on these explanations, I configured my Automatic Scrolling according to my personal preference:
;; for smooth scrolling and disabling the automatical recentering of emacs when moving the cursor
(setq scroll-margin 1