Sublime Forum

Incorrect auto-indentation for bash if/then

#1

In a bash script, when you type if [ cond ] and press Enter, Sublime 3 (build 3126) puts the cursor on the next line one level deeper than the if. As you continue typing then the cursor does not indent back. If you manually indent back before typing then then the editor will indent back after you type else or fi (which will make else or fi appear one level higher than the corresponding if). It will also not add one level of indentation after you type then.

So basically, you get this kind of code if you don’t manually readjust indentation:

if [ cond ]
    then
    echo True
else
    echo False
fi

While I would like the code automatically look like this:

if [ cond ]
then
    echo True
else
    echo False
fi
0 Likes

#2

try editing https://github.com/sublimehq/Packages/blob/61bfd68ea07a3aede68cd6a65c80be702b3d6db8/ShellScript/Miscellaneous.tmPreferences with https://packagecontrol.io/packages/PackageResourceViewer and replacing the whole contents of the file with this:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>Miscellaneous</string>
	<key>scope</key>
	<string>source.shell</string>
	<key>settings</key>
	<dict>
		<key>decreaseIndentPattern</key>
		<string>^\s*(\}|(elif|else|fi|esac|done)\b)</string>
		<key>increaseIndentPattern</key>
		<string>^\s*(elif|else|case)\b|^.*(\{|\b(do)\b)$|^.*\bthen\b\s*$</string>
		<key>disableIndentNextLinePattern</key>
		<string>^\s*then\s*$</string>
		<key>bracketIndentNextLinePattern</key>
		<string>^\s*if\b.*(?!\bthen\b)$</string>
	</dict>
</dict>
</plist>
4 Likes

#3

Yes, this works. Thank you.

Will it make it into the official Sublime?

0 Likes

#4

It will if somebody creates a PR to that GitHub repo (and it gets accepted/merged) :wink:

0 Likes

#5

Done: https://github.com/sublimehq/Packages/pull/1096

Thanks again.

2 Likes