How to comment a block in Eclipse?

345,662

Solution 1

Ctrl-/ to toggle "//" comments and Ctrl-Shift-/ to toggle "/* */" comments. At least for Java, anyway - other tooling may have different shortcuts.

Ctrl-\ will remove a block of either comment, but won't add comments.

Note: As for Eclipse CDT 4.4.2, Ctrl-Shift-/ will not uncomment a "/* */" block comment. Use Ctrl-Shift-\ in that case.

EDIT: It's Ctrl on a PC, but on a Mac the shortcuts may all be Cmd instead. I don't have a Mac myself, so can't easily check.

Solution 2

There are two possibilities:

Every line prepended with //

ctrl + / to comment
ctrl + \ to uncomment

Note: on recent eclipse cdt, ctrl + / is used to toggle comments (and ctrl + \ has no more effect)

Complete block surrounded with block comments /*

ctrl + shift + / to comment
ctrl + shift + \ to remove

Solution 3

For Eclipse Editor

For Single Line (Toggle Effect)

Comment : Ctrl+Shift+c

Uncomment: Ctrl+Shift+c

For Multiple Lines (Toggle Effect) (Select the lines you want to comment)

comment : Ctrl+Shift+c

Uncomment: Ctrl+Shift+c

It is for all html , css , jsp , java . It gives toggle effect.

Solution 4

I came here looking for an answer and ended up finding it myself, thanks to the previous responses.

In my particular case, while editing PHP code on Eclipse Juno, I have found that the previous commands won't work for me. Instead of them, I should press Ctrl+ 7 (on the superior number key) to obtain the double bar comment ("//"). There's no way I can comment them with the previous mentioned key combinations.

Solution 5

As other answers pointed out, the following shortcuts are defined by default (I'm referring to editing java source - shortcut bindings can be found in eclipse Window>Preferences, under 'General'/'Keys', search for 'comment'):

  • to add a block comment, the shortcut (binding) is: Ctrl + Shift + /
  • to remove a block comment, the shortcut (binding) is: Ctrl + Shift + \

Unfortunately, these shortcuts did not work for me (on Eclipse Java EE IDE for Web Developers, version: Juno Service Release 2).

The reason, I think, is my keyboard layout (QWERTZ keyboard layout used in Germany - see here for further information), where '/' actually has to be written via 'Shift + 7'.

I therefore had to change the shortcut bindings for comments (just did it for editing java source) in the eclipse preferences (under 'General'/'Keys') as follows:

  • Change the binding of command 'Add Block Comment' (when 'Editing Java Source') from 'Ctrl + Shift + /' to 'Ctrl + Shift + 7'
  • Change the binding of command 'Remove Block Comment' (when 'Editing Java Source') from 'Ctrl + Shift + \' to 'Ctrl + Shift + 8'
  • Unbind the binding 'Ctrl + /' for command 'Toggle Comment' (when 'Editing Java Source'); instead of assigning a new binding, I simply unbound this shortcut, as there already was one with Ctrl+7 and one with Ctrl+Shift+C (which both work for me)
Share:
345,662

Related videos on Youtube

snakile
Author by

snakile

Updated on October 05, 2020

Comments

  • snakile
    snakile over 3 years

    Does Eclipse have a hot key to comment a block? and to uncomment a block?

  • yvesb
    yvesb almost 12 years
    For the record: on Mac, Command-/ makes it a //, and Ctrl-Command-/ makes it a /* */. Removing comments is done with Ctrl-Command-\\ .
  • Yaroslav
    Yaroslav over 11 years
    Same happened to me, seems it should be some interaction with a plugin.
  • roho
    roho over 11 years
    a have code folding enabled so ctrl + / enables-disables that. For me control + shift + 7 give me // comment-uncomment .
  • ArtOfWarfare
    ArtOfWarfare over 11 years
    +1 Using Ctrl + 7 (on the number keys above the alphabet keys) toggles the currently selected block with //, even in Java code. Kind of annoying that it'll double comment lines, which potentially makes it impossible to quickly uncomment them all if one of the lines has more comments than the others... oh well, good enough!
  • Noah Lavine
    Noah Lavine over 11 years
    I tried it on a Mac. To remove // comments, I just did Command-/ again.
  • Inversus
    Inversus almost 11 years
    @Griffin Me neither, but Ctrl+Shift+\ works for /* */ style comments. // style comments can be removed with Ctrl+/
  • Jon Skeet
    Jon Skeet almost 11 years
    @IOS_Dev: I suspect that's a general Mac/PC thing. Will edit to note.
  • mohbandy
    mohbandy over 10 years
    I would advise people against using the block comment shortcuts as the implementation isn't, shall we say "smart". If you have block comments within the section you plan to block comment out, the start/end tags will be removed! So when you try to undo the commenting you now have to insert the missing "/* */" tags. At least this is what i see using Juno under Linux. Test it out first!
  • 8None1
    8None1 over 10 years
    Also found that other aware editors put the "appropriate" comment tag in for their syntax. Doing work in Puppet manifests gets me a "#".
  • ashraf mohammed
    ashraf mohammed over 9 years
    Ctrl-Shift-/ for me only collapse all methods and not related to comment
  • Jon Skeet
    Jon Skeet over 9 years
    @ashrafmohammed: Under "Preferences", go to the keyboard shortcut (type Keys into the filter) and then look for comments. Maybe you've got different key bindings to the default.
  • Lucky
    Lucky over 7 years
    IDK why, but the command `ctrl + shift + ` to remove block comment doesn't work for me eclipse mars for some reason.
  • S.L. Barth
    S.L. Barth over 7 years
    Please note that the question is if Eclipse has a hotkey, not what the syntax itself is.
  • Louis Langholtz
    Louis Langholtz over 5 years
    Greetings and welcome to StackOverflow. Generally answers for questions which already have highly voted answers are expected to add somehow to what's already provided. Is there a way you can update your answer to illuminate how it adds something new to what's already been said?
  • Antonio Rodríguez
    Antonio Rodríguez almost 5 years
    Similar here, using Windows 10 and a Spanish keyboard I need to type Ctrl + 7 to comment/uncomment code. Shift+7 produces /, there is no direct key to type / unless you have numeric keyboard.
  • Aaron Swan
    Aaron Swan almost 3 years
    The command to prepend every line also works in python; it just adds or removes #.
  • Katu
    Katu almost 3 years
    Is there a way to make the // appear next to the code instead of the beggining of the line?
  • Jon Skeet
    Jon Skeet almost 3 years
    @Katu: I don't know offhand, but I'd look at your code formatting options.