Add to your Key Bindings -- User:
- Code: Select all
/* Scrolloff keybinds */
{ "keys": ["up"], "command": "scroll_off", "args": { "forward": false } },
{ "keys": ["down"], "command": "scroll_off", "args": { "forward": true } },
Add to your Settings -- User: (Optional, defaults to 5)
- Code: Select all
"scrolloff": <positive integer>
Save as Packages/User/ScrollOffCommand.py:
- Code: Select all
import sublime, sublime_plugin
class ScrollOffCommand(sublime_plugin.TextCommand):
def run(self, edit, forward):
self.view.run_command("move", {"by": "lines", "forward": forward})
limit = abs(int(self.view.settings().get("scrolloff"))) or 5
cursor = self.view.rowcol(self.view.sel()[0].begin())[0]
view_top = self.view.rowcol(self.view.visible_region().begin())[0]
view_bottom = self.view.rowcol(self.view.visible_region().end())[0]
absolute_bottom = self.view.rowcol(self.view.size())[0]
if cursor < view_top + limit:
self.view.set_viewport_position((0, (view_top - 1) * self.view.line_height()))
elif cursor <= absolute_bottom - limit and cursor >= view_bottom - limit:
self.view.set_viewport_position((0, (view_top + 1) * self.view.line_height()))
Note that I opted to override the default up/down keybindings to avoid headaches with mouse selection and cursor jumping via CTRL+Home, CTRL+End, etc.
There is some strange behavior I don't know how to sort out. For instance, the cursor moves faster than the viewport for long scrolls, so the scrolloff limit isn't maintained. I've also had a few files where the cursor reaches the viewport bottom boundary when there is still content in the file (ie, not the absolute_bottom) . If anyone is willing to look into these issues I'd appreciate it.