Sublime Forum

Macro in Sublime Text - specific line number

#1

Hi,

I’m trying to record a macro that will add a specific line with text under line 95.
But the macro I recorded is just adding this new line as line number 1.
How do I tell the macro to add this new line after line 95?

The text in the new line I’m adding is: “cp.movie.play();”
Here’s the macro I recorded:

[
	{
		"args":
		{
			"characters": "\ncp."
		},
		"command": "insert"
	},
	{
		"args":
		{
			"characters": "movie."
		},
		"command": "insert"
	},
	{
		"args":
		{
			"characters": "play"
		},
		"command": "insert"
	},
	{
		"args":
		{
			"contents": "($0)"
		},
		"command": "insert_snippet"
	},
	{
		"args":
		{
			"by": "characters",
			"forward": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"characters": ";"
		},
		"command": "insert"
	}
]
0 Likes

#2

Some caveats, I’m using ST3, I’m reasonably sure there’s no difference in ‘macro language’ between ST3 & 4.

I’m also reasonably sure there isn’t a loop function in macro language so you’ll have to

	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"

95 times before you insert your code, if you start at the top of the file, but I’m getting ahead of myself.
The ‘macro code’ below moves to the top of the file, then moves down 10 lines (not 95) and then inserts some minus signs and a new line.

It might be easier to record parts of your snippet using the macro record function and then save it and then copy and paste that code into a larger bespoke macro.
As for a macro that inserts a snippet, I’m not sure but here’s a previous post on the subject which has links to some useful info concerning snippets and macros.

Macro to move to beginning of file, move down 10 lines and insert some minus signs

[
{
	"args":
	{
		"extend": false,
		"to": "bof"
	},
	"command": "move_to"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"by": "lines",
		"forward": true
	},
	"command": "move"
},
{
	"args":
	{
		"extend": false,
		"to": "eol"
	},
	"command": "move_to"
},
{
	"args":
	{
		"characters": "\n"
	},
	"command": "insert"
},
{
	"args":
	{
		"extend": false,
		"to": "bol"
	},
	"command": "move_to"
},
{
	"args":
	{
		"characters": "-----------------------------------"
	},
	"command": "insert"
},
{
	"args":
	{
		"characters": "\n"
	},
	"command": "insert"
}

]

I appreciate this isn’t an answer to your question, but might be enough to get you started and might help to get this post answered with a better answer than mine…

:rabbit::hocho:

0 Likes

#3

If you want your macro to jump to a specific line, you can use the goto_line command for that:

{
    "command": "goto_line",
    "args": {"line": 95}
}

There is also a command, prompt_goto_line that will ask you to enter the line and then jump there, but that particular command cannot be captured in a macro because it’s specific to the Window and not to the current file.

0 Likes