Question

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 Mac apps behave, and I'd like the cursor to be left alone for these keys too.

I've been able to get this half-working by adding this to my key bindings:

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

However, the amounts there are hard-coded. It looks like viewport_extent will get me the height of the viewport, but how can I use that from within the key bindings file? Is this even the correct solution for this? I feel like it's an awful lot of work to go to to get this behavior.

Thanks in advance.

Was it helpful?

Solution 2

To do this requires a text plugin. Thanks to user bizoo on the ST Forums, you don't have to write this yourself:

http://www.sublimetext.com/forum/viewtopic.php?f=3&t=12793

This works exactly as I'd expect.


Sublime Text 3 Update: You can follow the instructions below, with the minor change that the file should end with .py (e.g. scroll_lines_fixed.py) and should be loose in the ~/Library/Application Support/Sublime Text 3/Packages/User/ folder.


Sublime Text 2 Update: This isn't clear, and also uses a bare URL which could conceivably die in the future. So here's a more complete explanation of what you need to do.

  1. Add these four lines to Sublime Text 2 > Preferences > Key Bindings - User, inside any square brackets that are already in the file:

    [
        { "keys": ["ctrl+up"], "command": "scroll_lines_fixed", "args": {"amount": 1.0 } },
        { "keys": ["ctrl+down"], "command": "scroll_lines_fixed", "args": {"amount": -1.0 } },
        { "keys": ["pageup"], "command": "scroll_lines_fixed", "args" : {"by": "pages", "amount": 1.0 } },
        { "keys": ["pagedown"], "command": "scroll_lines_fixed", "args" : {"by": "pages", "amount": -1.0 } }
    ]
    
  2. Within Sublime Text, choose the Tools > New Plugin… option from the menu bar.
  3. Replace the contents of the new file with this:

    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})
    
  4. Save the file to ~/Library/Application Support/Sublime Text 2/Packages/ScrollLinesFixed/. You will need to create the ScrollLinesFixed folder.
  5. There's no step 5.

OTHER TIPS

Just using Fn+up to pageup and Fn+down to pagedown.

Just my 2¢, but I have mine setup to scroll up or down with the following:

{ "keys": ["super+up"], "command": "scroll_lines", "args": {"amount": 1.0} },
{ "keys": ["super+down"], "command": "scroll_lines", "args": {"amount": -1.0} }

I use a Mac, so the "super" key is the command key, which is the first key to the left (or right) of the space bar. Not sure what the equivalent would be on Windoze; perhaps it would be the "Start" key or something. Anyway, works like a charm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top