Eclipse Gems, Tips & Tricks: Block Comments

This article is part of a ‘mini series’ about hidden gems, tips and tricks around Eclipse.
The topic of this one is how to comment and un-comment a block of source code.

Block Comment

Block Comment

There are many different ways how to comment (or un-comment) multiple lines.

As as starter, there is the ‘Source’ menu and context menu for this:

context menu

context menu

select the text and choose

  • Source > Toggle Comment: add/remove C++ (//) comment
  • Source > Add Block Comment: Add C (/* …*/) comment
  • Source > Remove Block Comment: Remove C (/* … */) comment
Source Menu

Source Menu

The other (faster) way is using the keyboard shortcuts for the above (<CTRL>+<7>, <CTRL>+<SHIFT>+</> and <CTRL>+<SHIFT>+<\>).

I prefer the C++ (//) comment style as that way it is easy to comment lines with nested comments:

nested comment block

nested comment block

If commenting with /* … */ a nested /* … */ comment, then Eclipse will un-comment the nested comment which might not what you want.

Finally, what I’m using frequently is just to use the preprocessor and #if, for example

Disabling with preprocessor

Disabling with preprocessor

Changing the #if 0 to a #if 1 toggles the ‘comment’. That style works always and does not mess with nested normal comments.

Disabling with preprocessor

Disabling with preprocessor

That way I can easily switch between different alternatives:

Multi-Block Comment

Multi-Block Comment

Happy commenting 🙂

Links

 

 

11 thoughts on “Eclipse Gems, Tips & Tricks: Block Comments

    • Same; I’m told it’s preferable, and MISRA compliant as it allows comments inside the #if block.
      I even have a code template for it: hifr
      #if 1 // region ${description}
      ${cursor}
      #endif // region ${description}
      Then I use Alt-Cursor down on the last line to move the line around. A good way of tidying code up too. [It’s a shame the #region preprocessor directive doesn’t exist on this platform (C# .net has it, handy!).]

      Liked by 1 person

  1. Pingback: Eclipse Gems, Tips & Tricks: Working Sets | MCU on Eclipse

  2. Pingback: Eclipse Gems, Tips & Tricks: Highlight Usage and Definition | MCU on Eclipse

  3. Pingback: Eclipse Gems, Tips & Tricks: Code Completion | MCU on Eclipse

  4. Pingback: Eclipse Gems, Tips & Tricks: Faster Debugger Start | MCU on Eclipse

  5. Pingback: Eclipse Gems, Tips & Tricks: Project Filter | MCU on Eclipse

  6. Pingback: Eclipse Gems, Tips & Tricks: Text Block Selection | MCU on Eclipse

  7. Pingback: Eclipse Gems, Tips & Tricks: Close and Open Projects | MCU on Eclipse

  8. Pingback: Eclipse Gems, Tips & Tricks: Code Folding | MCU on Eclipse

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.