283

I thought this would be easy to achieve, but so far I haven't found solutions for comment/uncomment shortcut on both Java class editor and jsf faceted webapp XHTML file editor :

  1. to quickly comment/uncomment a line (like ctrl + d is for removing single line)
  2. being able to choose multiple lines and comment/uncomment it

For example :

single line java code, from :

private String name;

into

//private String name;

multiple line java code, from :

private String name;
private int age;

into

/*private String name;
private int age;*/

single line xhtml code, from :

<h:inputText ... />

into

<!-- h:inputText ... / -->

multiple line xhtml code, from :

<h:inputTextarea
  rows="xx"
  cols="yy"
  ...
/>

into

<!-- h:inputTextarea
  rows="xx"
  cols="yy"
  ...
/ -->

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Bertie
  • 17,277
  • 45
  • 129
  • 182
  • 3
    for single line comment you can use `ctrl + /`. – Harry Joy Apr 04 '11 at 05:06
  • Possible duplicate of [How to comment a block in Eclipse?](http://stackoverflow.com/questions/2001586/how-to-comment-a-block-in-eclipse) – CubeJockey Mar 14 '16 at 15:04
  • Mac shortcuts: // for single line comments toggle Cmd + / to insert and remove. /* multiline */ use Cntrl + Cmd + // to comment and Cntrl + Cmd + \\ to uncomment. Works with Eclipse Neon too! – lft93ryt Jan 26 '17 at 21:53
  • 1
    Any shortcuts to uncomment this type of comment /**Hello *this is second line *this is third line */ – MashukKhan Aug 29 '17 at 12:54

18 Answers18

373

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.

rhinoinrepose
  • 2,110
  • 2
  • 19
  • 26
Richie
  • 9,006
  • 5
  • 25
  • 38
195

Use

Ctrl + SHIFT + L which will open a list of all major shortcuts for eclipse.

  1. For single line java code comment and uncomment : Ctrl + / (Forward Slash)

  2. For multiple line java code comment : Ctrl + Shift + / (Forward Slash) and

    Multiline uncomment : Ctrl + Shift + \ (Backslash)

  3. For single line xhtml code comment/uncomment : Ctrl + Shift + c

  4. For multiple line xhtml code comment : Ctrl + Shift + / (Forward Slash)

    For multiple uncomment : Ctrl + Shift + \ (Backward Slash)

For Mac user it will be: instead of Ctrl

drac_o
  • 427
  • 5
  • 11
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
103

CTRL + 7

does comment/uncomment in the Java Editor.

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
Daniel
  • 27,718
  • 20
  • 89
  • 133
52
  1. Single line comment Ctrl + /
  2. Single line uncomment Ctrl + /

  1. Multiline comment Ctrl + Shift + /
  2. Multiline uncomment Ctrl + Shift + \ (note the backslash)
James Oravec
  • 19,579
  • 27
  • 94
  • 160
noelyahan
  • 4,195
  • 1
  • 32
  • 27
37

An easier way is to press Ctrl + Shift + C, just like in Code::Blocks

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
Denis
  • 371
  • 3
  • 2
  • 2
    Thank you. In my keyboard I have to press **Alt Gr** to access **/** and the common shortcuts don't work (ctrl + / and ctrl + shift + /). This shortcut is what I needed. – zezim Jan 30 '13 at 11:42
  • 1
    This is awesome because it uncomments XML. When I use Cmd-/ to uncomment XML/HTML, I end up with a nested comment. – David Mann Aug 20 '13 at 14:42
20

Select the code you want to comment, then use Ctr + / to comment and Ctrl + / also to uncomment. It may not work for all types of source files, but it works great for Java code.

drac_o
  • 427
  • 5
  • 11
squawknull
  • 5,131
  • 2
  • 17
  • 27
14

In eclipse Pressing Ctrl + Shift + L, will list all the shortcuts.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
sk85
  • 407
  • 4
  • 17
  • this answer actually more meaning full. gives answer for wide range of other shortcut related questions, in addition to what question is asked here – DeltaCap019 Aug 13 '13 at 05:00
  • 2
    ..but it does not answer the question because "comment" hot key is not listed in this popup. – WesternGun Jan 04 '18 at 08:45
10

Ctrl + Shift + C . Works great on .java, .xhtml, .properties and maybe on others but I only tested these ones. Can comment single or multiple lines.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
ihebiheb
  • 3,673
  • 3
  • 46
  • 55
10

Comments In Java class


  1. Toggle/Single line Comment ( Ctrl+/ ) - Add/remove line comments (//…) from the current line.
  2. Add Block Comment ( Ctrl+Shift+\ ) - Wrap the selected lines in a block comment (/*… */).
  3. Remove Block Comment ( Ctrl+Shift+/ ) - Remove a block comment (/*… */) surrounding the selected lines.
  4. Add Javadoc Comment ( Alt+Shift+J ) - Add a Javadoc comment to the active field/method/class.

