Sublime Forum

ST2: How to Page Up/Down without moving the cursor


I’m on OS X 10.8.4 using ST2. When I use the Home and End keys, the viewport moves and the cursor is left alone. This is standard Mac behavior, and what I’d expect.

However, when I use Page Up (pageup/pgup) and Page Down (pagedown/pgdn), the cursor moves along with the viewport. This is not how other apps on my platform behave, and it’s kind of driving me crazy.

How can I get ST2 to leave the cursor alone when using Page Up/Down?



I have partly figured this out. I have most of the behavior I want by adding this code to my user key bindings file (Sublime Text 2 > Preferences > Key Bindings - User):

	{ "keys": "pageup"], "command": "scroll_lines", "args" : {"amount": 30.0} },
	{ "keys": "pagedown"], "command": "scroll_lines", "args" : {"amount": -30.0} }

Obviously, that’s not a complete solution, since it doesn’t move the viewport by a page at a time. Is there some way to get the number of lines displayed per page, and pass that into the “scroll_lines” command? Thanks in advance!



You’d probably have to write a plugin… Here:

lines_in_view = len(view.lines(view.visible_region())) first_line,_ = view.rowcol(view.visible_region().begin()) + lines_in_view, 0))



Beware that scroll_lines move the cursor along the viewport when you have only one cursor without selection.
This is not the behavior that I expect and I wrote this small plugin to resolve this issue:

[code]import sublime, sublime_plugin

class ScrollLinesFixedCommand(sublime_plugin.TextCommand):
“”“Must work exactly as builtin scroll_lines command, but without moving the cursor when it goes out of the visible area.”""
def run(self, edit, amount, by=“lines”):
# only needed if one empty selection
if by != “lines” or (len(self.view.sel()) == 1 and self.view.sel()[0].empty()):
maxy = self.view.layout_extent()[1] - self.view.line_height()
curx, cury = self.view.viewport_position()
if by == “pages”:
delta = self.view.viewport_extent()[1]
delta = self.view.line_height()
nexty = min(max(cury - delta * amount, 0), maxy)
self.view.set_viewport_position((curx, nexty))
self.view.run_command(“scroll_lines”, {“amount”: amount})

{ “keys”: “ctrl+up”], “command”: “scroll_lines_fixed”, “args”: {“amount”: 1.0 } },

{ “keys”: “ctrl+down”], “command”: “scroll_lines_fixed”, “args”: {“amount”: -1.0 } },

{ “keys”: “ctrl+pageup”], “command”: “scroll_lines_fixed”, “args”: {“by”: “pages”, “amount”: 1.0 } },

{ “keys”: “ctrl+pagedown”], “command”: “scroll_lines_fixed”, “args”: {“by”: “pages”, “amount”: -1.0 } },[/code]

1 Like


Thank you, thank you! This is just what I was hoping for. I’m annoyed that this is so difficult, but now at least the next person will be able to Google for this solution.



Thank you so much bizoo. and nk9.
It’s been a major help for me.