How to comment a block in Eclipse?
Asked Answered
L

17

203

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

Laval answered 4/1, 2010 at 19:28 Comment(0)
R
289

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.

Reflate answered 4/1, 2010 at 19:29 Comment(11)
For the record: on Mac, Command-/ makes it a //, and Ctrl-Command-/ makes it a /* */. Removing comments is done with Ctrl-Command-\\ .Dasha
I tried it on a Mac. To remove // comments, I just did Command-/ again.Lucas
@Griffin Me neither, but Ctrl+Shift+\ works for /* */ style comments. // style comments can be removed with Ctrl+/Reinaldoreinaldos
@IOS_Dev: I suspect that's a general Mac/PC thing. Will edit to note.Reflate
Also found that other aware editors put the "appropriate" comment tag in for their syntax. Doing work in Puppet manifests gets me a "#".Keener
Ctrl-Shift-/ for me only collapse all methods and not related to commentHallux
@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.Reflate
IDK why, but the command `ctrl + shift + ` to remove block comment doesn't work for me eclipse mars for some reason.Horselaugh
Is there a way to make the // appear next to the code instead of the beggining of the line?Ginnifer
@Katu: I don't know offhand, but I'd look at your code formatting options.Reflate
This will also work great with Python under PyDev in Eclipse, by appending a hashtag to the beginning of each line. Do note that, when a block of lines which contains both comments and code is commented this way, the already commented lines become "double" commented. One must select only commented lines if one wants to uncomment them using the key binding. It is also possible to modify these keyboard shortcuts by going to Window -> Preferences -> General -> Keys and typing in "comment" to filter.Jester
C
74

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

Carrelli answered 4/1, 2010 at 19:30 Comment(3)
a have code folding enabled so ctrl + / enables-disables that. For me control + shift + 7 give me // comment-uncomment .Cursed
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!Nonplus
The command to prepend every line also works in python; it just adds or removes #.View
S
32

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.

Shane answered 9/4, 2014 at 7:18 Comment(0)
B
10

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.

Bahadur answered 28/8, 2012 at 17:15 Comment(2)
Same happened to me, seems it should be some interaction with a plugin.Wigwam
+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!Commemoration
E
7

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)
Emblaze answered 27/3, 2014 at 16:31 Comment(0)
S
6

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 .

Seventeenth answered 12/11, 2013 at 0:22 Comment(0)
C
5

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

change Eclipse shortcut

Couchman answered 2/11, 2016 at 21:34 Comment(0)
B
3

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.

Bart answered 21/3, 2014 at 10:38 Comment(0)
I
3

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

Imbrication answered 2/4, 2014 at 18:28 Comment(0)
U
3

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!

Unpile answered 13/1, 2019 at 20:0 Comment(1)
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?Yl
D
2

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

Directly answered 3/5, 2019 at 13:12 Comment(1)
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.Floruit
S
1

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

Spirometer answered 19/8, 2017 at 12:35 Comment(0)
W
0

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.

Whereon answered 27/11, 2015 at 11:39 Comment(0)
C
0

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

Carvalho answered 30/9, 2017 at 19:12 Comment(0)
N
0

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

Noleta answered 5/10, 2020 at 7:51 Comment(0)
I
-1

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

Imbrication answered 15/9, 2016 at 12:8 Comment(1)
Please note that the question is if Eclipse has a hotkey, not what the syntax itself is.Magnanimous
O
-1

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

To comment, Ctrl + 6

To uncomment, Ctrl + 8

Oxidate answered 18/1, 2018 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.