SabreWolfy
SabreWolfy

Reputation: 5540

Emacs: scroll buffer not point

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.

Upvotes: 27

Views: 6589

Answers (6)

yPhil
yPhil

Reputation: 8357

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

Upvotes: 2

Arch Stanton
Arch Stanton

Reputation: 394

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

Upvotes: 0

Bilal Qadri
Bilal Qadri

Reputation: 141

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

Upvotes: 3

Chris McMahan
Chris McMahan

Reputation: 2680

;;;_*======================================================================
;;;_* 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)

Upvotes: 11

kindahero
kindahero

Reputation: 5867

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

Upvotes: 18

Marvin Pinto
Marvin Pinto

Reputation: 30980

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.

Upvotes: 8

Related Questions