Sublime Forum

Setting for GNU-style braces

#1

Is there a setting that tells ST3 to use GNU-style braces and indentation?

if (condition)
  {
    statement;
  }
else
  {
    statement;
  }

Notice the braces are indented two spaces from the condition, and the statements are indented two spaces from the braces.

I have the following options set:

"tab_size": 2,
"translate_tabs_to_spaces": true

There are only two behaviors I want to correct:

When I type if (condition) and then hit enter, it places my cursor indented two spaces from the condition; however, as soon as I hit the “{” key, it pushes it back to the same level as the conditional. To correct this manually, I always need to move my cursor before the brace and insert an extra tab.

If I place my cursor after the “}” and press enter, it puts me at the same level as the brace. To correct this manually, I always need to hit backspace.

0 Likes

#2

There is no a “gnu-style-indent” settings.
You should try disabling the following settings (starting by smart_indent):

    // Calculates indentation automatically when pressing enter
    "auto_indent": true,

    // Makes auto indent a little smarter, e.g., by indenting the next line
    // after an if statement in C. Requires auto_indent to be enabled.
    "smart_indent": true,

Also you can customize yourself what happen on “enter” by using the following keybindings.
Like:

    // Insert line and 4 spaces after an opening parenthesis/bracket in C++.
    { "keys": ["enter"], "command": "insert", "args": {"characters": "\n    "}, "context":
        [
            { "key": "preceding_text", "operator": "regex_match", "operand": "^.*[([]" },
            { "key": "selector", "operator": "equal", "operand": "source.c++" }
        ]
    },
0 Likes

#3

you could also try to disable the disableIndentNextLinePattern from all the syntaxes you use. I don’t think this is quick/easy though - you’ll need to override all the tmPreferences files to set the disableIndentNextLinePattern to an empty string.

I think @gwenzek’s idea of using a keybinding is a good idea here - you may need a macro to insert a \n and then a backspace.

0 Likes