Sublime Forum

Indent to brackets with square brackets and curly braces?

#1

Is there any way for the indent_to_brackets setting to indent to square brackets and curly braces in addition to parentheses? This is a feature I very much want, especially when I’m writing with a language like Python that makes frequent use of both square brackets and curly braces.

2 Likes

indent_to_bracket {} [] () python
Python indent_to_bracket improvement
#2

Just noticed Build 3103 was released yesterday. It lists “Improved indenting behavior when indent_to_bracket is enabled” in the changelog but out of the box I’m still getting the same issue was square brackets and curly braces not being considered when indenting.

Is there any way to enable the behavior I want in this new build?

0 Likes

#3

No, this functionality has not been added.

I think this should be handled by a plugin tbh (easier to configure) and I also have a few plans for such, since the current behavior does not align with my style guidelines, but I never really started with it except for drafting a few test cases.

0 Likes

#4

If it’s possible to get this behavior out of a plugin I would be happy to develop it, as I constantly get annoyed by this.

Looking over the scarce API docs, I’m not sure what the correct approach is. Would I…

  1. make a TextCommand that creates a new line and indents to the current open bracket or brace
  2. is only enabled when there is an open bracket or brace
  3. bind it to the return / enter key?

If not, what’s the correct approach?

However, even better would be if there’s some way to modify the syntax definition for my preferred languages to allow for this kind of indentation. Because my guess is that if that’s possible then other indentation related functions will start working too, like reindent.

0 Likes

#5

Yes, pretty much like that. You have to make sure in the enter binding’s context is as specific as possible so it doesn’t override the default behavior. This context could also be specified by a plugin hook.

It would technically be possible to add this to syntaxes specifically (with .tmPreferences files), but they would be hard to read from plugins (no API to get non-shellVariables keys from them) and the reindent command can only indent by levels, not by a character’s position which is often referred to as “visual indentation”.

0 Likes

#6

Here’s the test draft with a few of my preferences which I made some time last year:

There is no implementation for this.

I intended to write the visual-indentation code as stand-alone as possible so that I could test it independently without having to use Sublime Text, and then only invoke that module from the ST plugin.

0 Likes

#7

Currently when you set indent_to_bracket = true in sublime user settings.

Where it lines up the brackets. However, the same cant be said for {} and []. How do I modify sublime text to get the same functionality. Ive searched everywhere and even tried writing my own plugin without avail. Help please.

This seemingly trivial aspect is driving me up the wall

0 Likes

#8

Here’s a related (and recent) thread:

0 Likes

#9

Thanks for the response, but I have no clue to do any of what you have just said. I clicked on your github link, but your explanations feels more like a conversation between veteran users than it is a guide for someone who just wants the product to “just work”

0 Likes

#10

What I intended to say with that is there is no “ready to use” solution available yet.

0 Likes

#11

I just merged two threads about the same issue into this one.

0 Likes

#12

I have my plugin that handles parenthesis better than sublime itself, but it is only made to work with parenthesis. It could probably be extended with support for other braces.

0 Likes

#13

Very useful feature suggestion indeed. Waiting for it to be implemented.

0 Likes

#14

I solved this issue with this plugin:

https://packagecontrol.io/packages/IndentToAllBrackets

Note that if you use enter to autocomplete, you’ll have to change the keybindings in

~/.config/sublime-text-3/Installed Packages/IndentToAllBrackets.sublime-package

1 Like