Sublime Forum

Need help creating a macro

#1

I want to create a macro which adds html tags around the selected text.

In my dreams it should work in this way:

  1. I select some text (i.e. “This will be inside a tag” )
  2. I launch my macro (either by menu or hot-key)
  3. I input the tag i need (i.e. “span”) and press enter
  4. I obtain “This will be inside a tag

I tried to google for that, but the terms are so much used that i’ve only found an example in this forum which i don’t understand much.
Also i landed on the Unofficial documentation but i can’t find any command reference.

Could someone show me how a macro like that is done?

0 Likes

#2

There’s an existing macro for that. Look in the Edit menu -> Tag -> Wrap selection with tag. To see how it is implemented, open the Command Palette:

  • View Package File
  • Default/Main.sublime-menu
  • Search for Wrap and find it is a snippet in the XML package which can be inspected the same way
0 Likes

#3

Thanks. I never looked in that Edit->Tag menu…
I see how it works and i find two “problems”:

  • it messes the indentation if i select multiple lines
  • I was hoping to retain my selection (so i could keep adding tags inside tags), this command instead “selects” the two added tags to edit them togheter.

Do you think that a macro build differently could achieve these result?

So, the code is:

<snippet>
    <content><![CDATA[<${1:p}>${0:$SELECTION}</${1/([^ ]+).*/$1/}>]]></content>
    <tabTrigger>lt</tabTrigger>
    <scope>text.xml - meta.tag - comment - string</scope>
    <description>Long Tag</description>
</snippet>

Could you explain to me what the content line means?

0 Likes

#4

the documentation will explain it better than I can :slight_smile: basically it is marking places for field placeholders and performing regex substitutions
https://docs.sublimetext.io/guide/extensibility/snippets.html#snippet-features

I’m pretty sure you would need a Python plugin to show an input panel if you don’t want to achieve it via multi selection

0 Likes

#5

OH!
Yesterday i landed at docs.sublimetext.info which is clearly incomplete, i didn’t know about docs.sublimetext.io.
I’ll try to learn from the docs, now. Thanks.

0 Likes