Sublime Forum

"Wrap selection with tag" for multiple selections not working in macro

#1

I’m recording some basic macro to wrap each line with <li> tag, and the macro looks like this:

[
	{
		"args": null,
		"command": "select_all"
	},
	{
		"args": null,
		"command": "split_selection_into_lines"
	},
	{
		"args":
		{
			"name": "Packages/XML/Snippets/long-tag.sublime-snippet"
		},
		"command": "insert_snippet"
	},
	{
		"args":
		{
			"characters": "li"
		},
		"command": "insert"
	},
	{
		"args": null,
		"command": "clear_fields"
	},
	{
		"args": null,
		"command": "single_selection"
	}
]

However this does not work as expected, leaving each line a trailing </p> rather than a </li>.
This only works when I’m not running the split_selection_into_lines command, which is not what I want.

0 Likes

#2

If you run each of these commands one by one manually, it works as expected, but in macro form it doesn’t. Some experimentation shows that the clear_fields command seems to be what’s messing it up. If you remove that from the macro, it works as intended (except you have to press Esc manually, which is a drag).

The most expedient solution would be to replace the snippet being used to one that doesn’t contain multiple fields (since you don’t need them), which would obviate the need for the clear_fields command at all. You can also inline a snippet by using the contents argument to the insert_snippet command.

That might look something like this as a final macro:

[
    {
        "command": "select_all"
    },
    {
        "command": "split_selection_into_lines"
    },
    {
        "args":
        {
            "contents": "<li>$SELECTION</li>",
        },
        "command": "insert_snippet"
    },
    {
        "command": "single_selection"
    }
]
1 Like

#3

Thanks, this helped a lot, and I now have several more ways to tackle this problem. :grinning:

1 Like