Reputation: 62528
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.
Upvotes: 110
Views: 40497
Reputation: 796
This is a good collection of settings. Together, these should do what you want.
;; scroll one line at a time (less jumpy than defaults)
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ;; one line at a time
(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq mouse-wheel-follow-mouse 't) ;; scroll window under mouse
(setq scroll-step 1) ;; keyboard scroll one line at a time
(I think I pulled this off a forum or blog someplace a few years ago. If you believe you are the author of this code, just drop a comment and I'll be happy to update this answer with proper credit.)
Upvotes: 0
Reputation:
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.
Upvotes: 1
Reputation: 123468
See some of the suggestions on the Emacs Wiki:
(setq scroll-step 1
scroll-conservatively 10000)
Upvotes: 61
Reputation: 143
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.
Upvotes: 2
Reputation: 273
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
Upvotes: 2
Reputation: 1360
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.
Upvotes: 47
Reputation: 131
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))))
Upvotes: 2
Reputation: 3
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.
Upvotes: 0
Reputation: 3071
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.
Upvotes: 3
Reputation: 119
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)
Upvotes: 4
Reputation: 548
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
Upvotes: 30
Reputation: 5710
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.
Upvotes: 32
Reputation: 131
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!
Upvotes: 13
Reputation: 24926
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)
Upvotes: 5
Reputation: 13831
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)))
Upvotes: 13
Reputation: 56752
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.
Upvotes: 57
Reputation: 42674
M-x customize-variable scroll-conservatively
Set it to 1.
You don't really want to do this, though.
Upvotes: 0