26

Is it possible to scroll the entire visible portion of the buffer in Emacs, but leave point where it is. Example: point is towards the bottom of the window and I want to see some text which has scrolled off the top of the window without moving point.

Edit: I suppose C-l C-l sort of does what I wanted.

2
  • C- l clears the screen Apr 22, 2018 at 6:19
  • 1
    In a terminal, yes, but not in Emacs.
    – SabreWolfy
    Apr 22, 2018 at 9:35

6 Answers 6

18

try these. Change M-n and M-p key bindings according to your taste

;;; scrollers
(global-set-key "\M-n" "\C-u1\C-v")
(global-set-key "\M-p" "\C-u1\M-v")
4
  • 4
    +1 I use something similar: (global-set-key "\M-n" (lambda () (interactive) (scroll-up 4)) ) and (global-set-key "\M-p" (lambda () (interactive) (scroll-down 4)) ).
    – Ben
    Jan 26, 2012 at 0:51
  • 11
    Unfortunately, the point will still move with after it hits the top or bottom of the window. I wonder if this is possible to avoid (without a retooling of Emacs itself)...
    – celwell
    Jul 23, 2014 at 8:59
  • @celwell: True, but I'm happy with it as a workable solution.
    – SabreWolfy
    Jul 23, 2014 at 13:20
  • @celwell There are variables that control the cursor's sensitivity to the window bounds. Perhaps you could put the scroll-down part inside a "let" block? Another thing one could try would be "save-excursion", although I'm not sure that that works with scrolling commands.
    – Stan
    Jul 5, 2018 at 15:55
11
;;;_*======================================================================
;;;_* define a function to scroll with the cursor in place, moving the
;;;_* page instead
;; Navigation Functions
(defun scroll-down-in-place (n)
  (interactive "p")
  (previous-line n)
  (unless (eq (window-start) (point-min))
    (scroll-down n)))

(defun scroll-up-in-place (n)
  (interactive "p")
  (next-line n)
  (unless (eq (window-end) (point-max))
    (scroll-up n)))

(global-set-key "\M-n" 'scroll-up-in-place)
(global-set-key "\M-p" 'scroll-down-in-place)
4
  • 1
    This should be the correct answer, scrolls in place, relative to the cursor/pointer. Thanks!!
    – cevaris
    Mar 22, 2015 at 21:31
  • 1
    To avoid compiler warnings, I replaced (previous-line n) with (forward-line (* -1 n)) and (next-line n) with (forward-line n)
    – garyp
    Nov 6, 2015 at 16:08
  • This actually works, but only if I switch the previous-line and next-line around, because it otherwise jumps by two lines
    – E. Sambo
    Jan 1, 2017 at 5:00
  • 2
    Since M-p and M-n are often reserved for other things, I bind these to C-S-y and C-S-e since they're about the same as Vim's CTRL_e and CTRL_y. Mar 27, 2017 at 22:53
8

This might be of use. According to the EmacsWiki page on Scrolling;

The variable scroll-preserve-screen-position may be useful to some. When you scroll down, and up again, point should end up at the same position you started out with. The value can be toggled by the built in mode M-x scroll-lock-mode.

3

I think this is better:

(defun gcm-scroll-down ()
      (interactive)
      (scroll-up 1))
    (defun gcm-scroll-up ()
      (interactive)
      (scroll-down 1))
    (global-set-key [(control down)] 'gcm-scroll-down)
    (global-set-key [(control up)]   'gcm-scroll-up)

reference : emacs wiki

2
  • 1
    Why do you think it's better? (And what does your code do?)
    – Stan
    Jul 5, 2018 at 15:56
  • It is better: it moves the view, not the point ; thanks a lot @Bilal
    – yPhil
    Mar 11, 2022 at 18:26
1

Based on Bilal's answer:

(global-set-key [(meta down)] (lambda () (interactive) (scroll-down 1)))
(global-set-key [(meta up)] (lambda () (interactive) (scroll-up 1)))
0
;; Preserve the cursor position relative to the screen when scrolling
(setq scroll-preserve-screen-position 'always)

;; Scroll buffer under the point
;; 'scroll-preserve-screen-position' must be set to a non-nil, non-t value for
;; these to work as intended.
(global-set-key (kbd "M-p") #'scroll-down-line)
(global-set-key (kbd "M-n") #'scroll-up-line)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.