Sublime Forum

Sort lines, keep window position and remove duplicates


The main reason I wrote this was to keep the window position when sorting. The built-in command scrolls elsewhere.

It also deletes any duplicate lines.

Here’s the code.

import sublime, sublime_plugin

# Written by Stephen.S on 2021.04.23.
# To get over the bug in ST where the window position is not remembered.
# Also removes any duplicate lines.

class SortlinesRemoveDuplicatesKeepWinPosition(sublime_plugin.TextCommand):

  def run(self, view):
    view = self.view
    col, row = view.viewport_position() # Get the position of the window.
    view.run_command('sort_lines', {"case_sensitive": False, "reverse": False, "remove_duplicates": True})
    view.set_viewport_position([col, row]) # Set the position of the window.


You probably want to remove duplicates after sorting…

1 Like


Thanks. I’ve come a cropper. It’s doing it for the whole document even when something is selected.

Do you know how to sort just the selection with the sort_lines command?



No, but maybe this will help you

1 Like


Thanks. I fixed the issue. Updated the code in the starting post.