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?
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:
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:
The magic is using a regular expression checkbox. It uses the regular expression
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:
So that way pressing CTRL+SPACE helps a lot:
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:
With this, the regular expression
finds one or multiple empty lines (as before), and it replaces it with a single empty line:
Now the sources need much less screen real estate😀.