Skip to content

Instantly share code, notes, and snippets.

@Westacular
Created December 2, 2012 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Westacular/4187126 to your computer and use it in GitHub Desktop.
Save Westacular/4187126 to your computer and use it in GitHub Desktop.
An alternative to Sublime Text 2's built-in join_lines command, which contains an annoying ideosyncracy of reaching outside the selected region when performing a join.
# -*- coding: UTF-8 -*-
import sublime_plugin
# An alternative to the builtin "join_lines" command,
# which contains an idiosyncracy that I consider a bug.
class JoinLinesAltCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
# First, join the lines in each selection together.
# (Taking care not to join empty lines.)
#
# Using view.sel()[] directly because it gets updated as we make replacements,
# whereas the region objects returned by it do not.
join_regex = r"(?<=\S)[ \t]*\n[ \t]*(?=\S)"
for c in xrange(len(view.sel())):
# If the selection is just a caret,
# assume the user wants to join the following line.
if len(view.sel()[c]) == 0:
line = view.full_line(view.sel()[c].begin())
m = view.find(join_regex, line.begin())
if line.intersects(m):
view.replace(edit, m, " ")
# If the selection contains something,
# only perform joins INSIDE the selected region
else:
m = view.find(join_regex, view.line(view.sel()[c].begin()).begin())
while m and view.sel()[c].intersects(m):
view.replace(edit, m, " ")
m = view.find(join_regex, m.begin())
def is_enabled(self):
return self.view.sel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment