Eclipse comment/uncomment shortcut?

2020-01-27 09:28发布

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

16条回答
We Are One
2楼-- · 2020-01-27 09:32

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

查看更多
Luminary・发光体
3楼-- · 2020-01-27 09:33

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

查看更多
老娘就宠你
4楼-- · 2020-01-27 09:37

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

查看更多
放荡不羁爱自由
5楼-- · 2020-01-27 09:39

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

查看更多
Lonely孤独者°
6楼-- · 2020-01-27 09:41

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

查看更多
走好不送
7楼-- · 2020-01-27 09:42

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

For comment and for uncomment do same .. :)

查看更多
登录 后发表回答