Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple sublime text 2 multiple cursor alignment command
import sublime, sublime_plugin, re
class AlignCursors(sublime_plugin.TextCommand):
def adjust_column_for_tabs(self, view, tab_size, region, rowcol):
line_text = view.substr(view.line(region))[:rowcol[1]]
return rowcol[1] + (line_text.count("\t") * (tab_size - 1))
def run(self, edit):
view = self.view
regions = view.sel()
right_most = 0
cursors = 0
tab_size = int(view.settings().get('tab_size', 8))
lines = set()
if len(regions) < 2:
sublime.error_message("Error: Must have more than one cursor active")
for region in regions:
if not region.empty():
sublime.error_message("Error: Must have no content selected")
return
rowcol = view.rowcol(region.begin())
if rowcol[0] in lines:
sublime.error_message("Error: Cannot have more than one cursor per line (line %i has more)" % (rowcol[0] + 1))
return
lines = lines | set([rowcol[0]])
col = self.adjust_column_for_tabs(view, tab_size, region, rowcol)
if col > right_most:
right_most = col
for region in regions:
rowcol = view.rowcol(region.begin())
col = self.adjust_column_for_tabs(view, tab_size, region, rowcol)
if col < right_most:
view.insert(edit, region.begin(), ''.join([' '] * (right_most - rowcol[1])))
[
{ "keys": ["ctrl+alt+]"], "command": "align_cursors" }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.