Sublime Forum

Snippet Help

#1

I have a snippet that currently takes a selected word and puts some text around it which works great.

To save a few key strokes what i want to do select the word via regex (a python variable at the start of the current line) copy it to a new line under the current line then put the same text around it as before.

Is this possible with a snippet or am i using the wrong tool?

Any help appreciated.

Thanks

Rob

0 Likes

#2

Ok think ive got it.

Created a macro which is bound to my hotkey with

{“keys”: [“clear”],
“command”: “run_macro_file”,
“args”: {“file”: “Packages/User/pythondebugnewline.sublime-macro”}

seems to work fine. Was just struggling to do it in a snippet.

Maybe this can help someone else trying to do something similar.

Regards
Rob

1 Like

#3

You nailed it; macros are definitely the way to go with something like this. In ST4 you can also use the chain command to chain multiple commands together, and the Chain of Command package for ST3 does the same thing as well.

In all cases though the solution boils down to executing multiple commands in a row. A snippet can only replace the selected text or insert new text; other manipulations require the backing of a plugin or manual intervention.

1 Like

#4

so as usual with these things speed one thing up find another bottleneck.

i decided it would be really cool to have another macro to clean up all the debug lines i have created with the above

however i cannot get the find and replace macro to work it does nothing, can you see where i have gone wrong? I think it may be to do with escaped characters as this works fine in find and replace but is cumbersome to type each time: ^.print(f"{.=}")\n

this is my removepythondebug.sublime-macro file contents:

[
{
“command”:“do_find_replace”,
“args”:{
“search”:"^.print\(f"{.=}"\)\n",
“replace”:""
}
}
]

as always any help appreciated i think im just missing something to do with the way it needs to be escaped in the macro.

Thanks
Rob

0 Likes

#5

Ok further help for anyone wanting to do the same.

I skipped too quickly through this great video https://www.youtube.com/watch?v=be4jVjZsnJk

I copied the do_find.py to my user folder and had to double escape the ( in my regex (solution below) and now i have a working solution.

I cannot express how much faster this makes my python debugging. i can use the clear key to output any variable in the form variable=value in the console and then clean up all the lines ive created with CMD+ALT+Clear.

Thank you very much @OdatNurd that was a great tutorial and the code sample saved me hours.

final macro contents:

[
{
"command":"do_find_replace",
"args":{
    "search":"^.*print\\(f\"{.*=}\"\\)\n",
    "replace":""
}
}
]

Kind regards
Rob

1 Like