How to have the formatter wrap code with IntelliJ?
Asked Answered
P

8

175

I'm not able to format my code in IntelliJ.

I can see the margin line (by default at 120 columns) but it seems that the activation from the menu:

Code -> Reformat Code

is just ignoring this margin.

Passed answered 1/10, 2010 at 9:31 Comment(0)
F
107

Do you mean that the formatter does not break long lines? Then please check:

Settings / Project Settings / Code Style / Wrapping

Update: In later versions of IntelliJ, the option can be found under:

Settings / Editor / Code Style

There select Wrap when typing reaches right margin.

Forbis answered 1/10, 2010 at 9:38 Comment(5)
this option is not there anymore, using version 13.0.2, and the problem remains..Oyer
@Oyer the option name is "Ensure right margin is not exceeded"Welterweight
The "Ensure right margin is not exceeded" option is in File / Settings / Editor / Code Style / Java / Wrapping and Braces / Keep when reformattingCuellar
these guys keep on renaming setting keys all the timeLuanneluanni
This is correct, but in new versions, additional options were added to provide finer control about what can be wrapped. See https://mcmap.net/q/142297/-how-to-have-the-formatter-wrap-code-with-intellij for the correct current answer.Agogue
C
207

IntelliJ IDEA 14, 15, 2016 & 2017

Format existing code

  1. Ensure right margin is not exceeded

    File > Settings > Editor > Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded

    File Settings Ensure right margin

  2. Reformat code

    Code > Reformat code...

    Reformat code

    or press Ctrl + Alt + L

    warning If you have something like this:

    thisLineIsVeryLongAndWillBeChanged();   // comment
    

    it will be converted to

    thisLineIsVeryLongAndWillBeChanged();   
    // comment  
    

    instead of

    // comment  
    thisLineIsVeryLongAndWillBeChanged();   
    

    This is why I select pieces of code before reformatting if the code looks like in the previous example.

Wrap when typing reaches right margin

  • IntelliJ IDEA 14: File > Settings > Editor > Code Style > Wrap when typing reaches right margin

    Wrap when typing

  • IntelliJ IDEA 15, 2016 & 2017: File > Settings > Editor > Code Style > Wrap on typing

    Wrap on typing

Coom answered 28/4, 2015 at 14:14 Comment(5)
Yes! This is the actual answer, thank you so much! The point is that the Wrap when typing reaches right margin will get in tremendous trouble once you change something on a line that has already been wrapped. Your answer just cleans upon invocation. Combined with a "save and format" macro this is pure gold!Overcheck
I would like to add one keyword here, which is what one may be looking for: this is what it is called a hard wrap (as opposed to soft wrap -- see the difference here).Instate
Also: No guidance for 2018.1Halpern
format code did it for me. This step should be automaticTripedal
still the working solution in 2023 :PFannyfanon
F
107

Do you mean that the formatter does not break long lines? Then please check:

Settings / Project Settings / Code Style / Wrapping

Update: In later versions of IntelliJ, the option can be found under:

Settings / Editor / Code Style

There select Wrap when typing reaches right margin.

Forbis answered 1/10, 2010 at 9:38 Comment(5)
this option is not there anymore, using version 13.0.2, and the problem remains..Oyer
@Oyer the option name is "Ensure right margin is not exceeded"Welterweight
The "Ensure right margin is not exceeded" option is in File / Settings / Editor / Code Style / Java / Wrapping and Braces / Keep when reformattingCuellar
these guys keep on renaming setting keys all the timeLuanneluanni
This is correct, but in new versions, additional options were added to provide finer control about what can be wrapped. See https://mcmap.net/q/142297/-how-to-have-the-formatter-wrap-code-with-intellij for the correct current answer.Agogue
C
24

Enabling "Ensure right margin is not exceeded" doesn't work for me in Intellij IDEA 2018.2. I have found the workaround, we need to change every elements below from "Do not wrap" to "Wrap if long".

enter image description here enter image description here

After that, we can preview what kind of wrap type will be changed by looking into right panel. If we are satisfied, Click "OK" or "Apply" to apply the changes. Finally we need a mannual format by using CTRL+ ALT+ L in Windows and Command+ Shift+ L in MacOS.

Craunch answered 4/9, 2018 at 0:31 Comment(2)
This was also the problem for me. If that checkbox is set but the rest is left to default IntelliJ tries to wrap lines, but can't because everything is set to "Do not wrap".Durance
it is working for me now, in 2023 :DFannyfanon
M
12

Or you can use the shortcut:

Ctrl + Alt + L

Microclimate answered 30/10, 2013 at 9:50 Comment(4)
Could you post this action's name (Settings -> Keymap)? On most Linuxes the CTRL+ALT+L combination is used to block the computer. I can't even use the "Find action by shortcut".Mudfish
@Mudfish this shortcut is for Code -> Reformat Code.Stator
That locks my screen.Halpern
Only this worked for me on Mac (Cmd + Option + L). None if the checking on boxes in Code Style did.Gosse
E
8

In order to wrap text in the code editor in IntelliJ IDEA 2020.1 community follow these steps:

Ctrl + Shift + "A" OR Help -> Find Action
Enter: "wrap" into the text box
Toggle: View | Active Editor Soft-Wrap "ON"

enter image description here

Edema answered 8/5, 2020 at 3:48 Comment(0)
T
4

In the latest IntelliJ, version 2020, we have an option called soft-wrap these files:

Settings > Editor > General > soft-wrap these files.

Check this option and add the type of files you would like to apply the wrapping to.

soft wrap intellij option

Related question on JetBrains' IDEs Support Forum: Wrap code to indent

Tungstate answered 27/7, 2020 at 4:45 Comment(2)
Isn't this the same what Noah G. already proposed via shortcut Find Action (CRTL + SHIFT + A) View | Active Editor Soft-Wrap "ON" ?Ruffled
Seems they existed already in earlier versions: 2019.1.3 and before.Ruffled
H
3

You can create a macro for Ctrl +Shift + S (for example) that do all this things:

Edit > Macros > Start Macro Recording (the recording will start). Click where you need.

For example:

Code > Reformat Code
Code > Auto-Indent Lines
Code > Optimize Imports
Code > Rearrange Code
File > Save All
... (all that you want)

Then, click on red button in bottom-right of the IDE to stop the Macro recording.

Set a macro name.

Go to File > Settings > Macros > YOUR MACRO NAME.

Right Click > Add Keyboard Shortcut, and type Ctrl + Shift + S.

Hotze answered 20/3, 2017 at 17:17 Comment(1)
This is a basic IDE feature. Macros are OTT and too complicated for something so simple.Halpern
T
0

For those using intellij 2023 version IDEs

I wanted to have all my files soft wrapped, so I did:

Settings -> Editor -> General

Then, in the Soft Wraps section a checked Soft-wrap these files. Then I added a wildcard for all files by prepending *.* to the current value in the textbox:

soft-wrap all files configuration

Worked for me in goland 2023

Tint answered 18/12, 2023 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.