Search code examples
sublimetext2sublimetext3sublimetext

Sublime Text 2/3 - How to always have at least 10 lines under current line?


Is there a way to show at least x number of lines below a current line in ST? Say my cursor is on line 55, I want Sublime to display at least 10 more lines below the current line so line 55 will never be at the bottom of the screen. Is this possible in Sublime?


Solution

  • Build 4075 added a setting called scroll_context_lines for this.

    If you are using an older build, you can achieve this with a simple plugin, that listens for cursor movement events.

    From the Tools menu in Sublime Text, click New Plugin. Replace the contents with the following:

    import sublime, sublime_plugin
    
    class ShowLinesUnderSelectionListener(sublime_plugin.EventListener):
        def show_lines_under_selection(self, view, number_of_lines_to_show):
            cursor_pos = view.sel()[0].end()
            row, col = view.rowcol(cursor_pos)
            desired_pos = view.text_point(row + number_of_lines_to_show, col)
            if not view.visible_region().contains(desired_pos):
                view.show(desired_pos, False)
        
        def on_post_text_command(self, view, command_name, args):
            if command_name in ('word_highlight_click', 'move', 'move_to', 'insert'):
                self.show_lines_under_selection(view, 10)
        
        def on_post_window_command(self, window, command_name, args): # for Vintageous support
            if command_name in ('press_key'):
                self.show_lines_under_selection(window.active_view(), 10)
    

    Save it to the folder it suggests as something like show_lines_under_cursor.py.

    This will ensure that there are always 10 visible lines under the cursor. Note that once you reach the bottom of the file, it won't scroll any further to show 10 non-existing-in-file lines. I'm not sure if this is possible via the API.