Comments In HTML/XML/Config file


  1. Add Block Comment ( Ctrl+Shift+/ ) - Wrap the selected lines in a block comment (< !-- -->).
  2. Remove Block Comment (Ctrl+Shift+\) - Remove a block comment (< !-- -->) surrounding the selected lines.
drac_o
  • 427
  • 5
  • 11
Deepak Gupta
  • 1,075
  • 15
  • 21
  • HAHA, by desesperally trying to uncomment in XML with `CTRL+SHIFT+\ ` , I discovered a new one `CTRL+ALT-GR+8` that seems to duplicate (vertical split) current edited file in 2 editors. Then you can see head and bottom of same file at same time – el-teedee Sep 07 '18 at 23:42
9

For those who like to customize things (and screw them up), some tips:

In "Preferences" - "General" - "Keys", type "comment" to get a full list of comment key combination in all editors. Be sure only to edit, not copy commands(because you cannot erase created ones).

For HTML files, look for those with scope "Editing HTML files". The names are confusing, yes, but the description shows more or less when each and every key is used.

At last, I am only able to define block comment in HTML here. It works for both HTML code and Javascript code(add at the beginning and the end, <!-- and --> if is HTML, and add /* and */ if is Javascript). (I remember once there may be a "line comment" key (add comments for every line) but now it is not there anymore. If someone knows how to find it and tell me, I will be very grateful.) enter image description here The category must be Edit. Source does not work.

EDIT:

I found how to configure XML Toggle comment! The category should be Edit, too. Then you can toggle comment in **XML Editor(the one with "Design" and "Source" tabs).

enter image description here

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 1
    On Maverics / Eclipse oxygen I got it to work by setting binding = command + shift + / , when = 'editing javascript source' where category = source and command = 'add block comment' . – jkb016 Oct 30 '19 at 19:21
8

Ctrl + 7 to comment a selected text.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Sangimed
  • 900
  • 8
  • 16
8

A simple way of doing is to press Ctrl + Shift + C, on the lines of your code.

For comment and for uncomment do same .. :)

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
Andy
  • 189
  • 1
  • 2
6

For a Mac it is the following combination: Cmd + /

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
bofredo
  • 2,348
  • 6
  • 32
  • 51
  • 1
    just for future reference: That key is called the "command-key". – katzenhut Nov 20 '13 at 20:30
  • @katzenhut I would hope a programmer would know what the keys on his/her keyboard are. – arkon Aug 13 '14 at 18:59
  • @b1nary.atr0phy - well, when i wrote this comment the answerer referred to the key in question as the "mac-key". so what does that tell you? luckily, somebody edited it since... – katzenhut Aug 14 '14 at 09:49
  • My boss forced me to use a Mac-Book back then. Not my fault i didn't know the buttons name :-) – bofredo Apr 28 '17 at 09:49
4

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.

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

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
Kamran
  • 465
  • 6
  • 9
4

Single comment ctrl + / and also multiple line comment you can select multiple line and then ctrl + /. Then, to remove comment you can use ctrl + c for both single line and multiple line comment.

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
GB_Bhayani ツ
  • 368
  • 1
  • 20
3

You can toggle the comment on one line or selection by using the shortcut Ctrl + / This adds/removes the // infront of the code line

You can block comment /* */ using the Ctrl + Shift + / eclipse shortcut

You can find a complete list of useful eclipse shortcuts here http://javatutorial.net/eclipse-shortcuts

filip_j
  • 993
  • 1
  • 14
  • 22
1

Source -> Remove Block Comment

link

enter image description here

busetekin
  • 834
  • 7
  • 12
1

The Ctrl + / stopped working for me in Eclipse 2021, due to conflicts of the same command. If you go into Eclipse Preferences Generic Keys and then filter by "comment" you can then click on each Toggle Comment to see if there are any conflicts. Remove conflicts by clicking on the command and clicking Unbind Command, and this fixed it for me: enter image description here