Sublime Forum

Sort lines, keep window position and remove duplicates

#1

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.
0 Likes

#2

You probably want to remove duplicates after sorting…

1 Like

#3

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?

0 Likes

#4

No, but maybe this will help you https://docs.sublimetext.io/reference/commands.html#discovering-commands

1 Like

#5

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

0 Likes