Sublime Forum

Curly braces on new line

#1

Hello,

how to set that the opening curly brace will be on new line?

The code actually is:
----abcde() {
-------- …
----}

I want this:
----abcde()
----{
--------…
----}

How to achieve this with a regex or using regReplace or by other tool?

Thank’s a lot!

Ps. every - is a space…

0 Likes

#2

You can replace it using a regex. Press Ctrl + H and then check regex option. Enter these parameters:

Find What: (\r\n|\r|\n)([ \t]*)(.*?)[ \t]*{[ \t]*(?:\r\n|\r|\n)
Replace With: $1$2$3$1$2{$1

Then click Replace All and it should work. It also idents brace with tabs or spaces to match the rest of the code.

0 Likes

#3

Hi, if I run it twice, it redo the action creating unespected newline before the opening {:

abcde() { ... }

become:

abcde() { ... }

and then:

abcde() // <-- newline { ... }

0 Likes

#4

@user11111 You didn’t mention that you may run it twice. But, there is still a solution:

Find What: (\r\n|\r|\n)([ \t]*)(\S.*?)[ \t]*{[ \t]*(?:\r\n|\r|\n)
Replace With: $1$2$3$1$2{$1

It allows you to run it as many times as you want. All correctly placed braces will not be affected.

You also asked how to do it with RegReplace plugin. Well, I don’t use it. Why would you use it when it is easier to do it with native Sublime regex replacement function.

1 Like

#5

Thank’s a lot! It works fine and fast!

I need the regReplace version because I’ll use a keyboard shortcut to run the search&replace.

There is a way to do that by using the regex you suggested without modify it by shortcut?

Edit:
The solution for who are using regReplace is:
"find": "(\\r\\n|\\r|\\n)([ \\t]*)(\\S.*?)[ \\t]*{[ \\t]*(?:\\r\\n|\\r|\\n)"
"replace": "\\g<1>\\g<2>\\g<3>\\g<1>\\g<2>{\\g<1>"

0 Likes

#6

Compare:

0 Likes