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.
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:
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
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:
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
Changing the #if 0 to a #if 1 toggles the ‘comment’. That style works always and does not mess with nested normal comments.
That way I can easily switch between different alternatives:
Happy commenting 🙂
Links
- Eclipse Gems, Tips & Tricks: Importing Projects
- Eclipse Gems, Tips & Tricks: Close and Open Projects
- Eclipse Gems, Tips & Tricks: Text Block Selection
- Eclipse Gems, Tips & Tricks: Project Filter
- Eclipse Gems, Tips & Tricks: Black Theme
- Eclipse Gems, Tips & Tricks: Faster Debugger Start
- Eclipse Gems, Tips & Tricks: RT(F)M
- Eclipse Gems, Tips &Tricks: Show in System Explorer
- Eclipse Gems, Tips & Tricks: Open Declaration
- Eclipse Gems, Tips & Tricks: Code Completion
- Eclipse Gems, Tips & Tricks: Highlight Usage and Definition
- Eclipse Gems, Tips & Tricks: Block Comments
- Eclipse Gems, Tips & Tricks: Working Sets
I always use the #if form, it gives a place to write a comment as to why it’s commented out, too.
LikeLiked by 1 person
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!).]
LikeLiked by 1 person
Yes, I’m missing as well the #region from the C# world. And as you I’m using the #if 1 way to get a similar function.
LikeLike
Pingback: Eclipse Gems, Tips & Tricks: Working Sets | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Highlight Usage and Definition | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Code Completion | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Faster Debugger Start | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Project Filter | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Text Block Selection | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Close and Open Projects | MCU on Eclipse
Pingback: Eclipse Gems, Tips & Tricks: Code Folding | MCU on Eclipse