How to surround code by curly braces in IntelliJ IDEA?

Intellij IdeaKeyboard ShortcutsCurly Braces

Intellij Idea Problem Overview


How to surround code by curly braces in IntelliJ IDEA? Is there any keyboard-shortcut for such operation?

Intellij Idea Solutions


Solution 1 - Intellij Idea

Just configure Smart Keys as follow:
IDE Settings > Editor > General > Smart Keys > Surround selection on typing quote or brace.

enter image description here

From the IntelliJ built-in help:

> If this check box is selected, the selected text on typing a quote, > double-quote or brace, will be surrounded with these characters. > > If this check box is not selected, then the typed quotes, double-quotes > or braces will replace the selection.

Solution 2 - Intellij Idea

Yes, try Ctrl+Alt+T (Surround With), then A (Curly Braces).

Solution 3 - Intellij Idea

Just click as the below 2 steps

1, CTRL + ALT + T
2, then press ALT + A at the same time.

Hope to help you.

Solution 4 - Intellij Idea

Add "Control flow statement without braces" to inspection settings. Then run inspection, select them in the list and then select "Apply Fix 'Add braces'".

It just saved me a whole days tedious work!

Solution 5 - Intellij Idea

I had oneline if statement without curly braces and I had to update it with braces in whole file. So I updated code style to always add braces for if/else statement. In Idea go to:

> Settings -> Editor -> Code Style -> Java -> Wrapping and Braces (tab)

Find "'If()' Statement"/"Force braces" and change value from "Do not force"(default) to "Always". Press ctrl + alt + L to reformat your file and curly braces would be added. settings

Solution 6 - Intellij Idea

You need to create a live template first. Just go Preferences and add a template to html/xml

enter image description here

Solution 7 - Intellij Idea

The easiest way is, select the code part that you want and press Shift + [

Solution 8 - Intellij Idea

Easy, just follow the arrows in this picture, from left to right: Screenshot with arrows

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavelView Question on Stackoverflow
Solution 1 - Intellij IdeaAjang RView Answer on Stackoverflow
Solution 2 - Intellij IdeazagyiView Answer on Stackoverflow
Solution 3 - Intellij IdeaHunter ZhaoView Answer on Stackoverflow
Solution 4 - Intellij IdeaMartin WickmanView Answer on Stackoverflow
Solution 5 - Intellij IdeaamisiurykView Answer on Stackoverflow
Solution 6 - Intellij IdeagokselView Answer on Stackoverflow
Solution 7 - Intellij IdeaSupun SandaruwanView Answer on Stackoverflow
Solution 8 - Intellij IdeaDr. Michael R. AlversView Answer on Stackoverflow