X

An Oracle blog about NetBeans for PHP

Removing Trailing Spaces

Last week, when I was at the IPC 2009 conference, a some user ask me whether NetBeans removes trailing spaces. It was one question from many and I would like to clarify how it works in NetBeans 6.0 - NetBeans 6.8.

Basically it works automatically on save. So when you invoke Save action then the trailing spaces should be removed from the file. There are two exceptions, when the spaces are not removed from a line:

  • The line wasn't changed. The main reason for such behavior is that usually you don't want to have unnecessary changes in the diff, if the files comes from a VCS. So unchanged lines are not touched and if there is a space at the end of such line, then is not removed. When you edit the line then the spaces are removed on save as well.
  • The line where the caret is located. When you invoke Save action, then the position of the caret is not changed. But all trailing spaces after caret are removed.

If you want remove all trailing spaces in the file, you have to invoke action Remove Trailing Spaces from Source menu by hand.

Join the discussion

Comments ( 32 )
  • Timo Tuesday, November 24, 2009

    Hi Petr,

    i was one of these guys. And i think this behaviour isn't very useful. I think it is very very necessary to give users a chance to configure those options. NetBeans as IDE has very less configuration options which is in my eyes not acceptable for a good IDE. Each developer handles a lot of things different to others and cannot modify the behaviour of an IDE is worse.

    I would like to decide this for my own. If i know forget to move my cursor to the first position in a line an save this the merging problem will only be moved forward away from NetBeans. If some other guys with other IDE's will know edit and save those files, they will have a change, because the will correctly drop the trailing white spaces which i have not. Which is a much bigger Problem, because this users have to check, what they have changed in a file in lines they never edited.

    And one more thing about this. If i edit a file which is NOT in svn/cvs, the behaviour stays the same. If i decide to strip trailing white spaces, the IDE should not try to decided when this should be done.

    Other example. We uses pre commit hooks in svn which checks the coding styles. If i try to commit a file with trailing white spaces, it will be blocked and i have to handle it by my own to fix this.

    Sorry, but the missing configuration options in NetBeans are a big big minus to the IDE. These are all stuff other IDE's like eclipse handles it much much better and convenient for the users.

    I although think, that an IDE is not a dummy tool like a WYSIWIG text editor. It is OK to have configuration options. Guys are in trouble with this should perhaps do not software developing :-)

    Timo


  • robo47 Tuesday, November 24, 2009

    Would really be happy if it would be possible to just add removing trailing spaces on ALL lines as an action on save, not only when changed something in that specific line.

    Meanwhile I have to use an ant-job which uses sed in my project which does it for me on ALL php/phtml-files:

    http://www.robo47.net/codeschnipsel/29-Remove-Trailing-Spaces-sed-basierter-Ant-Task


  • Petr Pisl Tuesday, November 24, 2009

    Hi Timo, robo47

    I agree with you that there has to be a possibility to configure the behavior. The functionality is not covered by PHP team, it's a part of basic editor functionality, so we have to push guys from editor core to fix it. I have talked with them and they know about this problem. I hope that they will have enough time to do it.

    Regards,

    Petr


  • Tom Tuesday, November 24, 2009

    I agree with the posts above.

    Just have a look at simple text editors like "ConTEXT". Even in such a simple editor I may configure the following behavior:

    - trim trailing spaces on save

    - allow cursor after end of line

    - backup file on save

    - remember editing positions on save

    - detect external file changes

    - automatically update on external file changes

    - auto-indent

    - enable drag-drop editing

    - trigger smart home-key positioning

    - automatically copy text at cursor to search dialog

    - enable smart tabs

    - enable line numbers

    - group undo

    - undo after save

    - automatically hide mouse when typing

    - enable word-wrap by default

    - show word-wrap glyph

    - default text font and size

    - show gutter

    - gutter width

    - default highlighter

    - indention settings

    - word-wrap width

    So: which of those above do I have in NetBeans?

    It's "indention settings". That's all.


  • Honza Marek Tuesday, November 24, 2009

    Some features from your list Netbeans already have.


  • Tom Tuesday, November 24, 2009

    @honza Yes, we now it has some of those. It's just that you can't configure those editor settings.

    We ask for a button to turn them on or off. ;-)


  • agreed Tuesday, November 24, 2009

    Trailing spaces and other options to cleanup files (whitespace/formatting wise) should be configurable as well.

    Please make new line characters configurable instead of OS-dependant. E.g. I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux.

    Sensible defaults are nice things, but they often collide with presets and defaults used in companies or personal and open source projects.


  • Tom Wednesday, November 25, 2009

    BTW: did you know that SVN has a property to enforce Unix-style endings on commit? Just set svn:eol-style to LF. This might be away around this issue for now.

    Only problem is: you need to set it by hand for each file. Plus it won't kick in for newly added files either.


  • dalin Wednesday, November 25, 2009

    The posters above are crazy! The existing method of removing trailing space only from the lines that you have edited is ingenious. You can still remove them all via the menu if you like.

    Petr Pisi if your VCS has a pre-commit hook that blocks commits when there's trailing whitespace then the default netbeans behaviour is perfect for you. Any lines that have whitespace that you didn't edit were there before you got there and thus caused by someone else. If your team is that concerned about trailing whitespace then write a script to cleanup your whole repository.

    Please don't add unnecessary options to Netbeans. If you want an option for every insignificant thing then you might prefer Eclipse. Netbeans' simplicity is a breath of fresh air allowing me to focus on the important things: the code.


  • Petr Pisl Wednesday, November 25, 2009

    to Tom: You are right, some features are not implemented. It doesn't mean that we don't want to do it. As every project, the NetBeans project has limited resources and we have to choose, which features we can do. For example word wrapping. It looks like simple feature, but it is not. Yes, almost every simple text editor has it, but there is "just" word wrapping. In NetBeans you have to be careful about much more things. So when we decide to implement it, it means that you have to change internal line representation and all other features like debugging, parsing, hints etc. should be aware of this.

    I agree that can the trailing spaces can be configurable. I talked this week with editor guys, and they agree with me as well. The current behavior is a result of evolution, which fits most case in Java world. To be honest, I haven't heard a complain about this until we published the PHP support.

    to agreed: I can be wrong, but I think that the current behavior is that NetBeans save the file with the same new line character that is at the first line, when the file is open. The internal representation is always in java way, but saving is done with the char which was found at the first line.

    As dalin wrote, every VCS system has an option to convert during commits the new line characters to unify them through repository. At least it works in all VCS that I have used so far(CVS, Subversion, Git and Mercurial).

    to dalin: Exactly, the main phylosophy behing the first exception (when the trailing spaces are not deleted) is that the diffs should contain only relevant changes to be easy readable. And it's good practice to do changes in separate commit like change formatting of the file or remove trailing spaces.

    On the other hand it's true that NetBeans usually know, whether the file is or isn't in a VCS, but it can be configurable as well. We are still trying to keep Options "clean" from the UI point of view. I hope that we will keep this trend in the feature, although we will add new options.:)


  • Tom Wednesday, November 25, 2009

    @Petr I wouldn't change word-wrap at all. It might not be perfect, but it's okay the way it is.

    To the trailing spaces issue: I agree that the line-ending should not be changed. However: I feel that a default line-ending style for new files would be okay.

    Perhaps it is more of a SVN-issue? Setting the eol-style property automatically when adding a new file (or even saving an existing file?), according to some project settings, should do the job, I suppose. This behaviour should also ensure that these changes don't appear in the SVN-diffs at all.


  • Nick Tuesday, December 8, 2009

    I don't like the current behavior in NetBeans. My fellow developers at this company use Zend Studio (the more recent versions based on Eclipse). They use a strip-trailing-whitespace-on-save option.

    I've gotten complaints that my commits to our Subversion repository have trailing tabs in them. This tends to happen, I believe, when I add several lines, and hit return to leave a blank line between my new code insert and the next block of code. Hitting return causes NetBeans to tab out to the correct indent point. I then hit save, and that blank line with one or more tabs on it is preserved.

    It's difficult to remember to go to the Source menu and select "Remove Trailing Spaces" before each save and commit.

    Is there an open ticket in the Netbeans bug tracker referencing this issue?


  • Petr Pisl Wednesday, December 9, 2009

    Hi Nick,

    the issue is https://www.netbeans.org/bugzilla/show_bug.cgi?id=157561 . You can vote for this. The good news is that is on the plan for NetBeans 6.8.

    You mentioned that you use subversion. I'm sure that there is a subversion hook, which can solve it.

    Regards,

    Petr


  • Nick Wednesday, December 9, 2009

    Hi Petr,

    Thanks for your reply. Unfortunately, that bug seems to be asking for the reverse of what I want -- the commenters seem to be requesting a UI feature to disable NetBeans from stripping trailing spaces on a save.

    What I want is for NetBeans to _more aggressively_ strip spaces on save -- at the moment, it skips spaces before the caret on an otherwise empty line. I want it to strip those spaces too.

    If I have the following code:

    if (i > 2)

    {

    <tab>echo "Some string.";

    <tab><-- caret is here after pressing return on previous line.

    and save the file, I end up with a tab on the line after the echo. Instead, I want the tab or spaces on that line to be removed on save. This only happens if I manually move the cursor to the beginning of the line, or to another line, before saving. I will not always remember to do this.

    The functionality I'm looking for would be equivalent with Zend Studio's PHP -> Editor -> Save Actions -> Remove trailing whitespace option, with the All lines radio button selected.

    Does that make sense?

    Best,

    Nick


  • Nick Wednesday, December 9, 2009

    This is bad form to reply to myself, but my post above was a long winded way of saying that I want exactly the behavior of NetBeans' existing "Remove Trailing Spaces from Source" feature to automatically occur when the file is saved. I do not want either of the two exceptions that are mentioned in the original blog post.

    Do you still think there's an open bug addressing this need? I've added a note with my request to Bug 157561, anyway.

    In the meantime -- thank you for the tip on the Subversion pre-commit hook to prevent a commit containing whitespace. This would be an acceptable workaround for the time being. Do you know of a script I can incorporate into pre-commit to do this?

    Thanks,

    Nick


  • Petr Pisl Thursday, December 10, 2009

    Hi Nick,

    Tom mentioned in his comment

    "BTW: did you know that SVN has a property to enforce Unix-style endings on commit? Just set svn:eol-style to LF. This might be away around this issue for now"

    I think that the issue #157561 converts into options that will allow to control the triling spaces behavior. It's on the editor team plan for NB 6.9.


  • Pakozdi Tibor Monday, February 15, 2010

    Tom wrote: "Please don't add unnecessary options to Netbeans. If you want an option for every insignificant thing then you might prefer Eclipse. Netbeans' simplicity is a breath of fresh air allowing me to focus on the important things: the code."

    I think that already this trailing space removal when something is set(or not) but caret is(or not) there or some other condition like the moon was full 3 days ago or anything... is exactly not simple!!!

    there should be an advanced option if I want this behaviour or I do not want Netbeans to remove the trailing spaces

    for some people it is important to add trailing space


  • Marko Thursday, February 25, 2010

    This is I think the only feature stopping me from moving to NetBeans, so I'm stuck with bloated Eclipse (I code in PHP). There really should be an option to enable this, do some "advanced settings" or something...


  • Mike Hunter Wednesday, March 10, 2010

    I really liked what this tutorial has to offer. thanks so much for sharing it! in my turn I'd like to recommenend you the best music search engine http://www.mp3hunting.com enjoy!


  • Dollar Items Tuesday, March 23, 2010

    did you know that SVN has a property to enforce Unix-style endings on commit?

    Just set svn:eol-style to LF. This might be away around this issue for now


  • labatterie Monday, April 26, 2010

    Just set svn:eol-style to LF. This might be away around this issue for now


  • error fix Thursday, August 26, 2010

    I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux.


  • yiwu Saturday, September 11, 2010

    I'm forced to work using windows, while all source code I develop


  • Backup Friday, September 24, 2010

    Strip trailing spaces on save is essential for every serious programmer. I am deeply disappointed and could almost not believe my eyes that there is no such option as of 6.9.1 yet.


  • yiwu Wednesday, October 6, 2010

    Strip trailing spaces on save is essential for every serious programmer. I


  • Rich Murphy Thursday, October 7, 2010

    Simply brilliant! What a time saver this is. Thanks. I am going to recommend it over to the guys at http://speedupmypctips.com.


  • Pakozdi Wednesday, November 3, 2010

    Use your browser's back button and you won't lose what you've written. If the Captcha image there isn't clear, click Preview to get a new one.


  • florida cpa Sunday, December 19, 2010

    hey I need the NetBeans for a new proyect, where can I get it??? thanks!


  • jacky Tuesday, January 11, 2011

    Strip trailing spaces on save is essential for every serious programmer. I am deeply disappointed and could almost not believe my eyes that there is no such option as of 6.9.1 yet.

    http://www.areshop.com/Watches-Breitling.html

    http://www.areshop.com/Watches-Cartier.html


  • steve Tuesday, January 11, 2011

    I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux. http://www.areshop.com/Watches-Omega.html

    http://www.areshop.com/Watches-Rolex.html


  • Costa Rica Doctors Saturday, January 15, 2011

    I think I need more configuration options, because It wuld make my life easier!!

    I like NetBeans 6.8., it easier but it could be a improvement piont in the future!


  • guest Friday, March 28, 2014

    I dont't understand why NetBeans doesn't have the option to remove trailing spaces of the line where the caret is on save. Eclipse has that option a long time.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.