Home Download Buy Blog Forum Support

Insert Open/Close Tag (With Current Word)

Insert Open/Close Tag (With Current Word)

Postby Perceptes on Sat Oct 29, 2011 11:14 am

I use this HTML key mapping constantly in TextMate (control-less-than) and it's sorely missed. Is there a way to implement this functionality via custom key mappings in ST2?
Perceptes
 
Posts: 9
Joined: Thu Sep 22, 2011 7:50 pm

Re: Insert Open/Close Tag (With Current Word)

Postby Perceptes on Thu Dec 29, 2011 10:00 am

Still hoping someone knows a solution for this.
Perceptes
 
Posts: 9
Joined: Thu Sep 22, 2011 7:50 pm

Re: Insert Open/Close Tag (With Current Word)

Postby C0D312 on Thu Dec 29, 2011 1:33 pm

If you could better explain the behavior of "Insert Open/Close Tag (With Current Word)", I might be able to help. I'm not a textmate user, so I'm unfamiliar with the command.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Insert Open/Close Tag (With Current Word)

Postby senzo on Thu Dec 29, 2011 4:16 pm

Code: Select all
word
becomes
Code: Select all
<word></word>
senzo
 
Posts: 106
Joined: Tue May 24, 2011 9:17 am

Re: Insert Open/Close Tag (With Current Word)

Postby senzo on Thu Dec 29, 2011 4:52 pm

Create a file InsertAsTag.py in your Users folder with this content:
Code: Select all
import sublime
import sublime_plugin


class InsertAsTagCommand(sublime_plugin.TextCommand):

    def run(self, edit):

        for region in self.view.sel():
            word_reg = self.view.word(region)
            word = self.view.substr(word_reg)
            s = "<%s></%s>" % (word, word)
            self.view.replace(edit, word_reg, s)


and ad this to your User-Key-Bindings:
Code: Select all
{ "keys": ["ctrl+<"], "command": "insert_as_tag"}
senzo
 
Posts: 106
Joined: Tue May 24, 2011 9:17 am

Re: Insert Open/Close Tag (With Current Word)

Postby Cottser on Thu Dec 29, 2011 5:02 pm

Thanks! I only got partway there using a snippet, because the original text would remain and I couldn't figure out how to remove it. I was using the following:
Code: Select all
<$TM_CURRENT_WORD>$1</$TM_CURRENT_WORD>


The only thing I had to do differently to get the command working was to use the following in my keybindings. I'm on OS X if that makes a difference.
Code: Select all
{ "keys": ["ctrl+shift+,"], "command": "insert_as_tag"}
Cottser
 
Posts: 22
Joined: Sat Oct 08, 2011 12:24 am

Re: Insert Open/Close Tag (With Current Word)

Postby Perceptes on Fri Dec 30, 2011 6:58 am

Awesome! Thanks very much! :D

Edit: Could it be edited so that the cursor ends up between the tags afterward?

Edit 2: Here is the implementation of this command in TextMate: https://gist.github.com/1539035
Perceptes
 
Posts: 9
Joined: Thu Sep 22, 2011 7:50 pm

Re: Insert Open/Close Tag (With Current Word)

Postby senzo on Fri Dec 30, 2011 2:49 pm

This should do it
Code: Select all
import sublime
import sublime_plugin


class InsertAsTagCommand(sublime_plugin.TextCommand):

    def run(self, edit):

        for region in self.view.sel():
            self.view.sel().clear()
            word_reg = self.view.word(region)
            word = self.view.substr(word_reg)
            s = "<%s></%s>" % (word, word)
            self.view.replace(edit, word_reg, s)
            self.view.sel().clear()
            if region.a < region.b:
                  self.view.sel().add(sublime.Region(region.b + 2))
            else:
                  self.view.sel().add(sublime.Region(region.a + 2))
            self.view.show(self.view.sel())


However, have you installed the zenCoding plugin? It makes this stuff pretty much obsolete.

Edit: It also works if the cursor is at the end of the word, it doesn't have to be selected.
senzo
 
Posts: 106
Joined: Tue May 24, 2011 9:17 am

Re: Insert Open/Close Tag (With Current Word)

Postby Perceptes on Fri Dec 30, 2011 10:22 pm

Thanks again! I modified it to default to a paragraph tag if there's no current word. https://github.com/jimmycuadra/sublime- ... rtAsTag.py
Perceptes
 
Posts: 9
Joined: Thu Sep 22, 2011 7:50 pm

Re: Insert Open/Close Tag (With Current Word)

Postby tito on Fri Dec 30, 2011 10:46 pm

This is the type of command that are very welcome to become part of the Tag Package.
https://github.com/SublimeText/Tag "Collection of packages about HTML/XML tags."

Feel free to submit or request the addition.

Regards,
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 878
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Next

Return to General Discussion

Who is online

Users browsing this forum: VladoBrigant, Yahoo [Bot] and 23 guests