How to scroll line by line in GNU Emacs?

Emacs

Emacs Problem Overview


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.

Emacs Solutions


Solution 1 - Emacs

See some of the suggestions on the Emacs Wiki:

(setq scroll-step            1
      scroll-conservatively  10000)

Solution 2 - Emacs

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.

Solution 3 - Emacs

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.

Solution 4 - Emacs

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

Solution 5 - Emacs

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.

Solution 6 - Emacs

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)))

Solution 7 - Emacs

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!

Solution 8 - Emacs

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)

Solution 9 - Emacs

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.

Solution 10 - Emacs

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)

Solution 11 - Emacs

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))))

Solution 12 - Emacs

M-x customize-variable scroll-conservatively

Set it to 1.

You don't really want to do this, though.

Solution 13 - Emacs

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

Solution 14 - Emacs

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.

Solution 15 - Emacs

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRookView Question on Stackoverflow
Solution 1 - EmacsarsView Answer on Stackoverflow
Solution 2 - EmacsstarblueView Answer on Stackoverflow
Solution 3 - EmacsStuart HickinbottomView Answer on Stackoverflow
Solution 4 - EmacsJosh CasonView Answer on Stackoverflow
Solution 5 - EmacsRajesh J AdvaniView Answer on Stackoverflow
Solution 6 - EmacsDerek SlagerView Answer on Stackoverflow
Solution 7 - EmacsAdamView Answer on Stackoverflow
Solution 8 - EmacsAlex BView Answer on Stackoverflow
Solution 9 - EmacsMichael David WatsonView Answer on Stackoverflow
Solution 10 - EmacsjjpikoovView Answer on Stackoverflow
Solution 11 - EmacsromwelView Answer on Stackoverflow
Solution 12 - EmacsjrockwayView Answer on Stackoverflow
Solution 13 - EmacsPhilipView Answer on Stackoverflow
Solution 14 - EmacssebView Answer on Stackoverflow
Solution 15 - EmacstcpaivaView Answer on Stackoverflow