Is it possible to remove empty/blank lines using code formatting in Intellij IDEA?

- 25,047
- 11
- 113
- 117

- 59,186
- 91
- 226
- 333
-
Just to notice, the accepted answer doesn't work on HTML. It works only for java , python, etc. – Luk Aron Nov 09 '19 at 08:31
-
you can also look for regex $\n\n and replace it with \n (original idea from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206344569/comments/206917645) – Joand Sep 19 '22 at 16:24
7 Answers
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

- 3,268
- 22
- 19

- 21,473
- 11
- 76
- 97
-
1
-
Weird: 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. – Rule Feb 11 '17 at 23:18
-
@Rule It's because it's "Minimum". Not "Maximum" or "Exact" numer of lines. – Danon Jun 29 '18 at 08:46
-
@Danon you're right, didn't catch that. Would be nice if they provided this option though – Rule Jun 29 '18 at 09:49
-
1I've submitted a feedback to JetBrains just now, asking whether they'd implement it. I'll post a comment when I get a response :> – Danon Jun 29 '18 at 10:48
-
Unfortunately, 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. – Dmitriy Popov Sep 17 '19 at 13:10
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

- 2,049
- 21
- 31
I use regular expressions to remove extra blank lines from the 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.

- 25,047
- 11
- 113
- 117
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

- 3,268
- 22
- 19
For those trying to remove blank lines at the end of the file, this became a feature as of August 2020.
The proper place to configure this action is under Settings|Editor|General|On Save. Check "Remove trailing blank lines"

- 453
- 3
- 18
I have recently encountered this problem and here is the solution I found out.
Minify your code to one line and then style it like you normally would.
ctrl + shift + j //shortcut to oneline
ctrl + alt + l //shortcut to styling

- 1,349
- 2
- 13
- 34