Sublime Forum

Auto-Indenting?

#1

Am I missing something or is there no current way to have auto-indentation? I’m just writing HTML and am trying to distance myself from TextMate that seems to have had some more convenience features :-/

Please let me know if I just haven’t found it and it’s in there somewhere already. Thanks for the help!

0 Likes

#2

ST does have that feature:

sublimetext.com/docs/indentation

I’m sorry I can’t think of a reason why it isn’t working for you off the top of my head…

0 Likes

#3

@guillermooo:

Is there automatic indentation in html files?

For me, auto indenting works in some languages, but not in html. And the documentation says that it only works for some languages.

Edan

0 Likes

#4

My understanding is that autoindentation should work by default in all scopes, since autoIndent defaults to true.

I think the docs mean that the built-in logic won’t apply the indentation you expect in all languages, but the feature per se should be functional.

I’ve just tried and it works for me in html, but I guess you might be expecting a smarter logic from it. In case it doesn’t work at all for you, have you maybe accidentally disabled autoindentation in your html file type preferences?

I believe you have some degree of control with regards to indentation using tmPreferences, but don’t quote me on this…

0 Likes

#5

What kind of indentation works for you in html?

I just tried a simple test:

[enter]

I expected the next line after the

to be indented.

Does this work for you? Or is there some other auto-indenting features which work in html?

Edan

0 Likes

#6

I see what you mean. Yeah, that won’t work.

But to prove my point, try

hi:

The next line should be indented automatically for you. Autoindentation is actually working, but it isn’t smart enough to do what you want. I’m not sure if this could be solved with a plugin, but I’d expect it could be…

Cheers

0 Likes

#7

You’re right.
That’s odd. Is the algorithm right now just to indent if the previous character is a colon/other special char?

One related thing I’d like to see:
When writing javascript, typing:
"function () { "
will autocomplete the closing bracket (on the same line).

Then, hitting enter will split the line up in a smart way, i.e.:
function() {
|
}

Where the cursor is properly indented.

What I’d really like to see is the same thing for HTML.

E.g., typing:

will automatically enter a
, then hitting enter will smartly split them up.

(Of course, it should allow overwriting of the in case someone doesn’t notice this happened and is blind typing).

0 Likes

#8

Then you need to start looking under the hood and tap into the enormous potential of ST! :wink: I’d expect over time all the bundles will be fleshed out, but someone has to start! :laughing:

Unfortunately I don’t have the time for that now myself.

Have you checked out the ZenCoding package? If you don’t know it yet, you might like it.

0 Likes

#9

One of the reasons I moved to SublimeText over vim/emacs was because I didn’t want to spend so much of my time tinkering with the editor. I like things to just work from the start :smile:
Not to mention that Sublime has no real central place to learn about all the various plugins and stuff.
I hope the direction Sublime is going is to try and integrate as many of the plugins into the default install as possible.

Yeah I know ZenCoding, but only just barely. Have to look into it more.

Edan

0 Likes

#10

Agreed. It’s a fair assumption. Particularly, improvements to “main stream packages” like HTML would have to be given priority.

0 Likes

#11

Yeah I’ve been trying to work out how to get auto indenting working properly for days now. I’ve been using the Web developer package found here: bitbucket.org/sublimator/webdeve … /wiki/Home

It would do the following:

// cursor === !
<body>
    <div>!</div> // hit return
</body>

// result
<body>
    <div>
!
    </div>
</body>

As you can hopefully see an extra line is inserted as it should be but the cursor doesn’t indent to the correct place. After trying a milllion fixes I tried downgrading to version 1.3 after reading that 1.4 has had modifications to it’s auto indent.

It now works perfectly so:

// cursor === !
<body>
    <div>!</div> // hit return
</body>

// result
<body>
    <div>
        !
    </div>
</body>

I thought it must have properly indented at some point as there wasn’t any posts around the net complaining…and, as a FED, it would stop me using Sublime!

I’d also like to point out that it was difficult to downgrade,I picked up an .exe from a pretty dubious source, perhaps it would be a good idea to offer downloads to older versions on this official site?

Anyway, hope this helps someone.

Rich

0 Likes

#12

That particular macro (indent-tag.sublime-macro) in the web developer package needs to be updated for 1.4, it currently looks like:

insertAndDecodeCharacters "\n\n"
move lines -1
indent "\t"

It’ll work if changed to:

insertAndDecodeCharacters "\n\n"
move lines -1
reindent
indent "\t"
1 Like

#13

Ahh, thanks for that, I’ve forked and updated the repo with that change, hopefully it gets pulled in by sublimator. Now I can use the latest version, cheers.

0 Likes

#14

I was also looking for how to do this in HTML. There’s a key binding for doing it in between {braces} by default in the Sublime Text 2 Default Key Bindings. I just modified the regexes there to work >between< angled brackets too. Not sure how to scope it just to HTML/XML, but in practice it shouldn’t be a problem. In my user key bindings file I added the modified version like:

	// other keys... ,

	{ "keys": "enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
		
			{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{|>$", "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}|<", "match_all": true }
		]
	}
]
0 Likes

#15

Hey!

Anyone got experience with auto indention when typing LaTeX in ST? I’m currently using TextMate, but there’s a couple of nice features in ST that would make me use ST instead. However, auto indention was back in the time one of my major motivations for leaving TexShop, and currently there’s no auto indention in ST when I’m typing in LaTeX…

Thanks =)

0 Likes

#16

Does not work for HTML. The only solution is to re-indent every time which is a bad coding experience.
Can you please at least suggest a package which will give me this feature in ST3? Many thanks!

0 Likes

#17

what build of ST you using? the built in HTML package does auto indent these days, can’t remember which build it was added in though, definitely 3156 has it

0 Likes

#18

Version 3, Build 3143.
It says no update available for my ST3… May be because I am using portable one?

0 Likes

#19

3156 is a build from the dev channel. You would have to install that in order to receive dev build updates.

However, this feature should work for your build too. Can you describe precise reproduction steps and what you expect the result to be and what it actually is?

1 Like

#20

I have found the solution, thanks for your help :slight_smile:

0 Likes