Erich is Professor at Lucerne University of Applied Sciences and Arts and Distinguished Member of Technical Staff at Freescale Semiconductor. Erich has a MsCS degree and 18+ years of experience in the embedded software and tools world. He created many embedded cross C/C++ compilers and debuggers. Additionally he is researching in the domain of programming languages, real time and mechatronic systems. Erich is a DZone MVB and is not an employee of DZone and has posted 131 posts at DZone. You can read more from them at their website. View Full User Profile

Removing Blank Lines in Eclipse

09.06.2012
| 13691 views |
  • submit to reddit

Many times I end up using source files from somebody else. And such files might have a lot of empty lines in it I want to get removed. The question is: how to get rid of empty or blank lines in the Eclipse Editor view? Or even better: how to merge multiple empty lines into one?

Shortcut: CTRL+D

Deleting a line is easy: I place the cursor on a line and press CTRL+D (for line Delete). That works as well for multiple line selection. Using the ‘Mother of all shortcuts‘ reveals even more shortcut options:

Delete Commands

Delete Commands

This approach is fine, but manual. There must be something better.

Search-And-Replace: Regular Expression

And there is: To automatically remove empty lines, the Search-And-Replace functionality helps. For this I press CTRL-F (for Find) and configure it like this:

Regular Expression to Remove Empty Lines

Regular Expression to Remove Empty Lines

The magic is using a regular expression checkbox. It uses the regular expression

^\s*\n

to find one or multiple empty lines and replaces it with ‘nothing’. That way I can clean up a full file and get rid of all empty lines.

If I do not remember the syntax of regular expressions any more, then there is help too: the dialog has content assist available:

Content Assist

Content Assist

So that way pressing CTRL+SPACE helps a lot:

Content Assist Help

Content Assist Help

Merging Empty Lines

With this in mind, it is easy to do something more advanced :-) : To merge multiple empty lines into a single one. Again, a regular expression does the magic work:

Merging multiple empty lines into a single one

Merging multiple empty lines into a single one

With this, the regular expression

^\s*\n

finds one or multiple empty lines (as before), and it replaces it with a single empty line:

\R

Now the sources need much less screen real estate :grin: .

Happy removing:-)

Published at DZone with permission of Erich Styger, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sébastien Féré replied on Mon, 2012/09/24 - 2:02am

Hello Erich,

Thank you for this article, especially for the content assist on Eclipse regular expressions !

A simple way to remove blank lines is to customize your Eclipse Code Style formatter. At bottom of "Blank Lines" tab, you can set the "Number of empty lines to preserve" to 1. If you set it to 0, you get a quite unreadable code.

Also, you can format your code at a gobal level (select multi packages or your source folder) with the Format command available in the contextual Source menu.

Happy formatting ;)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.