Sublime Forum

Caret follow scroll

#1

Wondering if there is a way to enable that a caret always follow the scroll viewport in sublime similar that to how emacs has it. I know emacs works differently of course and I am not saying it has to be exactly like that, but it enables you is to always keep the caret in the visible viewport of that part of the page so that even if you caret is at line 55 of the code and you scrolled all the way down below to line 200 something and when you caret is no longer in the visible region viewport of the editor, that this plugin, setting or feature would allow it to always follow your scroll and stay on top of it.

If somebody knows anything related to it I would appreciate any information you can provide. Thank you for taking the time to help out. Stay Sublime!

1 Like

#2

I’ve never used emacs so just want to clarify something: do I understand you correctly that you want the caret to move when you scroll. If you scroll down, the caret should move to the top of the viewport if it would otherwise be out of view. And if you scroll up, it would “stick” to the bottom of the viewport once it would otherwise be scrolled off screen?

0 Likes

#3

Yes exactly, I only mentioned emacs because it is the only editor I believe along side Vim that I know of and have seen that does that, and it is really neat, because it saves me a ton of time, if you are working with large files, large meaning in code lines, and you finally find something that you want to edit/change whatever, once you move the caret it moves your view to where the caret is and you lose track of where you were.

If the caret were to move with you it would always be on top/bottom depending on the direction of the scroll and it would make it easier to navigate, get around I guess.

1 Like

#4

If you use the arrow keys and page up/down to move around the document, you get this for free. If you want it to work that way when scrolling the mouse wheel, or clicking on the minimap etc, you are kinda out of luck for now because ST doesn’t expose any events to notify when the viewport moved. You can write a plugin which would check on a timer if the caret is out of view and move it, but it might not feel right…

1 Like

#5

FWIW, automatically moving caret when scrolling viewport would break any multi-cursor-operation with related carets not fitting into viewport. It only works for single-cursor editors.

When scrolling via mouse, it should be the most natural operation to left click once onto or nearby the location, which is to be edited.

Instead of just moving caret via scrolling events, it might be possible to override normal caret movement commands and move it into viewport, before continuing with original operation.

Some time ago, someone complained about pageup/pagedown reverting to old caret location after scrolling far away. It was solved by following plugin, which could maybe extended to hijack up/down/left/right keys.

import sublime
import sublime_plugin


class VisualMovePageCommand(sublime_plugin.TextCommand):
    """
    This class describes a visual move page command.

    This command moves the caret to center of visible viewport,
    if it is not already within, and starts paging up/down from their.

    ```json
    { "command": "visual_move_page", "args": {"forward": true} }
    { "command": "visual_move_page", "args": {"forward": false} }
    ```

    Pressing pageup/pagedown moves caret up/down by one page, from its current
    position even if it is far way from currently visible viewport, which may
    cause unexpected visual scrolling.
    """
    def run(self, edit, forward=True):
        sels = self.view.sel()
        visible_region = self.view.visible_region()
        if len(sels) == 1 and sels[0] not in visible_region:
            first_row, _ = self.view.rowcol(visible_region.begin())
            last_row, _ = self.view.rowcol(visible_region.end())
            centered_row = int((first_row + last_row) / 2)
            centered_pt = self.view.text_point(centered_row, 0)

            # adjust caret position
            self.view.sel().clear()
            self.view.sel().add(centered_pt)

        # call normal page up/down command
        self.view.run_command("move", {"by": "pages", "forward": forward})
[
	// Move By Page (visible viewport instead of current caret location)
	{
		"keys": ["pageup"],
		"command": "visual_move_page", "args": { "forward": false }
	},
	{
		"keys": ["pagedown"],
		"command": "visual_move_page", "args": { "forward": true }
	},
]
1 Like

#6

Yeah thank you all for the feedback, I will make sure to give this plugin a try perhaps, although do not really see the point in it because I believe I have something similar to this already that I did on my own.

Anyways, thanks guys for the valuable feedback appreciate it, have a nice one :grinning:

0 Likes