Sublime Forum

Sublime loses indentation when I type (with video)

#1

Hey guys, perhaps you could give me a hand with something that’s driving me mad.

It’s pretty silly actually, but in a whole day programming, it turns into a big issue - like that greek myth where the guy is trapped with an ocasional but constant drop of water dripping in is head - it ends up driving him insane, worst than any medieval pain.

Anyway, lol, here’s what’s happening:

0 Likes

#2
1 Like

#3

Thanks, I’ll leave a full explanation here for future searches.

How to fix this, as of 13/jan/2017:

  1. Go to C:/Program Files/Sublime/Packages
  2. Open HTML.sublime-package as if it were a zip
  3. Unzip it in some temporary folder and open Miscellaneous.tmPreferences
  4. Search for:
    <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 replace with:
    <key>batchIncreaseIndentPattern</key> <string>(?x) &lt;!--(?!.*--&gt;) |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1) |\{[^}"']*$ </string>
  5. Save the file, zip it again and rename it to HTML.sublime-package and replace the original file
  6. Done.

Credits for the fix @kingkeith

0 Likes

#4

As a note, if you modify or replace the sublime-package file that ships with sublime, it will get reverted back to the default file again the next time that Sublime updates itself or if you re-install sublime.

To get around that, you can override only that specific file. The easiest way to do that would be to use PackageResourceViewer. You can select Package Resource Viewer: Open Resource from the command palette, select HTML and then Miscellaneous.tmPreferences to open the file, and then make your changes and hit save.

That will create an override for only that particular file, where the changes will remain in effect even if/when Sublime updates or gets reinstalled.

Note that this also means that your override remains in effect even if the base version of the package modifies it.

1 Like

#5

Uhm, I didn’t really get your problem, but you better do div.myClass and then tab than typing everything

0 Likes

#6

If you hit tab from position 0, with the cursor at the correct indention spot, and then start typing, it will un-indent and revert back to be aligned with the prior line above it.

0 Likes

#7

Does this fix work for PHP as well? I’m not seeing something that looks identical sitting in the preferences for php.

However, there is a folder called indention rules…

<dict>
	<key>decreaseIndentPattern</key>
	<string>(?x) ^ (.*\*/)? \s* \} .* $|&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))</string>
	<key>indentNextLinePattern</key>
	<string>^(?!.*(#|//|\*/|&lt;\?))(?!.*[};:]\s*(//|/\*.*\*/\s*$)).*[^\s;:{}]\s*$|&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)</string>

	<key>bracketIndentNextLinePattern</key>
	<string>(?x)
	^ \s* \b(if|while|else|elseif|foreach)\b [^;]* $
	| ^ \s* \b(for)\b .* $
	</string>
</dict>

Might be what we’re looking for?

0 Likes

#8

Does this fix work for PHP as well? I’m not seeing something that looks identical sitting in the preferences for php.

I use PHP and it fixed for me, did you find the file Miscellaneous.tmPreferences?

0 Likes

#9

Cool. I do want to note though, that if you are at position 0, and hit tab, it only increases the indent by one tab, instead of snapping to the spot in which would be the correct position to be at.

This is a good temp fix, but not the long term fix we need.

Thanks for providing this though :slight_smile:

0 Likes

#10

Ah yes, that is true indeed. You have to press Tab two times to get the correct indentation with this fix.

You know a workaround for this @kingkeith? I don’t know much of what’s going on in that code.

0 Likes

#11

if you open the ST console (View -> Show Console) and type sublime.log_commands(True) Enter, and then press Tab at column 0 in your HTML, what is displayed in the console?

command: reindent or something different?

does it make a difference (to the indentation or the command executed when pressing Tab) whether the line above is:

  • an opening tag
  • a closing tag
  • only whitespace

?

0 Likes

#12

command: reindent is what shows at console

I don’t know exactly how to answer that, so I made a video:

0 Likes

#13

it could be because you have indented <main…, and if you were to select the whole file and do a Line -> Reindent, it would unindent <main… i.e. it is ignoring your actual indentation levels and suggesting what it thinks the indentation level should be, based on it’s rules. just a guess.

0 Likes