Removing Blank Lines in Eclipse
Join the DZone community and get the full member experience.Join For Free
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 d elete). 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 .
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.