Sublime Forum

Macro construction help (turn word wrap off)

#1

I’m trying to create a macro that does the following (this is helpful in dumping large quantities of text into basic HTML when setting up a new web page):

  • Select all
  • Turn word wrap off
  • Split into lines
  • Insert

    at the beginning of each line, and

    at the end of each line

Here’s what I’ve got. The key challenge seems to be I can’t figure out how to set the macro to turn word wrap off before continuing with splitting into lines:

[
	{
		"args": null,
		"command": "select_all"
	},
	{
		"args": null,
		"command": "split_selection_into_lines"
	},
	{
		"args":
		{
			"extend": false,
			"to": "bol"
		},
		"command": "move_to"
	},
	{
		"args":
		{
			"characters": "<p>"
		},
		"command": "insert"
	},
	{
		"args":
		{
			"extend": false,
			"to": "eol"
		},
		"command": "move_to"
	},
	{
		"args":
		{
			"characters": "</p>"
		},
		"command": "insert"
	},
]

Any help appreciated! Thank you.

0 Likes

#2

You can toggle the current state of a boolean setting using the toggle_setting command, so to invert the state here you could use something like:

{
    "command": "toggle_setting",
    "args": {"setting": "word_wrap"}
}

If you want to explicitly set the state (i.e. you don’t know going in if it’s turned on or not) you can use the set_setting command to do this instead; you have to specify the setting and the value:

{
    "command": "set_setting",
    "args": {
        "setting": "word_wrap",
        "value": false
    }
}

That said, you can use hardeof and hardbol in the move_to command to argument to specify that you want to jump to the unwrapped beginning or end of a line that happens to be wrapped, so potentially you could use that in your macro instead of changing the setting.

Since in this case you want to wrap every line in paragraph tags, you can also leverage the insert_snippet command and pass it a contents argument that does the job of wrapping a line in a <p> tag with a single command.

[
    {
        "command": "select_all"
    },
    {
        "command": "split_selection_into_lines"
    },
    {
        "command": "insert_snippet",
        "args": {
            "contents": "<p>$SELECTION</p>"
        }
    },
    {
        "command": "single_selection"
    }
]
0 Likes

#3

That was exactly what I needed! Thank you, OdatNurd.

0 Likes