18

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.

1
  • 1
    This question helped me to find how to use the Page Up and Page Down as keyboard shortcuts on Sublime. Thanks!
    – Ricardo
    Commented Nov 5, 2015 at 2:12

3 Answers 3

19

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

1
  • 1
    I'm on SublimeText 2 2.0.2 (2221) and the behavior hasn't changed. The cursor moves along with the viewport when you pageup/down, unless you make the fix I describe above.
    – Nick K9
    Commented May 17, 2016 at 17:26
9

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.
3
  • This doesn't work as described. Which file do you paste the plugin into? If in Preferences > Key Bindings > User, presumably outside the [...] list? But then there's a syntax error because amount is undefined??
    – smci
    Commented Jul 15, 2014 at 1:01
  • I just set up a new instance of SublimeText 2 (2.0.2) using these steps, and they are working as described. Are you sure you're not using ST 3?
    – Nick K9
    Commented May 17, 2016 at 17:27
  • Thanks Nick for suggesting. It's been two years since I touched it, but unless my memory is playing tricks it was ST 2.x
    – smci
    Commented May 18, 2016 at 0:48
5

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.

2
  • That will only scroll one line up or down, though. I wanted a page of scrolling based on the size of the viewport, like in TextEdit and any other text editor. The plugin from my answer below lets you do that. Thanks for commenting, though!
    – Nick K9
    Commented Dec 13, 2013 at 13:47
  • 2
    Changed the "amount": 1.0 to "amount": 30.0 which gets it to scroll 30 lines at a time. sufficient for what I was looking for. Thanks! Commented Sep 26, 2016 at 18:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.