Sublime Forum

Need a text surround plugin/solution

#1

Hi, first time here :wink:

Im user of Notepad++ for long time and i set it like that when i select a certain text or paragraph or text and press ALT+P (for instance) that it wraps selection in

tags. Same goes for , and much more.

IS there any equivalent of such thing in Sublime, i need it real bad?

Thanks

0 Likes

#2

That can easily be done with a plugin. Select Tools->New Plugin and save this:

import sublime, sublime_plugin

class WrapTextCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = ]
        for region in self.view.sel():
            region = sublime.Region(region.begin(), region.end())
            text = self.view.substr(region)
            self.view.replace(edit, region, "<tag>%s</tag>" % text)
            regions.append(sublime.Region(region.begin()+1, region.begin()+4))
            regions.append(sublime.Region(region.end()+11-4, region.end()+11-1))
        self.view.sel().clear()
        for region in regions:
            self.view.sel().add(region)

Then create a keybinding in your user keybindings:

    { "keys": "alt+p"], "command": "wrap_text"},
0 Likes

#3

Thank you quarnster,

this one makes the switch from gvim to ST2 even easier!

Regards,
Highend

0 Likes

#4

wow, thanks quarnster. Thats almost what i needed!

I wouldnā€™t want to be rude but can i define ALT+I for , ALT+S for , CTRL+B for etcā€¦ to auto wrap the text instead of giving me the i need to change to what ever the tag i need?

You know, like when youā€™re editing text in some rich text editor and it bold the text on CTRL+B for example?

Thanks a ton, you rock!

0 Likes

#5

Sure, no problem.

Hereā€™s the updated plugin code:

[code]import sublime, sublime_plugin

class WrapTextCommand(sublime_plugin.TextCommand):
def run(self, edit, tag=ā€œtagā€):
regions = ]
for region in self.view.sel():
text = self.view.substr(region)
self.view.replace(edit, region, ā€œ<%s>%s</%s>ā€ % (tag, text, tag))
off = 1
regions.append(sublime.Region(region.begin()+off, region.begin()+off+len(tag)))
off += len(tag)+3
regions.append(sublime.Region(region.end()+off, region.end()+off+len(tag)))
self.view.sel().clear()
for region in regions:
self.view.sel().add(region)
[/code]

And then you can add keybindings such as this:

{ "keys": "alt+p"], "command": "wrap_text"}, { "keys": "ctrl+b"], "command": "wrap_text", "args": {"tag": "b"}}, { "keys": "alt+i"], "command": "wrap_text", "args": {"tag": "img"}},

0 Likes

#6

Great. Sir, would you be interested in making this a official plugin?

I have more ideas for development of this one but im kinda ashamed of asking for more as you already did more than i expected.

Thanks for that!

0 Likes

#7

Sure, Iā€™ve set up a github repository for the plugin at github.com/quarnster/WrapText, please open up issues there for feature requests to make sure I see them

See github.com/quarnster/WrapText#setup for installation instructions. You probably have to delete the old plugin code.

0 Likes

#8

I think this is already built in with sublime: ctrl+shift+w

0 Likes

#9

Thatā€™s a good point weslly. The current snippet doesnā€™t do exactly what pentago wants, but using snippets provides for a much better solution to the problem.

Iā€™m probably going to delete the plugin soonish, but for anyone ending up here looking for a similar feature to Notepad++'s WebEdit plugin, here is a piece of python code to convert WebEdit commands to Sublime Text 2 key bindings + snippets.

0 Likes

#10

YES! Completelly satisfied, this is exactly what i wanted.
Itā€™ll be a pleasure developing with ST2.

Thanks guys, such amount of will to help is rarely seen in other communities.

Hats off.

Take care,
P.

0 Likes