How do you move the pointer up or down multiple lines with Emacs?
Asked Answered
G

2

15

I can move my pointer up and down one line with my arrow key just fine in Emacs, so I'd like to redefine C-n and C-p to move up and down 5 lines at a time.

I'm just beginning to learn how to use Emacs, and elisp is very alien to me. I tried using the GNU Emacs lisp reference, but I couldn't find how to bind a keystroke to multiple commands.

Here's what I have so far (concentrating on the moving up definition):

(global-set-key "\C-p"  '(loop for i in '(1 2 3 4 5) do ('previous-line))) 

But, this brings up an error message when I hit C-p, "Wrong type argument."

Any suggestions?

Thanks!

Gurule answered 17/4, 2010 at 6:57 Comment(0)
S
18

Those function (I believe next-line and previous-line) accept an optionnal argument with C-u, so i think that (next-line 5) would do what you want.

Edit: so I just tried and that would be

(global-set-key (kbd "C-n")
    (lambda () (interactive) (next-line 5)))

And the same with C-p and previous-line.

(Fiew not simple to write code in a textarea with a phone keyboard ^^)

Sanctimonious answered 17/4, 2010 at 7:7 Comment(1)
Yes! Thank you. I ended up using (next-line -5) for previous, since in the manual it says "If you are thinking of using this in a Lisp program, consider using `forward-line' with a negative argument instead. It is usually easier to use and more reliable (no dependence on goal column, etc.)."Gurule
E
1

according the warning in my emacs config, and the comment from Peter Ajtai, I propose the version that uses forward-line from my init.el

(global-set-key (kbd "C-n")
    (lambda () (interactive) (forward-line  5)))
(global-set-key (kbd "C-p")
    (lambda () (interactive) (forward-line -5)))

Of course there is also forward-char, works the same, just in a different direction.

The only thing that's missing is a complex-forward that takes a complex number as an argument, and can be used to navigate in all 4 directions :P

Episode answered 31/5, 2016 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.