Home Download Buy Blog Forum Support

Need a text surround plugin/solution

Need a text surround plugin/solution

Postby pentago on Wed Sep 26, 2012 8:45 am

Hi, first time here ;)

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 <p></p> tags. Same goes for <style>, <img> and much more.

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

Thanks
pentago
 
Posts: 11
Joined: Wed Sep 26, 2012 8:41 am

Re: Need a text surround plugin/solution

Postby quarnster on Wed Sep 26, 2012 9:04 am

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

Code: Select all
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:
Code: Select all
    { "keys": ["alt+p"], "command": "wrap_text"},
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Need a text surround plugin/solution

Postby highend on Wed Sep 26, 2012 9:25 am

Thank you quarnster,

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

Regards,
Highend
highend
 
Posts: 94
Joined: Fri Jan 20, 2012 2:47 pm

Re: Need a text surround plugin/solution

Postby pentago on Wed Sep 26, 2012 9:50 am

wow, thanks quarnster. Thats almost what i needed!

I wouldn't want to be rude but can i define ALT+I for <img>, ALT+S for <style>, CTRL+B for <strong> etc.. to auto wrap the text instead of giving me the <tag> 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!
pentago
 
Posts: 11
Joined: Wed Sep 26, 2012 8:41 am

Re: Need a text surround plugin/solution

Postby quarnster on Wed Sep 26, 2012 10:02 am

Sure, no problem.

Here's the updated plugin code:
Code: Select all
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)


And then you can add keybindings such as this:

Code: Select all
    { "keys": ["alt+p"], "command": "wrap_text"},
    { "keys": ["ctrl+b"], "command": "wrap_text", "args": {"tag": "b"}},
    { "keys": ["alt+i"], "command": "wrap_text", "args": {"tag": "img"}},
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Need a text surround plugin/solution

Postby pentago on Wed Sep 26, 2012 6:32 pm

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!
pentago
 
Posts: 11
Joined: Wed Sep 26, 2012 8:41 am

Re: Need a text surround plugin/solution

Postby quarnster on Thu Sep 27, 2012 9:01 am

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

See https://github.com/quarnster/WrapText#setup for installation instructions. You probably have to delete the old plugin code.
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Need a text surround plugin/solution

Postby weslly on Thu Sep 27, 2012 1:02 pm

I think this is already built in with sublime: ctrl+shift+w
weslly
 
Posts: 203
Joined: Sun Aug 28, 2011 3:57 am
Location: Brazil

Re: Need a text surround plugin/solution

Postby quarnster on Thu Sep 27, 2012 7:11 pm

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.
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Need a text surround plugin/solution

Postby pentago on Thu Sep 27, 2012 10:48 pm

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.
pentago
 
Posts: 11
Joined: Wed Sep 26, 2012 8:41 am


Return to General Discussion

Who is online

Users browsing this forum: Majestic-12 [Bot], Virsalus, Yahoo [Bot] and 19 guests