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"
  ...
/ -->

4
  • 3
    for single line comment you can use ctrl + /.
    – Harry Joy
    Apr 4, 2011 at 5:06
  • Possible duplicate of How to comment a block in Eclipse?
    – CubeJockey
    Mar 14, 2016 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, 2017 at 21:53
  • 1
    Any shortcuts to uncomment this type of comment /**Hello *this is second line *this is third line */
    – MashukKhan
    Aug 29, 2017 at 12:54

18 Answers 18

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.

9
  • 1
    cool ! works great for both java and xhtml file. it even uses the /* */ for multiline java codes
    – Bertie
    Apr 4, 2011 at 6:11
  • 1
    Using a swedish Keyboard the multiline is not possible since the '/' char is typed using shift + 7, is there a plugin to change the shortcuts for Swedish users? Sep 9, 2013 at 22:30
  • 1
    @DavidMårtensson it works fine for me, just press Ctrl + Shift + 7 (yielding ctrl + /), having multiple lines marked.
    – user
    Sep 13, 2013 at 9:03
  • 3
    To uncomment press Ctrl + / again if it is a single line comment and Ctrl + Shift + \ if it is a multi line comment
    – Richie
    Nov 6, 2014 at 11:28
  • 1
    @Richie ctrl + shift + \ doesnt work for me for uncommenting multiple lines
    – john
    Apr 16, 2017 at 13:48
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

4
  • 14
    Found it ! It's ctrl shift c in my eclipse, and toggle comment/uncomment works for both java code and xhtml file. But the comment on java code use // even for multiple line, and not /* */ .. but i think it's ok :-)
    – Bertie
    Apr 4, 2011 at 6:07
  • @bertie Use #4 if that's the effect you're going for.
    – arkon
    Aug 13, 2014 at 18:56
  • replace ctrl with ⌘, thanks for teaching me for teaching me how to fish! Jun 10, 2015 at 0:04
  • For multiple uncomment : Ctrl + Shift + \ (Backward Slash) : Most IMPORTANT! Mar 21, 2020 at 7:28
103

CTRL + 7

does comment/uncomment in the Java Editor.

3
  • 1
    This works for Android application. I swapped that to Ctrl+Q.
    – MrHIDEn
    Oct 24, 2014 at 10:16
  • This is a great because it's simpler than remembering two different commands for comment/un-comment!
    – levininja
    Sep 23, 2015 at 16:10
  • in contrast to most other answers this will always work (multiline comments cannot be nested) Oct 16, 2018 at 12:10
52
  1. Single line comment Ctrl + /
  2. Single line uncomment Ctrl + /

  1. Multiline comment Ctrl + Shift + /
  2. Multiline uncomment Ctrl + Shift + \ (note the backslash)
1
  • 1
    Single line stopped working in Eclipse 2021 Nov 16, 2021 at 13:03
36

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

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, 2013 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, 2013 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.

14

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

2
  • this answer actually more meaning full. gives answer for wide range of other shortcut related questions, in addition to what question is asked here Aug 13, 2013 at 5:00
  • 2
    ..but it does not answer the question because "comment" hot key is not listed in this popup.
    – WesternGun
    Jan 4, 2018 at 8: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.

1
  • yes, also works at least in .less, .css, .jsp, .xml
    – el-teedee
    Sep 7, 2018 at 23:37
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.
1
  • 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 7, 2018 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

1
  • 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, 2019 at 19:21
8

Ctrl + 7 to comment a selected text.

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 .. :)

2
  • Thanks this lead me to try cmd ⌘ + shift + c (essentially the same thing) to toggle comments on a on a mac Jun 10, 2015 at 0:04
  • Its only working for only comment and uncomment instructions. Apr 17, 2017 at 15:34
6

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

4
  • 1
    just for future reference: That key is called the "command-key".
    – katzenhut
    Nov 20, 2013 at 20:30
  • @katzenhut I would hope a programmer would know what the keys on his/her keyboard are.
    – arkon
    Aug 13, 2014 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, 2014 at 9: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, 2017 at 9: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

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.

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

1

Source -> Remove Block Comment

link

enter image description here

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.