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?

  • 1
    Not exactly what you asked for, but maybe you don't know about this: you can use C-l to manually recenter.
    – paprika
    Nov 18 '14 at 10:46
  • 2
    M-x scroll-lock-mode
    – Nsukami _
    Nov 18 '14 at 10:47

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)))
(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.
    – rekado
    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?
    – rekado
    Nov 18 '14 at 11:10
  • 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?
    – rekado
    Nov 18 '14 at 11:28
  • 2
    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. Oct 16 '20 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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