To put it simply, I'm trying to get scrolling in emacs like in vim and most other editors; when I'm for example, two lines from the bottom/top, and I press down/up (Ctrl-p,n, ↑,↓) it goes only one line up or down, not half the screen.
-
7I'm convinced this will never be fixed. It was a deficiency back in the last 90's and it's still broken here in 2010. – Steve Kuo Jul 15 '10 at 18:55
-
12@Steve Kuo: Quit bashing - this is working *as designed*, but can be changed (or "fixed") with a simple setting. – Teddy May 18 '11 at 08:23
-
19@Teddy and what is this "simple setting"? – Steve Kuo Jun 04 '12 at 21:53
-
1@Teddy yeah, what is it? – Scooter Feb 05 '19 at 09:14
-
@Rook: Actually I would recommend to use `z.` in vim more frequently to get the Emacs feeling ;-) It may be unusual, but it's actually more useful than scrolling line by line IMHO. – U. Windl Feb 06 '19 at 22:14
-
1Actually I've got a use case right now. I want to edit two slightly different files and then being able to visually align them, for convenience. So while it can be useful to work with 'the Emacs feeling ;-)', it's not very useful that we can *not scroll line by line. – HarryH Apr 28 '20 at 07:14
-
It's also useful to be able to **scroll without moving point**: http://stackoverflow.com/a/10541426/326516 – Micah Elliott Oct 07 '20 at 16:48
16 Answers
See some of the suggestions on the Emacs Wiki:
(setq scroll-step 1
scroll-conservatively 10000)

- 6,148
- 6
- 38
- 76

