Sublime Forum

Auto indentation not working correctly for an else block in python written using a snippet

#1

I am using sublime text 3. There is no auto complete suggestion for else and elif in python.

So I started writing a snippet for else first.

<snippet>
    <tabTrigger>else</tabTrigger>
    <scope>source.python</scope>
    <description>Else Condition</description>
    <content><![CDATA[
else:
    ${1}
]]></content>
</snippet>

Generally when I type else I will be inside if block.

when I use the snippet I wrote, it is indenting improperly as shown in the first half of the below image.

But I want it to indent correctly as shown in the second half of the above image.

I tried searching in sublime forums and else where on the internet but could not find a similar issue like this.
How can I write a snippet so that the else block indents correctly?

0 Likes

#2

I suspect that multi-line snippets don’t obey the auto indentation system very well - changing the snippet content to <![CDATA[else:]]> (i.e. a single line) ensures that it unindents as expected when typing el Tab, but I suspect it’s not very helpful for saving time. There have also been reports where the indentation increases too much for some snippets:

0 Likes

#3

Sir,
Is there a possibility to use in-built sublime commands like unindent inside snippets?

Also, can you please tell me why there are no in-built autocomplete suggestions for else and elif in python?

0 Likes

#4

nope

I can only speculate, but I guess because of this very issue relating to indentation? you could always create a snippet for if...else to workaround it:

<snippet>
	<tabTrigger>if</tabTrigger>
	<scope>source.python</scope>
	<description>If Condition</description>
	<content><![CDATA[
if ${1:$SELECTION}:
	${2:pass}
else:
	$3
$0
]]></content>
</snippet>
0 Likes

#5

Thank You very much sir. That solves my problem.

0 Likes