Eclipse Zone is brought to you in partnership with:

Having worked with a number of languages and tools over the years, I've learned one thing: work with the tools you have, learn them, master them, then make life easier by sharing the knowledge. Whether Java, C++, VB, or COBOL, I've always applied the same philosophy with success. I love studying manuals and being a keyboard addict, the list of keyboard shortcuts is often my favorite page. Byron is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. View Full User Profile

Add Comments and Javadocs in Eclipse With a Single Keystroke

  • submit to reddit

When you want to work with comments in Eclipse, you could use the slow way of moving to the start of the line, pressing // and then repeating this for all the lines you have.

Or you could use the quick way of adding a comment with a single keystroke no matter where the cursor’s positioned in the statement.

The same goes for Javadocs – there are just too many things to type before you can start commenting the good stuff. That’s why Eclipse also has a shortcut that let’s you add Javadoc to a field, method or class.

Keyboard shortcuts for comments and JavaDocs

Here are the keyboard shortcuts for manipulating comments.

Command Description
Ctrl+/ Toggle Comment Add/remove line comments (//…) from the current line. The position of the cursor can be anywhere on the line. Works with multiple selected lines as well.
Ctrl+Shift+/ Add Block Comment Wrap the selected lines in a block comment (/*… */).
Ctrl+Shift+\ Remove Block Comment Remove a block comment (/*… */) surrounding the selected lines.
Alt+Shift+J Add Javadoc Comment Add a Javadoc comment to the active field/method/class. See the notes below for more details on where to position the cursor.

Bear the following in mind when using Add Javadoc comment (Alt+Shift+J):

  • To add a comment to a field, position the cursor on the field declaration.
  • To add a comment to a method, position the cursor anywhere in the method or on its declaration.
  • To add a comment to a class, the easiest is to position the cursor on the class declaration. Also works if you’re in the class, but not in a method, field or nested type.
  • The Javadoc comment inserted is based on the Code Templates defined  under Window > Preferences > Java > Code Style > Code Templates. If you expand the Comments section, you can change the default for Fields, Methods, Types (eg. classes), etc.

Here’s a video to give you an idea of how fast and easy it is to add/remove comments using these shortcuts. The video shows toggling of single line comments, block comments and also adding a Javadoc comment to the method and class.

Once I’ve commented out lines, I often find myself copying them and moving them around (eg. to try different variations of the code). You can do this faster by moving and copying lines using with a single keystroke.

You can also have Eclipse format the comments whenever you save, saving you formatting time.



Published at DZone with permission of Byron M, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Sancar G. replied on Thu, 2010/05/06 - 4:07am

Shouldn't Ctrl+/ be rather Ctrl+7? Because Ctrl+/ is Ctrl+Shift+7.

Strangely both do the same for me in eclipse, adding/removing line comments (//). 

Also Ctrl+Shift+c does the same, but is easier to type for me.

Sancar G. replied on Thu, 2010/05/06 - 4:35am in response to: Sancar G.

Didn't think of different keyboard layouts, I am using a German one, so there is the difference

Shams Mahmood replied on Thu, 2010/05/06 - 8:49am

Isn't ctrl + <X>, two keystrokes ;)

Byron M replied on Thu, 2010/05/06 - 5:11pm in response to: Sancar G.

Sancar, thanks for pointing that out. You're right that there are multiple shortcuts defined by default for Toggle Comment. If you go to Window > Preferences > General > Keys and search for Toggle Comment, you'll see that Ctrl+/, Ctrl+7 and Ctrl+Shift+C are bound to the same command.

On the QWERTY keyboard, / is normally close to Ctrl on the bottom right, so is easier for me to press than Ctrl+Shift+C, but I would see how you'd prefer this key over Ctrl+7 in the German layout.

Thanks again for pointing it out - I'll update the post on my blog to mention this (probably do the DZone one later as well).

Byron M replied on Thu, 2010/05/06 - 5:28pm in response to: Shams Mahmood

Shams, not if you press it fast enough... Nah, I keep jumping between "shortcut" "keyboard shortcut" and "keystroke" and this time it was keystroke. And hotkey seems to have gone out of fashion with the crowd I hang out with, so that's out.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.