Sublime Forum

Designate final cursor position in snippet

#1

I’m writing a few snippets to use in Ruby on Rails Erb views. For example:

[code]
<![CDATA[<% if $1 %>

<% else %>

<% end %>
]]>
-if
text.html.ruby
<% if %><% else %><% end %>
[/code]
Here I want to be able to fill out the if condition, then hit tab and have the cursor move inside the if block. Ideally, I’d like be able to trigger another snippet here, but the current snippet is still active so I can’t. Failing that, I’d like to just leave the cursor inside the if block and end the snippet block.

What I want to do:
-if
(fill out the if statement)
(inside the if block. this snippet is over; now I can trigger other snippets)

What currently happens:
-if
(fill out the if statement)
(jump to after the end tag. move the cursor back up to the if block manually.)

I guess I can just make three snippets. This seemed more convenient.

0 Likes

#2

Not sure that I fully understand, but… you can just add ${2}, ${3} at the positions where you wish to tab to, and (generally) add $0 at the end of the line as a finishing point. You can use ${2:sometext} if you want a text place-holder/prompt.

To allow one snippet to be used within another, I set

"auto_complete_commit_on_tab": true, "auto_complete_with_fields": false,
in File Settings (Syntax Specific), and

[code]
{ “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: true} }, // deleted “\t”
{ “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: false}, // deleted “\t”
“context”:

		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
},
{ "keys": "ctrl+alt+right"], "command": "replace_completion_with_next_completion", "context":
	
		{ "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
},
{ "keys": "ctrl+alt+left"], "command": "replace_completion_with_prev_completion", "context":
	
		{ "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
		{ "key": "setting.tab_completion", "operator": "equal", "operand": true }
	]
}

][/code]
in Key-Bindings (User). This allows me to tab between fields, but use Ctrl-Space (perhaps) and Ctrl-Alt_Left and Right for the other/nested snippet or completion.

0 Likes

#3

For more info on snippets, check out the unofficial docs: readthedocs.org/docs/sublime-tex … ppets.html

0 Likes