Sublime Forum

Inserting Snippet

#1

Hi all,

Can anyone tell me how (or maybe I should say ‘if’) I can ensure that when I insert a snippet in my code it always gets placed at character position 0 on the line? If I’m on the first typed character on the line, and I hit ‘Home’ the cursor goes to position 0. In the opposite fashion, if I’m on position 0 and I hit ‘Home’ it goes to the first character. What I want to do is always insert my snippet at character position 0. And I want it to be automatically placed there without any additional keystrokes. Any ideas?

0 Likes

#2
view.run_command("move_to", {"to": "hardbol"})

Just use that in a macro and then the “insert_snippet” command.

0 Likes

#3

Thanks again FichteFoll. You are a great fount of knowledge I must say!! :smiley:

So I’ve followed your advice and it almost works :angry: . My macro now looks like this:

{"command": "set_setting", "args": {"setting": "auto_indent", "value": false} }, {"command": "move_to", "args": {"to": "hardbol"} }, {"command": "insert_snippet", "args": {"name": "Packages/User/mysnippet.sublime-snippet"} }, {"command": "set_setting", "args": {"setting": "auto_indent", "value": true} }, ]

The theory is that by setting auto-indent to false, then moving to ‘hardbol’ the (multi-line) text I insert will be inserted at character position 0, and line up there correctly. However, the auto-indent seems to be ignored from within the snippet. So I get a translation from

if (some condition) { a line of code another line of code

to

[code] if (some condition)
{

    my snippet line 1
    my snippet line 2
    my snippet line 3

			a line of code
    another line of code[/code]

whereas what I want is

[code]if (some condition)
    {

my snippet line 1
my snippet line 2
my snippet line 3

    a line of code
    another line of code[/code]

If I insert the lines manually with auto-indent off, it works fine. Any suggestions?

0 Likes

#4

{"command": "move_to", "args": {"to": "hardbol", "extend": true} }, {"command": "right_delete" }, {"command": "insert_snippet", "args": {"name": "Packages/User/mysnippet.sublime-snippet"} }, ]

like this maybe?

0 Likes