- 120,335
- 23
- 147
- 134
-
3Saw it, actually - both answers, yours and jrockway's but on Wiki both of there are not recommended. Unfortunatelly, the reasons for that are not clearly explained. That's why I posted the question, hoping someone's found a method which works without flaws. – Rook Jul 15 '09 at 01:42
-
1I can't see why this is not recommended. I've been using similar configuration and it has been working quite well. To assure you further, see this doc of scroll-step(notice the last sentence): *The number of lines to try scrolling a window by when point moves out. If that fails to bring point back on frame, point is centered instead. If this is zero, point is always centered after it moves off frame. If you want scrolling to always be a line at a time, you should set `scroll-conservatively' to a large value rather than set this to 1. – an0 Jul 15 '09 at 02:13
-
Yeah, looking around a bit more I don't see much more to back up that cautionary note on the wiki. For example, scroll-step seems to be quite common in multiple .emacs files through a google search. Maybe the wiki author is referring to an old version of emacs? – ars Jul 15 '09 at 06:04
-
Never had any issues with `scroll-conservatively`. The warnings against this are a bit odd - if it causes issue, it's easy enough to remove the line from your init.el! – dbr Jan 23 '11 at 06:04
-
1@ars http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/0a7a9c730c037d57 so with emacs < 23.1 and large files, it might sometimes jump the cursor to the middle, which doesn't seem like the biggest problem – dbr Jan 23 '11 at 06:09
-
@dbr: Definitely a mild annoyance at worst. Thanks for clarifying and pointing to that thread! – ars Jan 23 '11 at 18:14
-
The current doc-string for `scroll-conservatively` in a developer build of Emacs Trunk (as of June 2014) states: *If the value is greater than 100, redisplay will never recenter point, but will always scroll just enough text to bring point into view, even if you move far away. A value of zero means always recenter point if it moves off screen.* So an integer of **101** will currently suffice; however, the users are certainly free to use a greater number if they so choose. Using **101** or **10000** will render the same result. `scroll-step`; however, should remain a default of **0**. – lawlist Jun 25 '14 at 01:33
If you want to position the screen exactly, you can use Ctrl-L.
By default it positions the current line in the middle of the screen.
ESC 0 Ctrl-L positions the current line at the top.

- 55,348
- 14
- 97
- 151
-
15Don't take this wrong, but what has that got to do with scrolling line by line ? (i.e. the question) – Rook Jul 15 '09 at 05:14
-
10Offering an alternative workflow that is (in the author's opinion) more natural with the tool at hand is reasonable. But you should preface it with a disclaimer to that effect, and explain why this approach is "better" if at all possible. – dmckee --- ex-moderator kitten Jul 15 '09 at 05:30
-
2@dmckee - I've used vim ... uhmm, a long time. Very good with it. However, since I'm doing something with lisp, I started forcing myself to at least get to grips with emacs again, and this "alternative" way of moving is just too much for me at the moment. Read the tutorial, know it ... but, it's just too weird, imho. Not natural. – Rook Jul 15 '09 at 05:41
-
1@Idigas: Sure. That's fair, and you don't need to take starblue's advice. But I think this kind of answer *can* be helpful if it is up-front and includes a compelling explanation. – dmckee --- ex-moderator kitten Jul 15 '09 at 05:59
-
1Yay, this does the job in a couple of keystrokes. (I Googled "how to scroll line by line" only because that seemed like a useful way to search for "how to make the code go where I want".) Thanks! – Ennael Apr 29 '15 at 16:06
-
Very useful. If you are here to use the single-line-scroll to align a piece of code on your screen, then this approach works well and is even more efficient than single-line-scrolling. – dojuba Nov 19 '18 at 08:20
-
This is actually more useful than scrolling until you find a comfortable position. Thank you for the suggestion! – brainplot Dec 16 '19 at 12:03
I'm a bit late to the party, but if you don't mind installing a package then smooth-scrolling (github, also available in MELPA) may be what you're looking for - it certainly works for me.
Once you've installed it you can pop the following in your init.el:
(require 'smooth-scrolling)
(smooth-scrolling-mode 1)
(setq smooth-scroll-margin 5)
The last line is optional; it starts scrolling near the screen edge rather than at it, so you've always got a little context around the point. Adjust to taste.

- 3
- 3

- 1,320
- 9
- 9
-
7I broke down weeping when I read this. Pretty sure this is the answer to the question. – Eric Brown Oct 11 '13 at 21:23
-
3Yes, as a Vim user trying Emacs this is exactly what I was looking for. – juanjux Sep 06 '14 at 18:16
-
i used both this solution and the scroll-step solution, the combination is fascinating when i move to the 6th line from bottom and press down it'll scroll one line, and same from top. – Emad Elsaid Mar 17 '16 at 15:29
-
Sadly, smooth-scrolling now fails quite often when you're jumping around your file using, e.g., occur-mode or searching: both can bring the point off-screen and you have to hit C-L to get your result to display. – Erik Knowles Mar 06 '19 at 22:09
My solution is not to change Emac's default scrolling, but rather to create a key sequence command from a macro. This way you have a convenient way to scroll one line at a time when you want. Not ideal, but super easy. It just happens that M-(↓) and M-(↑) are available, so that's what I used.
This is how I did it. First, you need to record a macro for one line scrolls, up and down.
Begin macro
C-x (
Scroll down one
C-u 1 C-v
Stop macro
C-x )
For scroll up one, use
C-u 1 M-v
Next you need to name the macro.
M-x name-last-kbd-macro
Give it a name when prompted like:
down-one-line
Then just use the following to bind a key sequence to that command name:
M-x global-set-key
And upon prompting, use something like:
M-(down arrow)
Then it will ask you which command you want to bind, and you should give it the name you invented earlier, e.g., down-one-line.
Here is where I got this information. You can also find instructions below and elsewhere about adding your macro to the .emacs file.
Here for macro definition explanation
Here for how to control scrolling

- 3,079
- 13
- 31
- 46

- 528
- 5
- 9
I've been using these in my .emacs
file since 2000.
(global-set-key (quote [M-down]) (quote View-scroll-line-forward))
(global-set-key (quote [M-up]) (quote View-scroll-line-backward))
This way, I can keep the Emacs default behavior as well as scroll one line at a time, depending on what I'm doing.
This worked till at least GNU Emacs 22. I recently upgraded to Emacs 24 and discovered that View-scroll-line-forward
and View-scroll-line-backward
are no longer available. After some hunting, I discovered that scroll-up-line and scroll-down-line work. So if you're using Emacs 24, you can use this.
(global-set-key (quote [M-down]) (quote scroll-up-line))
(global-set-key (quote [M-up]) (quote scroll-down-line))
I mostly skipped Emacs 23, so if that is the version you're using, you can experiment with both the above.
Note: scroll-up-line
actually scrolls one line down, because the buffer is being moved one line up.

- 5,585
- 2
- 23
- 35
-
Thanks, this works... but I hit another issue I've asked about here: http://stackoverflow.com/questions/19256551/emacs-elisp-global-set-key-bindings-not-taking-effect-for-meta-down-or-up – scorpiodawg Oct 08 '13 at 19:30
I rebind my arrow keys to perform scrolling operations.
(global-set-key [up] (lambda () (interactive) (scroll-down 1)))
(global-set-key [down] (lambda () (interactive) (scroll-up 1)))
(global-set-key [left] (lambda () (interactive) (scroll-right tab-width t)))
(global-set-key [right] (lambda () (interactive) (scroll-left tab-width t)))

- 13,619
- 3
- 34
- 34
-
2This is not exactly what I was looking for. I don't want for the screen to scroll (as it would like scroll-lock was enabled), but for the screen to "move" ... oh, ... just how notepad does it :) – Rook Mar 10 '12 at 14:07
Simples do this:
(global-set-key [M-up] (lambda () (interactive) (scroll-up 1)))
(global-set-key [M-down] (lambda () (interactive) (scroll-down 1)))
then meta cursor up moves up and meta cursor down moves down.
QED. Not sure what all the above people were smoking!
-
Or, as I just posted, you can use scroll-up-line and scroll-down-line. – Rajesh J Advani Apr 26 '13 at 05:06
-
I came here from Google search and this is still exactly what I was looking for because it fits well with `C-l` etc. +1 :) – Celdor Sep 10 '22 at 17:01
I have the following in my .emacs file to enable a nice ctrl-up, ctrl-down scrolling behavior. I also use this for the mousewheel.
(defun scroll-down-in-place (n)
(interactive "p")
(previous-line n)
(scroll-down n))
(defun scroll-up-in-place (n)
(interactive "p")
(next-line n)
(scroll-up n))
(global-set-key [mouse-4] 'scroll-down-in-place)
(global-set-key [mouse-5] 'scroll-up-in-place)
(global-set-key [C-up] 'scroll-down-in-place)
(global-set-key [C-down] 'scroll-up-in-place)

- 24,678
- 14
- 64
- 87
-
I've just tried this, but it isn't the behaviour I was looking for. It enables you to go down to the last line on screen, but upon clicking down it will still "jerk the screen by several lines up" ... don't know how to explain it better. – Rook Mar 10 '12 at 14:12
-
I like the idea of mapping the mouse wheel. But wouldn't the built-in View-scroll-line-forward and View-scroll-line-backward (or the newer scroll-up-line and scroll-down-line) work, instead of defining your own functions? – Rajesh J Advani Apr 26 '13 at 05:09
To have the "vim" scrolling put this to your .emacs file:
(defun next-line-and-recenter () (interactive) (next-line) (recenter))
(defun previous-line-and-recenter () (interactive) (previous-line) (recenter))
(global-set-key (kbd "C-n") 'next-line-and-recenter)
(global-set-key (kbd "C-p") 'previous-line-and-recenter)

- 119
- 1
- 11
-
-
The problem with the code above is that it does not support the options available for `next-line` and `previous-line`, so prefixing the custom functions with a number of lines to go is not possible. – Claudio Apr 09 '23 at 22:51
If you are looking for a quick way to create a scroll-like effect, enter in C-n
and C-l
sequentially which moves the cursor down and then centers it.

- 3,028
- 22
- 36
Since it can be annoying to use the M-up, M-down because it interferes with the org-mode which overloads these commands. To avoid this issue I personally use those commands which combine M-page-up M-page-down". Here I defined the scroll up and down to 1 line.
;;;scroll by `number-of-lines' without the cursor attached to the screen
(global-set-key [M-prior] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-down number-of-lines)
(forward-line (- number-of-lines)))))
(global-set-key [M-next] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-up number-of-lines)
(forward-line number-of-lines))))
;;;scroll by `number-of-lines' with the cursor attached to the screen
(global-set-key [S-M-prior] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-down number-of-lines))))
(global-set-key [S-M-next] (lambda () (interactive) (let ((number-of-lines 1))
(scroll-up number-of-lines))))

- 111
- 1
- 5
M-x customize-variable scroll-conservatively
Set it to 1.
You don't really want to do this, though.

- 42,082
- 9
- 61
- 86
-
5Saw it, actually - both answers, yours and ars's but on Wiki both of there are not recommended. Unfortunatelly, the reasons for that are not clearly explained. That's why I posted the question, hoping someone's found a method which works without flaws. – Rook Jul 15 '09 at 01:42
-
3would be handy if you could spell out for us noobs why this is not a good idea. – Daniel May 27 '11 at 13:26
-
The only reason I can think why this is not a good idea is that scrolling becomes slow: you'll always have to scroll by one line at a time, instead of the default half-screenful. (I guess?) – ShreevatsaR Oct 22 '11 at 03:16
-
For me is perfect. The only reason I think @jrockway says that you don't really want to do this could be because the number recommended by GNU is 10000: [5.44 How can I force Emacs to scroll only one line when I move past the bottom of the screen?](https://www.gnu.org/software/emacs/manual/html_node/efaq/Scrolling-only-one-line.html) here you have more information about [14.3 Automatic Scrolling](https://www.gnu.org/software/emacs/manual/html_node/emacs/Auto-Scrolling.html#Auto-Scrolling). – oml Apr 07 '23 at 04:54
If you don't mind using the mouse and have a scroll wheel, you can customize the variable mouse-wheel-scroll-amount by either:
C-h v mouse-wheel-scroll-amount (click on customize, change value to "Specific # of lines" 1, ApplyAndSave.)
or add to .emacs the line: '(mouse-wheel-scroll-amount '(1 ((shift) . 1) ((meta)) ((control) . text-scale)))
There are lots of possibilities listed at https://www.emacswiki.org/emacs/Scrolling

- 253
- 3
- 10
If you start emacs in .xsession, in my case setting scroll-conservatively to 100+ will not work, nor scroll-step 1. But if u start emacs after X, it works.

- 3
- 1
After playing a bit with the available configuration (emacs 26.3), I got to the following set of values:
(setq scroll-step 1
scroll-preserve-screen-position t
scroll-margin 10
scroll-conservatively 10
maximum-scroll-margin 0.0
scroll-up-aggressively 0.0
scroll-down-aggressively 0.0)
I believe the values for scroll-margin
and scroll-conservatively
do not matter much because the maximum-scroll-margin
clamps them down. They just need to be equal (maybe?).
Scroll happens line by line, even on the end of the file (worst case for me). The only missing feature was that with this the margin on top and bottom are lost.
Its a compromise and, for me, smooth scrolling is worth it.

- 103
- 5
The simplest way to get smooth one line at a time scrolling is to put into the .emacs file the line:
(setq scroll-conservatively 101)
This avoids to cope with potential problems appropriate packages with same/similar functionality come with.
In my own .emacs initialization file I have currently two one-liners which combine smooth scrolling with centered one preserving the ability to prefix scrolling with an appropriate number of lines to go:
(global-set-key (kbd "<down>") (lambda (&optional ARG TRY-VSCROLL) (interactive "^p\nP") (next-line ARG TRY-VSCROLL) (recenter)))
(global-set-key (kbd "<up>") (lambda (&optional ARG TRY-VSCROLL) (interactive "^p\nP") (previous-line ARG TRY-VSCROLL) (recenter)))
To scroll for example 20 lines down you press the Esc followed by 2 0 and then hit the arrow down ↓ key.
In between there is in the MELPA repository the package topspace ( see my answer about it here https://emacs.stackexchange.com/a/76736/40171 ) which allows smooth centered scrolling line by line including also the first line(s).
Most impressive is to combine smooth centered scrolling with the follow-mode
viewing a text file for example in three columns/windows side by side which scroll all at the same time. This way you can view three times more lines of a file at one glance as fit in one window height and scroll them all from one of the columns.

- 7,474
- 3
- 18
- 48