Sublime Forum

ST3 Bug: Indenting moves in HTML

#1

I just ran across this issue on SuperUser (http://superuser.com/questions/641878/sublime-text-3-html-auto-indent-issue) and figured out a workaround, but I figured I’d post it here as well.

In HTML mode, open up a tag like a div, hit enter twice, close the div, then go back up a line and hit Tab to start entering another tag, like so (the | is the cursor):

<div class="someClass">
    |
</div>

As soon as you type a character (it can be any character, not just a < for opening a new tag), the indenting slides to the left and whatever you’re typing is now aligned with the

tags:
<div class="someClass">
<|
</div>

The original poster saw this on Win7 and Win8, and I confirmed it on OSX 10.8.4. There was a comment indicating someone did not see the behavior on Linux x64, but I can’t confirm that.

I did come up with a workaround, though. Open the tag like usual, then without hitting enter or anything, immediately close it. Position your cursor between the opening and closing tags:

<div class="someClass">|</div>

Then, hit Enter and your cursor is placed where it was in the first code block above. You can now type anything you want and the indenting stays put. However, if your cursor is at first indented:

<div class="someClass">
    |
</div>

and you hit backspace to delete the automatically-inserted tab, then manually insert it again, when you start typing the line will be de-indented back to the beginning, just like in the original problem.

Hopefully someone in the know can track this down and fix it.

Thanks for your time!
MattDMo

0 Likes

Sublime cancels my tab
Indent and tab key with html inside php document
Next Line Tab Undone As You Start Typing
#2

This is really annoying and also occurs when creating tags with the XML/long-tag snippet.

0 Likes

#3

+1

0 Likes

#4

+1

0 Likes

#5

It’s 2016 and this still exists.

I’ve gone bare bones - no plugins installed. If your content type is set to PHP (which it WILL be in an HTML document that uses PHP) it causes it.

Absolutely fed up with this.

0 Likes

#6

I’ve narrowed this down to being caused by the batchIncreaseIndentPattern in the Packages/HTML/Miscellaneous.tmPreferences file. Commenting it out prevents it from unindenting when typing the < character. No idea why, seeing as it is an indent pattern and not an unindent pattern…

In particular, it is the first alternation that is causing it:

^\s*
&lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
  ([A-Za-z0-9-]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
|

removing that stops it unindenting when typing <

2 Likes

Auto-indent from HTML/PHP on OSX not working
#7

Which version of ST3 did you get this from? This is from Build 3117:

<key>batchDecreaseIndentPattern</key>
<string>(?x)
^\s*
(&lt;/(?!html)
  [A-Za-z0-9-]+\b[^&gt;]*&gt;
|--&gt;
|&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
|\}
)
</string>
0 Likes

#8

3117. It’s the batchIncreaseIndentPattern - with emphasis on the increase, which is the odd thing.

0 Likes

#9

Whoops, didn’t read closely enough. So, if I understand you correctly, taking this:

&lt;(?!\?|area|...

and changing it to this

&lt;(?!area|...

should do the trick?

0 Likes

#10

sorry for not being clear. I’m saying, taking this:

<key>batchIncreaseIndentPattern</key>
<string>(?x)
    ^\s*
    &lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
      ([A-Za-z0-9-]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
    |&lt;!--(?!.*--&gt;)
    |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
    |\{[^}"']*$
</string>

and changing it to this, should do the trick:

<key>batchIncreaseIndentPattern</key>
<string>(?x)
    &lt;!--(?!.*--&gt;)
    |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
    |\{[^}"']*$
</string>

of course it may (should?) affect selecting an entire document and using the reindent command in perhaps undesired ways…

1 Like

#11

It is now 2017 and this issue still hasn’t been fixed. Is the developer even aware this is an issue that NEEDS to be fixed? It is currently my number 1 annoyance.

0 Likes

#12

yep, the ST devs have taken a look and have a fix in mind:

0 Likes

#13

writing HTML in ST build 3128 no longer causes the caret to move back an indentation level unexpectedly :slight_smile:

1 Like