Anirudh Jayakumar
Anirudh Jayakumar

Reputation: 1234

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.

Upvotes: 14

Views: 8503

Answers (3)

Ikbel
Ikbel

Reputation: 2203

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.

Upvotes: 2

byxor
byxor

Reputation: 6349

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

Upvotes: 17

Makoto
Makoto

Reputation: 106430

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.

Upvotes: 3

Related Questions