Remove empty lines in Netbeans 7.x [closed]
Asked Answered
F

4

20

Is there any plugin, macro or built-in function that can remove empty or redundant empty lines in Netbeans 7.2 (PHP bundle, Windows 7)? Due to different line-break formats in my upload and download process (Win <> Unix, via FileZilla and Netbeans), many PHP files doubled (even quadrupled) their line breaks.

Double line breaks in PHP file

I could remove them file by file using PSPad - but I am looking for an automatic or batch-capable solution. I can accept an external tool if available.

Edit: I know about the Source > Format helper, but there are no settings to remove blank lines.

Footsie answered 6/3, 2013 at 15:25 Comment(1)
it's worth pointing out the Netbeans 7.3 was released recently. I don't suppose it'll make any difference to this question, but you might want to upgrade anyway -- it has some good enhancements for PHP devs.Chrisse
P
64

You can do this with a regular expression + and a replacement. Do this in an editor window or in the project (or files) directory tree:

  • Ctrl + H
  • Check "Regular Expression"
  • Search for \n\n
  • Replace with \n
  • Click "Replace all" until all lines are deleted

If you want to keep a blank line you should:

  • Search for \n\n\n
  • Replace with \n\n

If you want to replace consecutive lines that are not empty but contain whitespaces only you should:

  • Search for \n[\t\r]+$\n
  • Replace with \n
Peisch answered 6/3, 2013 at 17:12 Comment(2)
Works great, I can even choose a folder in the project tree, hit CTRL-H and apply the pattern to multiple files. I added a second expression to your answer - for empty lines with whitespaces.Footsie
It should be part of netbeans autoformat; other editor has it :(Melanymelaphyre
S
13

Try this one...

Ctrl + F (find) in Netbeans as Regular Expression:

\n\s*(\n)

and Replace All by

$1
Scotopia answered 23/5, 2015 at 7:38 Comment(0)
V
9

In NetBeans 7.3 (I'm not sure about the earlier version, it should be available in 7.x)

Automatic removal:

Tools > Options > Editor > On Save > Remove Trailing Whitespace:

you can choose from: "None / All Lines / Modified Lines Only"

Manual way:

Alt + Shift + F

Vermont answered 10/10, 2013 at 11:29 Comment(2)
it does not remove the empty blank lines if there is too many blank lines (\n)Melanymelaphyre
Otherwise you wouldn't be able to insert intentionally empty lines to visually separate your code. This function is just for unification of lines endings.Vermont
A
3

Yes there is one and this is a very important feature indeed. It depends on your settings but by default you can press Alt + Shift + F or find it in the context menu: Source > Format.

You can setup your own style for formating here: Tools > Options > Formatting. You can set this for all languages or individualy.

Hope this helps.

Ashcraft answered 6/3, 2013 at 15:30 Comment(3)
Thanks - although you're basically right, there are no settings to remove blank lines. Source > Format was the place I hoped to get lucky, but there is nothing that does correct my line problem.Footsie
The solution provided by Code-Source is fine. But nonetheless if you format the code (click in the editor window and press Alt+Shift+F) it actually does automatically adjust / remove indents and whitespace. At least in my netbeans editor this is the case.Clung
works for me!!! excellentLeekgreen

© 2022 - 2024 — McMap. All rights reserved.