Search code examples
pluginssublimetext3sublimetext2sublimetextsublime-text-plugin

How to keep current line centered vertically on the screen in Sublime Text?


I want to keep the currently selected line at the vertical center of the screen.

In VIM this is achieved with the :set scrolloff=9999 command.

Is there any option or plugin to do that in Sublime Text?


Solution

  • import sublime_plugin
    class AlwaysCenterCommand(sublime_plugin.EventListener):
        def on_modified(self, view):
            sel = view.sel()
            pt = sel[0].begin() if len(sel) == 1 else None
            if pt != None:
                view.show_at_center(pt)
    

    Center if not multi selection and modifying. That is it.

    Or alternatively you could just use the region.

    import sublime_plugin
    class AlwaysCenterCommand(sublime_plugin.EventListener):
        def on_modified(self, view):
            sel = view.sel()
            region = sel[0] if len(sel) == 1 else None
            if region != None:
                view.show_at_center(region)
    

    https://forum.sublimetext.com/t/always-centered-cursor/4005


    Another Option

    Buffer Scroll is a simple Sublime Text plug-in which remembers and restores the scroll, cursor positions, also the selections, marks, bookmarks, foldings, selected syntax and optionally the colour scheme, when you open a file. Will also remember different data depending the position of the file in the application (example file1 in window1 has scroll line 30, file1 in window2 has scroll in line 40)

    Also, via preferences, allows to enable syncing of scroll, bookmarks, marks and folds between cloned views, live.

    Syncing features are disabled by default. You need to enable these via the preferences. Main menu -> Preferences -> Package Settings -> BufferScroll -> Settings Default. You may want to copy and paste your edited preferences to "Settings Users" located under the same sub-menu. To keep your preferences between updates.

    Requested by Kensai this package now provides "typewriter scrolling": The line you work with is automatically the vertical center of the screen.

    https://github.com/titoBouzout/BufferScroll