Skip to content

Instantly share code, notes, and snippets.

@kemayo
Last active September 21, 2016 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kemayo/5131191 to your computer and use it in GitHub Desktop.
Save kemayo/5131191 to your computer and use it in GitHub Desktop.
Tiny sublime plugin to wrap a selection in something.
[
{ "keys": ["alt+p"], "command": "wrap_text", "args": {"start":"<? ", "end":" ?>" }},
]
import sublime, sublime_plugin
class WrapTextCommand(sublime_plugin.TextCommand):
def run(self, edit, start="", end="", select_mode='contents'):
regions = []
for region in self.view.sel():
text = self.view.substr(region)
self.view.replace(edit, region, "%s%s%s" % (start, text, end))
if select_mode == 'contents':
regions.append(sublime.Region(region.begin() + len(start), region.begin()+len(start)+len(text)))
elif select_mode == 'wrappers':
regions.append(sublime.Region(region.begin(), region.begin() + len(start)))
regions.append(sublime.Region(region.begin() + len(start) + len(text), region.begin() + len(start) + len(text) + len(end)))
elif select_mode == 'tags':
# Assumes the start/end are html tags, and you want to select the "tag part"
regions.append(sublime.Region(region.begin() + 1, region.begin() + len(start) - 1))
regions.append(sublime.Region(region.begin() + len(start) + len(text) + 2, region.begin() + len(start) + len(text) + len(end) - 1))
if not select_mode or select_mode == 'all':
return
self.view.sel().clear()
for region in regions:
self.view.sel().add(region)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment