Home Download Buy Blog Forum Support

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

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

Postby nk9 on Wed Jun 19, 2013 7:07 pm

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?
nk9
 
Posts: 3
Joined: Wed Jun 19, 2013 7:01 pm

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

Postby nk9 on Thu Jun 20, 2013 11:43 am

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):

Code: Select all
[
   { "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!
nk9
 
Posts: 3
Joined: Wed Jun 19, 2013 7:01 pm

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

Postby adzenith on Thu Jun 20, 2013 2:39 pm

You'd probably have to write a plugin... Here:

Code: Select all
lines_in_view = len(view.lines(view.visible_region()))
first_line,_ = view.rowcol(view.visible_region().begin())
view.show(view.text_point(first_line + lines_in_view, 0))
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

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

Postby bizoo on Thu Jun 20, 2013 3:35 pm

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: Select all
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]
         else:
            delta = self.view.line_height()
         nexty = min(max(cury - delta * amount, 0), maxy)
         self.view.set_viewport_position((curx, nexty))
      else:
         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 } },
bizoo
 
Posts: 886
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

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

Postby nk9 on Fri Jun 21, 2013 10:04 am

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.
nk9
 
Posts: 3
Joined: Wed Jun 19, 2013 7:01 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 56 guests