在 Eclipse 中取消注释多行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21724456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:01:36  来源:igfitidea点击:

Uncomment multiline code in eclipse

eclipse

提问by Sundeep1501

Yes, this could be duplicate of Eclipse comment/uncomment shortcut?But, answers given their are not working for my case.

是的,这可能是Eclipse 注释/取消注释快捷方式的副本但是,给出的答案不适用于我的案例。

I have formatted java code and When I select and use ctrl + shift + /it looks like

我已经格式化了 java 代码,当我选择和使用ctrl + shift + /它时,它看起来像

/*
 * if (isKilled) { Log.i("TAG", "Killed"); }
 */

But when I use ctrl + shift + /for above selected code, eclipse is not un-commenting my code.

但是当我使用ctrl + shift + /上面选择的代码时,eclipse 并没有取消注释我的代码。

I tried ctrl + shift + \, ctrl + shift + /and ctrl + \. Nothing worked. I always manually remove *s even if commented code is very big.

我试过了ctrl + shift + \ctrl + shift + /而且ctrl + \。没有任何效果。即使注释代码非常大,我也总是手动删除 *s 。

How to do this? I also want *s to be removed.

这该怎么做?我也希望 *s 被删除。

回答by Pratik Roy

Try using Ctr+Shift+CThis should work

尝试使用 Ctr+Shift+C这应该有效

回答by greg-449

For me Remove Block Comment(Ctrl+ Shift+ \) works but only if there is code on the first line of the comment, so your example does not work but:

对我来说Remove Block CommentCtrl+ Shift+ \)有效,但前提是评论的第一行有代码,所以你的例子不起作用,但是:

/* if (isKilled) { Log.i("TAG", "Killed"); }
 */

does work.

确实有效。

回答by CITBL

One partial solution to this is to disable block comment formatting.

对此的一种部分解决方案是禁用块注释格式。

If you do that, Eclipse will not add * for intermediate lines in multiline block comments when you format the code. And so you will be able to remove such block comments with Ctrl+Shift+\or just by removing /*and */

如果这样做,当您格式化代码时,Eclipse 将不会为多行块注释中的中间行添加 *。因此,您将能够使用Ctrl+Shift+\或仅通过删除/*和删除此类块注释*/

In Eclipse for PHP (Windows), in the main menu Window->Preferences->PHP->CodeStyle->Formatter->Edit->Comments
Uncheck "Enable block comment formatting"

在 Eclipse for PHP (Windows) 中,在主菜单Window->Preferences->PHP->CodeStyle->Formatter->Edit->Comments 中
取消选中“启用块注释格式”

In other versions of Eclipse this option also exists but under a different path.

在 Eclipse 的其他版本中,此选项也存在,但位于不同的路径下。

回答by maihe

I am having this problem since ever. Always had in mind that Ctrl + Shift + Cwould away be the right way to comment but it never "uncomment".

我一直有这个问题。始终牢记Ctrl + Shift + C将是正确的评论方式,但它永远不会“取消评论”。

Instead just change for to use Ctrl + /(for groups also) , or Ctrl + Shift + /for /**/ if you prefer.

相反,只需更改为使用Ctrl + /(也适用于组),或Ctrl + Shift + /用于 /**/ 如果您愿意。

It never fails, do not stress yourself again.

它永远不会失败,不要再给自己压力。

回答by Dean Woods

  1. Select the lines of code u wanna comment/uncomment.

  2. Then hit "Ctrl + / " to comment/uncomment.

  1. 选择您想要注释/取消注释的代码行。

  2. 然后点击“Ctrl + /”评论/取消评论。

回答by harsha

In Eclipse, To add and remove single line comments,

在 Eclipse 中,要添加和删除单行注释,

CNTRL+SHIFT+C

CTRL+SHIFT+C

Example:

例子:

// if(i==1){
// ...
// }

To add and remove multi line comments,

要添加和删除多行注释,

CNTRL+SHIFT+/ --> add CNTRL+SHIFT+\ --> remove

CNTRL+SHIFT+/ --> 添加 CNTRL+SHIFT+\ --> 删除

Example:

例子:

/* if(i==1){
   ...
}
*/

回答by Dexter

It is basically a case when save actionsis enabled in eclipse. When you put block comments using "Crtl + Shift + /" or by typing yourself as:

这基本上是在 Eclipse 中启用保存操作的情况。当您使用“Crtl + Shift + /”或通过键入以下内容来放置块注释时:

/*
 line 1
 line 2
 line 3
*/

and save it, the editor formats it to

并保存它,编辑器将其格式化为

 /*
 * line 1
 * line 2
 * line 3
 */

* are added on each intermediate lines. This results "Crtl + Shift + \"to fail to remove those "*"before the lines.

* 在每个中间行上添加。这导致 "Crtl + Shift + \"无法删除行前的那些"*"

The solution is:

解决办法是:

1- Use "Crtl + Shift + C" to comment and uncomment (toggle). Preferable one.

1- 使用“Crtl + Shift + C”来注释和取消注释(切换)。首选之一。

2- If you don't want to use "Crtl + Shift + C". This is a hack actually. When you save, the editor formats the code like:

2- 如果您不想使用“Crtl + Shift + C”。这实际上是一个黑客。保存时,编辑器将代码格式化为:

 /*
  * line 1
  * line 2
  * line 3
  */

You intermediately undo (Crtl + Z) it. The editor will put it back to earlier stage but code is saved like:

你中间撤消(Crtl + Z)它。编辑器会将其放回早期阶段,但代码保存如下:

/*
 line 1
 line 2
 line 3
*/

Now you can use "Crtl + Shift + \" to uncomment when needed. The * won't trouble you :)

现在您可以在需要时使用“Crtl + Shift + \”取消注释。* 不会打扰你:)

回答by Ram

This might not be as easy. But still it works! I use Ctrl+Fto get the Find and Replace window, check the Regular expressions check box and use this regex ^(\s*)/?\*/?(.*)$and \1\2in the replace part and click "Replace All". If you want to uncomment only one commented code part, you can select that area and use the radio button "Selected lines" in the scope part.

这可能没那么容易。但它仍然有效!我使用Ctrl+F获取查找和替换窗口,选中正则表达式复选框并使用此正则表达式^(\s*)/?\*/?(.*)$\1\2在替换部分中单击“全部替换”。如果您只想取消注释一个已注释的代码部分,您可以选择该区域并使用范围部分中的单选按钮“选定的行”。

Regex break up ^ #denotes start of the line. To avoid matching * in other part of the code. ( #first group. To preserve the indentation \s* #selects the tab/space. The indentation ) #first group is closed /? matches the forward slash in the first line of comment \* matches the star(Asterik) in the comments. backward slash is used as a delimiter /? matches the forward slash in the last line of comment ( #second group. To preserve the indentation .* # the actual code to be uncommented ) #second group is closed $ # till the end of the line

正则表达式分手 ^ #denotes start of the line. To avoid matching * in other part of the code. ( #first group. To preserve the indentation \s* #selects the tab/space. The indentation ) #first group is closed /? matches the forward slash in the first line of comment \* matches the star(Asterik) in the comments. backward slash is used as a delimiter /? matches the forward slash in the last line of comment ( #second group. To preserve the indentation .* # the actual code to be uncommented ) #second group is closed $ # till the end of the line

Hope this helps!

希望这可以帮助!