2

I find it convenient to move by multiple lines for quickly scrolling up/down:

;; replaces forward-sentence
(global-set-key (kbd "M-e")
                (lambda () (interactive) (next-line 3)))

;; replaces backward-sentence
(global-set-key (kbd "M-a")
                (lambda () (interactive) (previous-line 3)))

However, this gives doesn't save the column of the cursor after you switch directions. For example, suppose a buffer contains the following:

This is line 1. 
This is line 2.
This is line 3.

This is line 5.

Put your cursor on the "1" in the first line, then press C-n 3 times, then press C-p 3 times. You should find that you end up back on the "1".

Now, define M-e and M-a using the above keyboard shortcuts. Try putting your cursor on the "1", press M-e once, then M-a once. Your cursor ends up at the beginning of the first line! (Tested on Emacs 24.5.1).

How can you move multiple lines in one keypress and preserve the column after switching directions? I tried replacing next-line with forward-line... no dice.

2

The original poster may wish to use the universal-argument to move N lines up or down -- e.g., C-u 3 up and C-u 3 down; or C-3 up and C-3 down.

Internal functions within simple.el (relating to next-line and previous-line) check the value of the variable last-command. Since the original poster created new lambda functions attached to keyboard shortcuts, it is necessary to expressly set the value of this-command -- which becomes last-command towards the end of the current command loop.

;; replaces forward-sentence
(global-set-key (kbd "M-e")
  (lambda ()
    (interactive)
    (setq this-command 'next-line)
    (next-line 3)))

;; replaces backward-sentence
(global-set-key (kbd "M-a")
  (lambda ()
    (interactive)
    (setq this-command 'previous-line)
    (previous-line 3)))
  • Unfortunately it seems like highline.el is interfering with this approach. It's probably a bug in that package. To answer your question: typing C-u 3 down is tedious if I want to quickly scroll down to a line that's, say, 30 lines down from the current cursor position. – user2445797 Nov 28 '16 at 1:55

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.