90

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

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
Eugene
  • 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 Answers7

128

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

Blank lines configuration

mithunpaul
  • 3,268
  • 22
  • 19
Vic
  • 21,473
  • 11
  • 76
  • 97
  • 1
    Where can I set it for //region //endregion? – Maxim Dec 12 '16 at 17:54
  • 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
  • 1
    I'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
39
  • 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

enter image description here

huu duy
  • 2,049
  • 21
  • 31
36

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

maxshuty
  • 9,708
  • 13
  • 64
  • 77
Ajaykumar
  • 471
  • 4
  • 4
18

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.

enter image description here

  • Open Reformat Code dialog and click Run. Use shortcut CTRL+ALT+L.

This works on all JetBrains IDEs. Use the screenshot as referece.

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

mithunpaul
  • 3,268
  • 22
  • 19
1

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"

Peter
  • 453
  • 3
  • 18
0

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
Martin54
  • 1,349
  • 2
  • 13
  • 34