• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a way in Eclipse to remove all the blank lines in code at once?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way in Eclipse to remove all the blank lines in code at once ? I know one way is to go to each line and do cntrl D but can it be done at one shot ?
Thanks
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this page on the web says there is, and it gives details.

Warning: the answer was posted 8 years ago. (Although I believe you have an old Eclipse version, so maybe that doesn't matter that much.) And also there's a fair bit of squabbling about just how to do it and whether to use regular expressions and if so which ones. But you might be able to pick a correct answer out of all that.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, this page on the web says there is, and it gives details.

Warning: the answer was posted 8 years ago. (Although I believe you have an old Eclipse version, so maybe that doesn't matter that much.) And also there's a fair bit of squabbling about just how to do it and whether to use regular expressions and if so which ones. But you might be able to pick a correct answer out of all that.



It's still there. Still about the same.

It's not actually so much about cleaning up code as it is about enforcing whitespace standards, which is why it has a lot of options above and beyond just removing blank lines. It also requires you to construct a custom code formatting profile and do a Source Format operation, so it's likely to so a lot more than just adjust blank lines.

If you literally want to remove ALL blank lines, you can do that with grep. If you have text that's double-spaced - which is common when you copy stuff off a web page - and you want single spacing, there's sed. Anything much more complex than that I usually use perl.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all. I went to the preferences and created new profile while setting the option for blanks to 0. After this on doing control F it worked fine. For regular expression, I could not understand that where is it supposed to be typed ?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to remove all blank lines? What do you think you're saving by doing this?

Whitespace is important in a program, especially when used judiciously. Whitespace makes your program easier to read, it helps you separate things that logically belong together from other things, and it helps make the structure of your code easier to discern.

Code without any blank lines at all would be horrific to read.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Why would you want to remove all blank lines? What do you think you're saving by doing this?

Whitespace is important in a program, especially when used judiciously. Whitespace makes your program easier to read, it helps you separate things that logically belong together from other things, and it helps make the structure of your code easier to discern.

Code without any blank lines at all would be horrific to read.



Sometimes when I delete some lines while coding  ,the class looks uneven with some place having big blanks even 2 blank lines some leaving 3. And some having no blank lines . I do control F to format but even that doesn't help in such a case. So , thought of removing such big blanks .

I would not mind white space (blank line ) but would mind unevenness caused by several blank lines at one place which is caused by deleting some code.
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't sound like you're actually deleting lines, it sounds like you're just blanking them out.

You can delete a line in several ways:

1. Ctrl+D - deletes the entire line
2. Select line(s) from column 1 to end of last line to delete, press DEL

Quite a few others.

To apply a source format that uses the line-spacing rules in your selected standard format, do Ctrl-A (Select ALL) to highlight the entire source file, then Ctrl+Shift+F to format everything.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I can delete using control D but for that I have to do that for so many blanks again and again.These blanks get unevenly created when I type some line to code and then delete it to type something else.

 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always select, cursor up/down as many lines as you want to delete, then hit the "delete" key.

Eclipse has lots of ways to select, insert and delete text, and many of them are just the same as they would be with almost any other text editing program such as Microsoft Word.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You can always select, cursor up/down as many lines as you want to delete, then hit the "delete" key.

Eclipse has lots of ways to select, insert and delete text, and many of them are just the same as they would be with almost any other text editing program such as Microsoft Word.



Yes I do that.And also I do Control D repeatedly for such irregular blanks.However , I find that error prone as I may mistakenly deleted some code which I need. But it also has to do with building a habbit.I will do.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:That doesn't sound like you're actually deleting lines, it sounds like you're just blanking them out.



What's the difference ?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original code:



Deleting the middle line:



Blanking out the middle line:


 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Original code:



Deleting the middle line:



Blanking out the middle line:




I could not observe any difference between the two.What is the difference?
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another reason why so many blanks get created in my code is that when I add some new lines of working code I create few blank lines and then add, so that if it does not work I can easily identify and delete the extra code in between the blank lines on both sides and it would thus revert back easily to the working state. But I need to delete blank lines which I created later on.Sometimes I forget. I need to be disciplined with this.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Yes I can delete using control D but for that I have to do that for so many blanks again and again.These blanks get unevenly created when I type some line to code and then delete it to type something else.


Sounds like you have a very chaotic experience when you write code. I hardly can imagine how that could happen in the first place. Maybe try to slow down. Are you guessing a lot during code writing?
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe try to slow down. Are you guessing a lot during code writing?

Thanks.Not guessing a lot but it happens that one writes a statement and then does mistake and rewrites it until it works.Must he happening with everyone I think.

May be doing control D regularily would help as soon as any irregular blank is created.

Also, if I develop thought of writing cleaner code , such things would not happen .
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Not guessing a lot but it happens that one writes a statement and then does mistake and rewrites it until it works.


If I'd re-write statement, I'd rewrite on the same line as opposed to use new line. It is a computer, not a plain white paper, but even then you can use an eraser (asuming the pencil was used) and re-use the same line.
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We think that you have a major confusion between what is blanking a line and deleting a line.

To blank a line on paper, you could simply erase what is written. To delete a line on paper, you would have to take scissors, cut out the part of the page with the deleted line on it and paste the page back together with the lines above and below the deleted line touching.

Computer memory and text display does not work that way.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. yes , I was blanking them. I end up with code having uneven blanks because of 2 reasons. Firstly , I was thinking that before introducing any new code to a working code , I can make space and then introduce the new code in between blanks up and down  to keep a clear demarkation such that if the existing code breaks I can clearly know and remove the newly introduced code between these blanks. This is a bad practice. Secondly , for any blanks that got created, I was apprehensive of doing control D as I used to think that by doing control D over some continous blanks I may accidently delete some line of code neighbouring them .This thinking is wrong because even if I accidentally delete a line of code the editor will start showing error and I can do a control Z to revert as soon as I see the compile error.
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's good practice to keep your project source under source version control. The git version control system is very good for that, since you can simply "git init" the project directory and have a per-project archive. Which you can then export to a git server if you like at any time in the future. Eclipse has git menus that can help go it all from within the IDE.

But even without a VCS like git, Eclipse itself keeps a history of changes. Several days worth, in fact. You can list them by their save time/date and "diff" compare/merge different versions. It's a feature that has saved my skin many times.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

But even without a VCS like git, Eclipse itself keeps a history of changes. Several days worth, in fact.



Could you tell where in Eclipse to check for this ?
 
Tim Holloway
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right-mouse (context) menu: "Compare With/Local History..."

Note that this menu mixes itself with VCS history menu if you have a version control system active for the project.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had never seen this and thought such a thing is possible only with version control.
Thanks. I am sure ,someday it will surely help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic