A plugin for sorting lines of columnar data based on whichever column the user has selected with the cursor. Basically you highlight all the rows you want to sort, and then place the end cursor on the column to use as a key. Mostly just a re-implementation of the sort command line tool, but I find it comes in handy enough for things like sorting import *, etc.

I didn't see any mentions when I did a search for sort columns, so I hope this isn't reinventing the wheel.

# get region of word under cursor and that line wordselection = self.view.word(selection.end()) last_line_sel = self.view.line(selection.end())

# Get str of the beginning of last line up to cursor line_start = self.view.substr(sublime.Region(last_line_sel.begin(), wordselection.end())) # column that the end cursor is positioned at colkey = len(line_start.split(" ")) - 1

# position of the start of the line at the beginning of selection startp = self.view.line(selection.begin()).begin()

# create array of all lines in the selection region sortarray = [] for linesel in self.view.lines(selection): sortarray.append(self.view.substr(linesel).split(' '))

# erase the lines from the window so we can place in proper order for linesel in reversed(self.view.lines(selection)): self.view.erase(edit, self.view.full_line(linesel))

# sort then append the list together as a single string with \n sortarray.sort(key=colsort) sorted_str = "" for line in sortarray: sorted_str += " ".join(line) + "\n" # Need space between cols