How to remove unnecessary blank line on code formatting in IntelliJ?
Solution 1
Yes. It is possible to configure the number of blank lines in the settings menu (CTRL+ATL+S): File -> Settings -> Editor -> Code Style -> Java(or Scala or whatever your language is) -> Blank Lines
Solution 2
File >> Setting >> Editor >> Code style java >>Blank lines tab
You should change to 0 in code label(as picture), It would remove all unnecessary blank line when press format shortcut: ctrl + alt + L
Solution 3
You can find and replace with regex option also ^(?:[\t ]*(?:\r?\n|\r))+
. It searches all empty lines in file. You need to just replace it with empty
Solution 4
I use regular expression to remove extra blank line from code. Here are the instructions
- Open Find and Replace dialog. Use shortcut CTRL+SHIFT+R.
- In the search box type
^(?:[\t ]*(?:\r?\n|\r)){2,}
. This will search for two ore more blank lines. - In a replace box type
\n
. This will replace with one blank line.
- Open Reformat Code dialog and click Run. Use shortcut CTRL+ALT+L.
This works on all JetBrains IDEs. Use the screenshot as referece.
Solution 5
Just in case it helps someone using newer versions of Intellij, in Intellij IDEA 2016.2.4 it is File -> Other Settings -> Default Settings -> Editor -> Code Style -> Java(or Scala or whatever your language is) -> Blank Lines
Related videos on Youtube
Eugene
Updated on July 05, 2022Comments
-
Eugene almost 2 years
Is it possible to remove empty/blank lines using code formatting in Intellij IDEA?
-
Luk Aron over 4 yearsJust to notice, the accepted answer doesn't work on HTML. It works only for java , python, etc.
-
-
Maxim over 7 yearsWhere can I set it for //region //endregion?
-
Rule over 7 yearsWeird: when I set "Before method body" to 1, don't add an empty line there, then format the code, the empty line is automatically added. But the other way around: I set it to 0, do add an empty line there, then format the code, the empty line is not deleted.
-
Danon almost 6 years@Rule It's because it's "Minimum". Not "Maximum" or "Exact" numer of lines.
-
Rule almost 6 years@Danon you're right, didn't catch that. Would be nice if they provided this option though
-
Danon almost 6 yearsI've submitted a feedback to JetBrains just now, asking whether they'd implement it. I'll post a comment when I get a response :>
-
Dmitriy Popov over 4 yearsUnfortunately, this does not work after the opening brace
{
. So I cannot remove the empty lines after opening a class or a method. For lines before the closing brace}
, works like a charm. I also cannot delete a line after a class ending}
brace.