7

I am editing a large file. I would like the current line where the cursor is at to be always in the middle of the screen. Is this possible?

Name
  • 7,689
  • 4
  • 38
  • 84

1 Answers1

8

There is a mode for that: centered-cursor-mode. It can be installed from MELPA.

It does not play well with a couple of modes, so this is my configuration:

;; keep the cursor centered to avoid sudden scroll jumps
(require 'centered-cursor-mode)

;; disable in terminal modes
;; http://stackoverflow.com/a/6849467/519736
;; also disable in Info mode, because it breaks going back with the backspace key
(define-global-minor-mode my-global-centered-cursor-mode centered-cursor-mode
  (lambda ()
    (when (not (memq major-mode
                     (list 'Info-mode 'term-mode 'eshell-mode 'shell-mode 'erc-mode)))
      (centered-cursor-mode))))
(my-global-centered-cursor-mode 1)

As @Nsukami mentioned, an alternative is the built-in scroll-lock-mode.

  • Thank you for your solution. I applied your solution, but when I move by arrows, the position of current line changes and it is no more at the middle of screen. – Name Nov 18 '14 at 10:52
  • Oh, I think I misunderstood your question then. centered-cursor-mode will keep the *current* line in the centre. When you move the cursor to another line, *that* line will be in the centre. –  Nov 18 '14 at 11:07
  • @Name: do you want to disable scrolling completely so that no matter where the cursor is (even hundreds of lines away) the selected line remains in the middle of the screen? –  Nov 18 '14 at 11:10
  • @redako yes exactly, I would like the selected line always remains in the middle of the screen. – Name Nov 18 '14 at 11:23
  • 1
    @Name: disabling vertical scrolling altogether is rather extreme. Would it not be acceptable to split the window in two (`C-x 2`) and scroll in one window while keeping the selected line in the other? –  Nov 18 '14 at 11:28
  • 3
    IMHO, `scroll-lock-mode` is *not* an alternative. It is broken. I loses centering in many cases, and doesn’t even try to re-center if you enter lines. – Torsten Bronger Oct 16 '20 at 20:53
  • yes, ``scroll-lock-mode`` doesn't work as I would expect it. ``centered-cursor-mode`` works properly – slk500 May 03 '23 at 14:17