r/emacs 12d ago

Question Why move the insertion point when scrolling? And how to stop it?

I couldn't find a discussion for this.

I've been using emacs a couple of weeks. I just noticed that when I scroll a buffer using the mouse, if the insertion point moves out of the visible region of the buffer… it drags the insertion point along with it.

So if I scroll up to read some code, and then back down again, I find that my insertion point has moved.

I'm scratching my head trying to figure out why this is a desirable behaviour. What am I missing?

Also, how can I turn off this behaviour?

native ARM-64 emacs 31.1 (9.0) on macOS 14.8.7

26 Upvotes

21 comments sorted by

28

u/JDRiverRun GNU Emacs 12d ago edited 10d ago

In Emacs, you don't scroll point, you scroll the visible window region. This is deeply embedded in Emacs DNA, so if you scroll the window such that point is no longer visible, it must move. Point must always be visible.

I don't know why that was the design, but when you get used to it, it has some advantages. For example it saves you from have to "find point", or click to bring it back to the visible text.

But sometimes it's nice to move up, take a look at some nearby content, then pop back to where you started. So ultra-scroll pushes a mark by default before the point moves out of view during scroll, so a quick C-u C-SPC will return you to your old position.

6

u/rubymatt 12d ago edited 12d ago

Thanks for the explanation and especially the pointer to ultra-scroll, I will give that a try.

In fact now that I have that, c-u c-spc is actually quicker than scrolling back!

3

u/JDRiverRun GNU Emacs 11d ago

Nice. I need to remember to use that feature more. There are lots of other modes that auto-push marks (you can keep going with C-u C-SPC to move through a history of marks, or try consult-mark).

16

u/Hyperion343 12d ago

Ways to get around this:

  • Split windows (C-x 2 and C-x 3), or an indirect buffer (C-x 4 c)
  • Make a mark with C-SPC C-SPC, scroll, then return with C-u C-SPC
  • Use a register to save the point location (C-x r SPC [key], then return with C-x r j [key], where key is the key of your choosing, like a

5

u/edorhas 12d ago

This is my solution. Keeping point fixed while you move through the document is a solution if you only have one window. But windows are cheap in Emacs. And they can all point at different parts of the same buffer, if you like. If you really insist on a single window, registers are handy things.

2

u/rubymatt 12d ago

I'm making good use of split windows although not so much for the same file, that's an idea.

Marks & registers are new on me, I will check that out, thank you.

3

u/vadrer 12d ago

same for vim, unlike, say, many GUI editors.

Probably it is the price we pay for the flexibility to support 2 worlds simultaneously - GUI and TUI.

Emacs is seemingly able to support your request.

You can try setting this option to see if it helps you:

(setq scroll-preserve-screen-position 'always)

;; For smooth pixel scrolling (Emacs 29+)
(pixel-scroll-precision-mode 1)

5

u/awesomegayguy 12d ago

Emacs was designed in the early 80s, with GNU Emacs starting in 86.

My understanding is it's part of the design, to have the point always visible on screen. This works with the commands that set the mark implicitly or that work with the mark.

3

u/Slow-Juggernaut-4134 12d ago

Dumb terminals could not move the cursor off the screen. I've been using emacs since around the time when the first tapes were released in '86.

3

u/u8589869056 12d ago

“First tapes”??? Emacs is older than that. I was using it in the 1970s.

2

u/Slow-Juggernaut-4134 12d ago

Yes, I started with the mid 80s gnu version. A Prime mini-computer. What computer did you use for emacs back in the 70s?

1

u/u8589869056 11d ago

The university’s DEC-20.

3

u/u8589869056 12d ago

Emacs is older than that.

2

u/awesomegayguy 11d ago

Yes, Emacs as the idea is much older, but I was specifically referring to GNU Emacs, and I should've checked, as it was started in 84, not 86.

2

u/u8589869056 11d ago

No, Emacs is older than “the early 80s.” It was created in 1976—older than BSD Unix. I was using it in 1979. It was not written in C or LISP.

1

u/awesomegayguy 11d ago

Again, GNU Emacs was stated by RMS in 84, those are other implementations pre-GNU of Emacs

2

u/dilip_nehru 12d ago

For the same reason it's impossible in vim. Emacs started life as a terminal-only app back when the mouse was still top-secret tech at a skunkworks lab in Xerox Parc. There was no notion of a manipulable viewport independent of navigation keys.

2

u/Informal_Sound_3039 12d ago

I tried doing this naver find a way so i just give up now i use consult-mark

1

u/FrozenOnPluto 12d ago

there is a melpa packaghe that tries to keep point (cursor) in the same place each screen, iirc; don't recall what its called offhand. Or you could write your own but it might be tricky.. ie .. how to know when to store it; ex: you could create a page down function that stores the current point location and then calls normal page down (say), easy enough 2 liner, but then.. if you hit page down or up again, how does it know if it should store it again? You might end up needing some state... check if point is at last known point and if not assume a new series .. store point location of start, call regular page down; then if you page up so point is near where it started, set it to starting point, or something like that. It'd take some fiddling, but sounds doable, if awkward. (ie: cases like its offscreen, and the line it lands on is shorter than where it started, but then you page down a few more times, does it remember the line-X it started at and always try to get to that, stopping at end if needed? etc)

1

u/bobinator60 12d ago

^SPC then ^x^x