reddit / emacs

I finally figured out the best way (for me, at least) to handle multi-line cursor jumping

ted-tanner · 2022-06-15 23:19
It just makes sense in my head to have M-n and M-p scroll multiple lines as an addition to the standard of having C-n and C-p go forward/backward a line. In prog-mode, that looks like forward-paragraph and backward-paragraph and in other modes that looks like jumping 6 lines at a time. This has really been a joy. I hope others find this useful as well. ;; Bind scroll 6 lines at a time to M-n and M-p. If prog-mode, remap ;; to forward-paragraph and backward-paragraph respectively (defun jump-multiple-lines-forward () (interactive) (forward-line 6)) (defun jump-multiple-lines-backward () (interactive) (forward-line -6)) (global-set-key (kbd "M-n") #'jump-multiple-lines-forward) (global-set-key (kbd "M-p") #'jump-multiple-lines-backward) (add-hook 'prog-mode-hook (lambda () (local-set-key (kbd "M-n") #'forward-paragraph) (local-set-key (kbd "M-p") #'backward-paragraph))) ;; Respect M-n and M-p key bindings in shell and eww modes. For shell mode, ;; remap next and previous command scrolling to M-N and M-P respectively (add-hook 'shell-mode-hook (lambda () (local-set-key (kbd "M-n") #'jump-multiple-lines-forward) (local-set-key (kbd "M-p") #'jump-multiple-lines-backward) (local-set-key (kbd "M-N") #'comint-next-input) (local-set-key (kbd "M-P") #'comint-previous-input))) (add-hook 'eww-mode-hook (lambda () (local-set-key (kbd "M-n") #'jump-multiple-lines-forward) (local-set-key (kbd "M-p") #'jump-multiple-lines-backward)))
emacs_expert · 2022-06-16 02:56
Nice! I used to do this, but it ended up not working well for me. My experience: there were *a lot* of packages (including built-in ones) that would set up their own keybindings, often stealing `M-n` and `M-p`. There is another package that lets you set up global keybindings, but I still had to unset these specific keys for a dozen packages. I ended up just using the default paragraph navigation (`M-{` and `M-}`, which also usually tend to work intuitively in non-prog modes. For the rest, I really like [avy](https://github.com/abo-abo/avy) for jumping straight to the line I want. Thanks for sharing, though. If you don't use a lot of packages, it's a nice and convenient setup!
deaddyfreddy · 2022-06-16 09:04
prefix argument allows you to jump by 4(,16 etc) lines at a time
ted-tanner · 2022-06-16 13:37
`M-{` and `M-}` doesn’t work well in shell mode (which I prefer over `eshell` for a myriad of reasons), and some of the text files I was using had really long or really short “paragraphs,” making `forward-paragraph` and `backward-paragraph` somewhat useless to me in those modes. That’s really what prompted the change. I’m very judicious about the packages I install. I don’t have many packages installed, so I’ve had no conflicts. If I did have a conflict with several packages, I’d probably map the same functionality back to `M-{` and `M-}`. I just really like `M-n` and `M-p` as they seem intuitive and require two rather than three key presses, but I’d consider switching back if there were good reason to.
emacs_expert · 2022-06-16 18:00
> I just really like M-n and M-p as they seem intuitive and require two rather than three key presses Yep, agreed. Happy it works for you.
cradlemann · 2022-06-17 16:56
I just recently discover `beginning-of-defun` and `end-of-defun` functions and they are the most used functions for me right now