How to have the formatter wrap code with IntelliJ?

Intellij IdeaCode Formatting

Intellij Idea Problem Overview


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.

Intellij Idea Solutions


Solution 1 - Intellij Idea

##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

  1. 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

Solution 2 - Intellij Idea

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.

Solution 3 - Intellij Idea

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.

Solution 4 - Intellij Idea

Or you can use the shortcut:
> Ctrl + Alt + L

Solution 5 - Intellij Idea

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

Solution 6 - Intellij Idea

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

Solution 7 - Intellij Idea

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.

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
QuestionJeroen RosenbergView Question on Stackoverflow
Solution 1 - Intellij IdeaROMANIA_engineerView Answer on Stackoverflow
Solution 2 - Intellij IdeaPéter TörökView Answer on Stackoverflow
Solution 3 - Intellij IdeaguoView Answer on Stackoverflow
Solution 4 - Intellij IdeaEslam HamoudaView Answer on Stackoverflow
Solution 5 - Intellij IdeaNoah PGView Answer on Stackoverflow
Solution 6 - Intellij Ideak sarathView Answer on Stackoverflow
Solution 7 - Intellij Ideauser3006181View Answer on Stackoverflow