Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format only the selected block of code in IntelliJ

Is it possible to format only a selected block of code. I could only either format the whole file or the whole project with IntelliJ. I use community edition 2017.2 on MacOS.

like image 741
Anirudh Jayakumar Avatar asked Aug 17 '17 18:08

Anirudh Jayakumar


3 Answers

From the IntelliJ IDEA Help (Reformat File Dialog):

  1. Highlight the block of code.

  2. Open the Reformat File dialog:

    • Mac: Shift+++L
    • Windows/Linux: Ctrl+Alt+Shift+L
  3. Click the "Selected Text" radio button.

  4. Click the "Run" button.

It should look something like this:

Imagine of Reformat File dialog

like image 107
byxor Avatar answered Oct 04 '22 23:10

byxor


Yes; simply highlight the block of code and format it, either through the keyboard or through the menu. How it will be formatted, and what will be formatted, are driven solely by your formatting preferences.

like image 44
Makoto Avatar answered Oct 05 '22 00:10

Makoto


Reformat a code fragment in a file:

  1. In the editor, select a code fragment you want to reformat.
  2. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L.

Note. If you don't select a code fragment, IntelliJ IDEA will reformat the whole file.

like image 40
Ikbel Avatar answered Oct 05 '22 01:10

Ikbel