How to remove unnecessary blank line on code formatting in IntelliJ?

59,722

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

Blank lines configuration

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

enter image description here

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.

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.

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

Share:
59,722

Related videos on Youtube

Eugene
Author by

Eugene

Updated on July 05, 2022

Comments

  • Eugene
    Eugene almost 2 years

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

    • Luk Aron
      Luk Aron over 4 years
      Just to notice, the accepted answer doesn't work on HTML. It works only for java , python, etc.
  • Maxim
    Maxim over 7 years
    Where can I set it for //region //endregion?
  • Rule
    Rule over 7 years
    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.
  • Danon
    Danon almost 6 years
    @Rule It's because it's "Minimum". Not "Maximum" or "Exact" numer of lines.
  • Rule
    Rule almost 6 years
    @Danon you're right, didn't catch that. Would be nice if they provided this option though
  • Danon
    Danon almost 6 years
    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 :>
  • Dmitriy Popov
    Dmitriy Popov over 4 years
    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.