Nearly every command works based on caret position and so do move
commands.
Pressing
-
pageup
calls move {"by": "pages", "forward": false}
-
pagedown
calls move {"by": "pages", "forward": true}
Those commands actually move the caret by a certain amount of lines up and down. The viewport scrolling is just a side-effect to reveal new caret location.
PageUp/PageDown behavior after “find” also depends on where caret is positioned.
This is an aspect applications indeed behave different:
-
VS Code, gedit and Sublime Text, just start moving caret from its current caret position, even if it is far away from currently visible viewport. As a result viewport may scroll way more than a page to reveal caret’s new location.
-
LibreOffice Writer and Windows Notepad first move caret to currently visible viewport and start paging up/down from this new location. That’s what you probably expect to happen.
I’d say both of which have their reasons for existance.
The scheme I see is advanced code editors behaving equally as those expect keyboard navigation, primarily. Scrolling via mouse is more or less a 2nd class feature, which is often only used to quickly peek some code and quickly return to last edit position. A related feature request is https://github.com/sublimehq/sublime_text/issues/6425 for instance.
I am not aware of any setting to adjust ST’s behavior.
You can either click into visible viewport, before pressing pageup/down, to place caret into it, manually or use the following plugin.
Note: Maybe there’s an easier way to move a caret to visible viewport with an existing command, but I am not aware of it at the time of writing this post.
Packages/User/pager.py
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})
Packages/User/Default.sublime-keymap
[
{
"keys": ["pageup"],
"command": "visual_move_page", "args": { "forward": false }
},
{
"keys": ["pagedown"],
"command": "visual_move_page", "args": { "forward": true }
},
]