[java] Eclipse comment/uncomment shortcut?

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

This question is related to java eclipse keyboard-shortcuts shortcut

The answer is



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.

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


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

For comment and for uncomment do same .. :)


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.


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


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.


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.


Source -> Remove Block Comment

link

enter image description here


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


Ctrl + 7 to comment a selected text.


  1. Single line comment Ctrl + /
  2. Single line uncomment Ctrl + /

  1. Multiline comment Ctrl + Shift + /
  2. Multiline uncomment Ctrl + Shift + \ (note the backslash)

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


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


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


CTRL + 7

does comment/uncomment in the Java Editor.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to keyboard-shortcuts

Collapse all methods in Visual Studio Code Is there a keyboard shortcut (hotkey) to open Terminal in macOS? Jupyter/IPython Notebooks: Shortcut for "run all"? Any way (or shortcut) to auto import the classes in IntelliJ IDEA like in Eclipse? How do I duplicate a line or selection within Visual Studio Code? How do I search for files in Visual Studio Code? OS X Terminal shortcut: Jump to beginning/end of line window.close() doesn't work - Scripts may close only the windows that were opened by it Comment shortcut Android Studio Column/Vertical selection with Keyboard in SublimeText 3

Examples related to shortcut

Collapse all methods in Visual Studio Code How do I create a shortcut via command-line in Windows? window.close() doesn't work - Scripts may close only the windows that were opened by it How to automatically generate getters and setters in Android Studio How to update gradle in android studio? Is there a short cut for going back to the beginning of a file by vi editor? How to create a shortcut using PowerShell How do I call a function twice or more times consecutively? Eclipse comment/uncomment shortcut? 2D array values C++