How to comment a block in Eclipse?

EclipseCommentsHotkeys

Eclipse Problem Overview


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

Eclipse Solutions


Solution 1 - Eclipse

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

Ctrl-</kbd> 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-</kbd> 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 - Eclipse

There are two possibilities:

##Every line prepended with //##

ctrl + / to comment
ctrl + </kbd> to uncomment

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

##Complete block surrounded with block comments /*##

ctrl + shift + / to comment
ctrl + shift + </kbd> to remove

Solution 3 - Eclipse

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 - Eclipse

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 - Eclipse

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)

Solution 6 - Eclipse

I have Eclipse IDE for Java Developers Version: Juno Service Release 2 and it is -

Every line prepended with //

ctrl + / for both comment and uncomment .

Solution 7 - Eclipse

In addition, you can change Eclipse shortcut in Windows -> Preferences -> General -> Keys

change Eclipse shortcut

Solution 8 - Eclipse

For single line comment you can use Ctrl+/ and for multiple line comment you can use Ctrl + Shift + / after selecting the lines you want to comment in java editor.

On Mac/OS X you can use āŒ˜ + / to comment out single lines or selected blocks.

Solution 9 - Eclipse

for java code

if you want comments single line then put double forward slash before code of single line manually or by pressing Ctrl +/ example: //System.Out.println("HELLO");

and for multi-line comments, Select code how much you want to comments and then press

Shift+CTRL+/

Now for XML code comments use Select code first and then press Shift+CTRL+/ for both single line and multi-line comments

Solution 10 - Eclipse

For JAVA :

Single line comment:

               // this is a single line comment

To comment: Ctrl + Shift + C

To uncomment: Press again Ctrl + Shift + C

Multiple line comment:

         /* .........
            .........
            ......... */ 

First, select all the lines that you want to comment/uncomment then,

To comment: Ctrl + Shift + C

To uncomment: Press again Ctrl + Shift + C

I hope, this will work for you!

Solution 11 - Eclipse

It depends upon the version of OS - for me it works with Command + 7

Solution 12 - Eclipse

Using Eclipe Oxygen command + Shift + c on macOSx Sierra will add/remove comments out multiple lines of code

Solution 13 - Eclipse

Using Eclipse Mars.1 CTRL + / on Linux in Java will comment out multiple lines of code. When trying to un-comment those multiple lines, Eclipse was commenting the comments. I found that if there is a blank line in the comments it will do this. If you have 10 lines of code, a blank line, and 10 more lines of code, CTRL + / will comment it all. You'll have to remove the line or un-comment them in blocks of 10.

Solution 14 - Eclipse

Eclipse Oxygen with CDT, PyDev:

Block comments under Source menu

Add Comment Block Ctrl + 4

Add Single Comment Block Ctrl+Shift+4

Remove Comment Block Ctrl + 5

Solution 15 - Eclipse

I have Mac ,I was also facing problem to comment multiple line in STS

I have tried

single line comment:

command+/

Multi line comment:

control+command+/ 

Multi line uncomment:

control+command+\ 

And it was success

Solution 16 - Eclipse

For single line comment just use // and for multiline comments use /* your code here */

Solution 17 - Eclipse

Select the text you want to Block-comment/Block-uncomment.

To comment, Ctrl + 6

To uncomment, Ctrl + 8

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnakileView Question on Stackoverflow
Solution 1 - EclipseJon SkeetView Answer on Stackoverflow
Solution 2 - EclipseIkkeView Answer on Stackoverflow
Solution 3 - EclipseParesh3489227View Answer on Stackoverflow
Solution 4 - Eclipset3b4nView Answer on Stackoverflow
Solution 5 - EclipsejechterhoffView Answer on Stackoverflow
Solution 6 - EclipseURL87View Answer on Stackoverflow
Solution 7 - EclipseDaniView Answer on Stackoverflow
Solution 8 - EclipseKamranView Answer on Stackoverflow
Solution 9 - EclipseLovekush VishwakarmaView Answer on Stackoverflow
Solution 10 - Eclipsejd95View Answer on Stackoverflow
Solution 11 - EclipseManoj Kumar DharView Answer on Stackoverflow
Solution 12 - EclipseIsa SouzaView Answer on Stackoverflow
Solution 13 - EclipsecausticSandView Answer on Stackoverflow
Solution 14 - EclipseTT--View Answer on Stackoverflow
Solution 15 - Eclipseabhinav kumarView Answer on Stackoverflow
Solution 16 - EclipseLovekush VishwakarmaView Answer on Stackoverflow
Solution 17 - Eclipsepawan kumarView Answer on Stackoverflow