Skip to content

Instantly share code, notes, and snippets.

@justsml
Last active August 8, 2023 17:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justsml/1cc1627fd69adbcd453a2dd54962cbff to your computer and use it in GitHub Desktop.
Save justsml/1cc1627fd69adbcd453a2dd54962cbff to your computer and use it in GitHub Desktop.
Add Page Up and Page Down Keyboard Bindings to Sublime Text 3 (and 2)

Add PageUp PageDown (Keyboard Shortcut Scrolling) to Sublime Text 3 (and 2)

  • Save ScrollLinesFixedCommand.py to ~/Library/Application Support/Sublime Text 3/Packages/User/ folder
  • Copy the suggested usage from sublime-keymap.json into your ~/Library/Application Support/Sublime Text 3/Packages/User/Default (OSX).sublime-keymap

Hey Sublime, why the hell this isn't an available keybinding by default? Seriously, I'm tired of looking up how to do this every so often. For most of my work I've switched to Visual Studio Code, the Open Source IDE from Microsoft (still sounds wierd)... But it's genuinely amazing.

# Credit: https://forum.sublimetext.com/t/st2-how-to-page-up-down-without-moving-the-cursor/10434/4
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": ["alt+up"], "command": "swap_line_up" },
{ "keys": ["alt+down"], "command": "swap_line_down" },
{ "keys": ["super+alt+up"], "command": "scroll_lines_fixed", "args": {"by": "pages", "amount": 1.0 } },
{ "keys": ["super+alt+down"], "command": "scroll_lines_fixed", "args": {"by": "pages", "amount": -1.0 } }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment