ChatGPT解决这个技术问题 Extra ChatGPT

How to scroll line by line in GNU Emacs?

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.

I'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: Quit bashing - this is working as designed, but can be changed (or "fixed") with a simple setting.
@Teddy and what is this "simple setting"?
@Teddy yeah, what is it?
@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.

P
Philippe Fanaro

See some of the suggestions on the Emacs Wiki:

Emacs Wiki: Smooth Scrolling

(setq scroll-step            1
      scroll-conservatively  10000)

Saw 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.
I 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.
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?
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!
@ars groups.google.com/group/gnu.emacs.help/browse_thread/thread/… 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
s
starblue

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.


Don't take this wrong, but what has that got to do with scrolling line by line ? (i.e. the question)
Offering 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 - 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.
@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.
Yay, 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!
H
Hans Maree

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.


I broke down weeping when I read this. Pretty sure this is the answer to the question.
Yes, as a Vim user trying Emacs this is exactly what I was looking for.
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.
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.
H
Hulk1991

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


R
Rajesh J Advani

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.


Thanks, this works... but I hit another issue I've asked about here: stackoverflow.com/questions/19256551/…
D
Derek Slager

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

This 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 :)
k
kleopatra

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.
A
Alex B

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)

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.
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?
M
Michael David Watson

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.


j
jjpikoov

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)

@Matt it does for me
r
romwel

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

j
jrockway

M-x customize-variable scroll-conservatively

Set it to 1.

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


Saw 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.
would be handy if you could spell out for us noobs why this is not a good idea.
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?)
P
Philip

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


s
seb

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.


t
tcpaiva

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.