Geertjan's Blog

  • May 22, 2008

Comment/Uncomment Lines in HTML/JSP/XML Files

Geertjan Wielenga
Product Manager
I demoed a small plugin I was working on yesterday to Ludo—it lets you toggle the comment/uncomment tags in HTML, JSP and XML files, which is possible by default in Java source files (and in Properties files) but not in HTML files, JSP files, and XML files. At the time, the plugin only worked for single lines and only via a shortcut or a contextual menu item. However, Ludo told me that he uses the editor's toolbar and that he'd also like multiline selections to be possible. Well, Ludo, both items have been added and now the plugin is available in the Plugin Portal:


Make sure you read the open issues below the screenshots there, so that you're aware of them before you run into them by accident. In short, you're able to select multiple lines, as below, but if you don't, the plugin assumes that you're only interested in the current line, even if you haven't selected anything in the current line. Then you invoke the action, via Ctrl-Shift-C (just like in the Java editor and the Properties editor), or choose the contextual menu item, shown below, or Ludo's button in the editor's toolbar.

And then you find yourself with the comment tags around the selection (as illustrated below) which, when you select the selection again and then invoke the action again, are removed so that the selection is returned to its original state.

Note that the plugin installs this functionality for HTML files, XML files, and JSP files. I have tried it out on Ubuntu and a colleague tried it out on Windows XP. It worked as expected in both environments. Comments/uncomments on this plugin are more than welcome...

Join the discussion

Comments ( 17 )
  • Casper Thursday, May 22, 2008

    Cool, but this would not for facelets (.xhtml files). Which makes me wonder, is there a mime-type hierarchy in place such that one could install something in a layer file without having to know each and every mime-type?

    It's very verbose to do a plugin for general-purpose XML when you have to list each an every possible xml mime-type up front (x-schema+xml, xml-dtd, x-ant+xml, x-jsf+xml, x-struts+xml,x-wsdl+xml, xml etc.)

  • Jesse Glick Thursday, May 22, 2008

    Strange, for me Toggle Comment is on Ctrl-Slash.

    Editor-related APIs generally use the MIME Lookup API, which lets you use Editors/Something to register "something"s applicable to any editor, Editors/text/x-java/Something for the Java source editor, Editors/text/x-ant+xml/Something for the Ant editor... and Editors/text/xml/Something for any XML-based editor (text/xml or any text/\*+xml).

  • Geertjan Thursday, May 22, 2008

    Yes, all XML types (inc. XHTML) are covered. Anything that is XML (such as Spring config files) has this menu item. Jesse, I believe that Toggle Comment is invoked both by Ctrl-Slash and by Ctrl-Shift-C. I have just now uploaded the 0.7 version of this plugin, which now lets you invoke the action both ways, i.e., not just via Ctrl-Shift-C, but also via Ctrl-Slash.

  • Casper Thursday, May 22, 2008

    Ok thanks Geertjan, I can see your plugin indeed works with xhtml files. Which mime-type do you use for this (can't simply be "xml")?

    Also, the plugin has issues with "undo". When "rolling back" from a comment toggle, the entire block is removed rather than only the <!-- --> markers.

  • Geertjan Thursday, May 22, 2008

    Casper, the "Undo" thing is already mentioned on the plugin's page in the Plugin Portal. Please go there and read the issues -- good to do before you use it so that you're not surprised by them. Secondly, in answer to your question, and as Jesse points out, I simply used text/xml MIME type, which covers all XML files.

  • Casper Thursday, May 22, 2008

    I can't get it to work with text/xml. Oh well, I'll just have to wait until you decide to offer the source so I can take a look. ;)

  • Geertjan Thursday, May 22, 2008

    Really? Works fine for me in text/xml. I downloaded the 0.7 version of the plugin, installed it, created an XML file, and saw everything as it should be. It should work in all XML files. Sources will be made available once I've fixed the currently listed issues.

  • Geertjan Thursday, May 22, 2008

    Uploaded version 0.8, which fixes the Undo problem.

  • joshis Thursday, May 22, 2008

    Hi! ;)

    The plugin works quite well, but I think there is problem with the usability at this moment (which won't be so hard to fix).

    IMHO when you select some text (multi-line) and when you invoke the comment action, each line of selection should be commented out separately... This would allow more possibilities for commenting/uncommenting text (I want to comment everything except a few lines...).

    Furthermore, try to invoke the comment/uncomment action on multi-line selection now. It is not very comfortable to comment/uncomment some large selection...

    Otherwise, great work.

  • Geertjan Thursday, May 22, 2008

    Hi joshis, thanks for the comment. You're exactly right, in fact, you perfectly describe the first issue that is listed on the plugin's page. The workaround is described there too. That's the first next thing that will be fixed. Version 0.9 of the plugin will solve it, hopefully in the next few days.

  • Geertjan Thursday, May 22, 2008

    Uploaded version 0.9, which fixes the multiline select problem discussed above.

  • Tor Norbye Friday, May 23, 2008

    I don't think this should be covered by a separate optional module; it should be part of the base product. This action works in other file types and I thought it was a bug (at least a usability bug even if it can be argued that it's a missing feature) that it didn't work in XML files last time I tried. Can you work with the editor team to get this into the editor implementations?

  • joshis Sunday, May 25, 2008

    Geertjan: Hmm, you are right - it was there, sorry for the spam... I should read first before spamming known issues under blogs:)... I was so eager to try out the plugin that I forgot to read about its issues...

    Tor Norbye: Yes, you are right, this is in progress. For more details see the nabble thread: http://www.nabble.com/Newbie%3A-which-shortcut-key-comment-uncomment-code-in-JSP-file--to17330355.html#a17336894 and maybe this page: http://wiki.netbeans.org/J2eeEditorsTasksAndFeatures60. BTW: It is funny that comment/uncomment is working (I tried it and I think it works quite well) for XML, but not for (X)HTML and JSP...

  • Joshis Sunday, May 25, 2008

    For Tor again: If you want to use Comment/Uncomment only for XML (not HTML, XHTML, which is quite funny), you can set the shortcut in Tools > Options > Keymaps, under node Other > "XML Comment" || "XML Uncomment"...

  • vu the cuong Friday, August 29, 2008

    I'm new to netbeans. I downloaded your plugin.

    How can I install it.

    Could you describe in details?

    thanks and regards

  • Devin Whalen Monday, September 21, 2009

    Hey, I want to try out your plugin but am having problems downloading it. Is there another download URL besides the one on the netbean plugins page? Or a way to fix the download from the plugins page?


  • Geertjan Wielenga Monday, September 21, 2009

    No need to try it out. That functionality is now standard in NetBeans IDE.

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