是否有在 Xcode 中进行块注释的快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13212288/
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
Is there a shortcut to make a block comment in Xcode?
提问by limp_chimp
I'm writing ANSI-compatible C code, and hence I can't use the line (//
) comment. I'm using Xcode. In Sublime Text and Eclipse, and I think most other IDEs, there are separate keyboard shortcuts for line comments and block comments (/**/
). However, I don't see that in Xcode - in fact, I don't even see a menu option to add a block comment. Is it simply not supported in Xcode? That would certainly seem to be a lame decision if so.
我正在编写与 ANSI 兼容的 C 代码,因此我不能使用 ( //
)行注释。我正在使用 Xcode。在 Sublime Text 和 Eclipse 以及我认为大多数其他 IDE 中,行注释和块注释都有单独的键盘快捷键 ( /**/
)。但是,我在 Xcode 中没有看到 - 事实上,我什至没有看到添加块注释的菜单选项。Xcode 根本不支持它吗?如果是这样,那肯定是一个蹩脚的决定。
回答by Stefan Vasiljevic
Try command+ /. It works for me.
试试command+ /。这个对我有用。
So, you just highlight the block of code you want to comment out and press those two keys.
因此,您只需突出显示要注释掉的代码块,然后按这两个键即可。
回答by Nikola Milicevic
UPDATE:
更新:
Since I was lazy, and didn't fully implement my solution, I searched around and found BlockComment for Xcode, a recently released plugin (June 2017). Don't bother with my solution, this plugin works beautifully, and I highly recommend it.
由于我很懒,没有完全实现我的解决方案,我四处搜索并找到了BlockComment for Xcode,这是一个最近发布的插件(2017 年 6 月)。不要打扰我的解决方案,这个插件工作得很好,我强烈推荐它。
ORIGINAL ANSWER:
原始答案:
None of the above worked for me on Xcode 7and 8, so I:
以上都不适合我在Xcode 7和8 上,所以我:
- Created Automatorservice using AppleScript
- Make sure "Output replaces selected text" is checked
Enter the following code:
on run {input, parameters} return "/*\n" & (input as string) & "*/" end run
- 使用AppleScript创建Automator服务
- 确保“输出内容替换选定的文本”被选中
输入以下代码:
on run {input, parameters} return "/*\n" & (input as string) & "*/" end run
Now you can access that service through Xcode - Services menu, or by right clicking on the selected block of code you wish to comment, or giving it a shortcut under System Preferences.
现在,您可以通过 Xcode - Services 菜单访问该服务,或者通过右键单击要注释的所选代码块,或在 System Preferences 下为其提供快捷方式。
回答by Beninho85
Now with xCode 8 you can do:
现在使用 xCode 8,您可以执行以下操作:
?+ ?+ /
?+ ?+/
to auto-generate a doc comment.
自动生成文档注释。
Source: https://twitter.com/felix_schwarz/status/774166330161233920
来源:https: //twitter.com/felix_schwarz/status/774166330161233920
回答by fzwo
There is now an Xcode plugin that allows this: CComment.
现在有一个 Xcode 插件允许这样做:CComment。
The easiest way to install this is to use the amazing Alcatraz plugin manager for Xcode.
安装它的最简单方法是使用令人惊叹的 Xcode 的 Alcatraz 插件管理器。
EDITApple has sadly (and wrongly, IMHO) retired the old plugin model with Xcode 8. The new plugin system is quite limited, but should allow development of a plugin like this again. For anyone interested in doing this, watch WWDC 2016 session 414. Also, please file radarsfor API for plugins you'd like to write or see.
编辑Apple 遗憾地(并且错误地,恕我直言)淘汰了带有 Xcode 8 的旧插件模型。新插件系统非常有限,但应该允许再次开发这样的插件。对于任何对此感兴趣的人,请观看WWDC 2016 session 414。另外,请为您想编写或查看的插件的 API归档雷达。
回答by Baig
UPDATE: Xcode 8 Update
更新:Xcode 8 更新
Now with xcode 8 you can do:
现在使用 xcode 8,您可以执行以下操作:
?+ ?+ /
?+ ?+/
Note: Below method will not work in xcode version => 8
注意:以下方法不适用于 xcode 版本 => 8
Very simple steps to add Block Comment functionality to any editor of mac OS X
将块注释功能添加到 mac OS X 的任何编辑器的非常简单的步骤
- Open Automator
- Choose Services
- Search Run Shell Scriptand double click it
- 打开自动机
- 选择服务
- 搜索Run Shell Script并双击它
Add the below applescript in textarea
在textarea中添加以下applescript
awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'
awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'
- Save script as
Block Comment
- 将脚本另存为
Block Comment
Add a keyboard shortcut
添加键盘快捷键
Open System Preference > Keyboard > Shortcuts, add new shortcut by clicking +
and right the same name i.e. Block Comment
as you given to applescript in the 4th step. Add your Keyboard Shortcutand click Addbutton.
打开System Preference > Keyboard > Shortcuts,通过单击+
并右键添加新的快捷方式,即Block Comment
与您在第 4 步中给 applescript相同的名称。添加您的键盘快捷键,然后单击添加按钮。
Now you should be able to use block comment in Xcode or any other editor, select some text, use your shortcut key to block comment any line of code or right click, the context menu, and the name you gave to this script should show near the bottom.
现在您应该能够在 Xcode 或任何其他编辑器中使用块注释,选择一些文本,使用快捷键来阻止注释任何代码行或右键单击,上下文菜单以及您为此脚本提供的名称应显示在附近底部。
回答by A. Buksha
I modified the code of Nikola Milicevica little bit so it also remove comment block if code is already commented:
我稍微修改了Nikola Milicevic的代码,所以如果代码已经被注释,它也会删除注释块:
on run {input, parameters}
repeat with anInput in input
if "/*" is in anInput then
set input to replaceText("/*", "", input as string)
set input to replaceText("*/", "", input as string)
return input
exit repeat
end if
end repeat
return "/*" & (input as string) & "*/"
end run
on replaceText(find, replace, textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textString to text items of textString
set AppleScript's text item delimiters to replace
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
回答by paras gupta
回答by Ohmy
You can assign this yourself very easily, here goes a step by step explaination.
你可以很容易地自己分配这个,这里有一步一步的解释。
1.) In you xCode .m file type the following, it does not matter where you type as long as it's an empty area.
1.) 在您的 xCode .m 文件中输入以下内容,只要它是一个空白区域,您在哪里输入都没有关系。
/*
*/
2.)Highlight that two lines of code then drag and drop onto 'code snippet library panel' area (it's at the bottom part of Utilities panel). A light blue plus sign will show up if you do it right.
2.) 突出显示两行代码,然后拖放到“代码片段库面板”区域(位于“实用工具”面板的底部)。如果操作正确,将显示一个浅蓝色加号。
3.) After you let go of your mouse button, a new window will pop up and will ask you to add name, short cut etc; as shown. As you can see I added my shortcut to //. So every time I want a block comment I will type //. Hope this helps
3.) 松开鼠标按钮后,会弹出一个新窗口,要求您添加名称、快捷方式等;如图所示。如您所见,我将快捷方式添加到 //。所以每次我想要一个块注释时,我都会输入 //。希望这可以帮助
回答by lozflan
i managed to get this working well via an automator task and have used shortcut to bind it to key combination ctrl+option+command+b. all i have to do is highlight the code i want block commented in xcode and press the above keys and the selected text is block commented out using /* ... */.
我设法通过自动化任务使这个工作正常,并使用快捷方式将其绑定到组合键 ctrl+option+command+b。我所要做的就是突出显示我想要在 xcode 中注释的代码,然后按上面的键,然后使用 /* ... */ 注释掉所选文本。
i use code folding a fair bit so the reason i wanted this functionality was so i could easily fold down a block of commented code ... code commented the usual way using // wont fold.
我使用代码折叠相当多,所以我想要这个功能的原因是我可以轻松地折叠一块注释代码......代码使用//不会折叠的通常方式进行注释。
im not familiar with using mac automator but i simply followed the instrux in the following wwdc video
我不熟悉使用 mac automator,但我只是按照以下 wwdc 视频中的说明进行操作
in the WWDC 2012 video Session 402 - Working Efficiently with Xcode ( from around 6 minutes in) there's a description of how to use the Mac OSX Automator to add a service to manipulate selected text. The example shown in the video is to remove duplicates in a selection of text using the shell commands sort and uniq. Using this approach you do the same but you enter the following command instead of what he does in the video
在 WWDC 2012 视频 Session 402 - Working Efficiently with Xcode(从大约 6 分钟开始)中,描述了如何使用 Mac OSX Automator 添加服务来操作所选文本。视频中显示的示例是使用 shell 命令 sort 和 uniq 删除选定文本中的重复项。使用这种方法你做同样的事情,但你输入以下命令而不是他在视频中所做的
awk 'BEGIN{print "/"}{print $0}END{print "/"}'
awk 'BEGIN{print "/ "}{print $0}END{print "/"}'
(note there are meant to be 2 asterisks in the previous line that for some reason are not showing .... they do show up in the screenshot below so copy that as the correct command to enter)
(请注意,前一行中有 2 个星号由于某种原因没有显示......它们确实出现在下面的屏幕截图中,因此将其复制为正确的输入命令)
you should end up running a shell script like this
你最终应该像这样运行一个shell脚本
this will, for any given selected text, put the comment delimiters before and after.
对于任何给定的选定文本,这将在前后放置注释分隔符。
when you save it you should get options to name it (i called it blockcomment) and also to assign a keyboard shortcut
当你保存它时,你应该有选项来命名它(我称之为 blockcomment)并分配一个键盘快捷键
then you should be able to open xcode, select some text, right click, the context menu, and the name you gave to this script should show near the bottom
然后你应该能够打开 xcode,选择一些文本,右键单击,上下文菜单,你给这个脚本的名字应该显示在底部附近
simply click the name and the script will run and block comment the selected code or use the keyboard shortcut you assigned.
只需单击名称,脚本就会运行并阻止注释所选代码或使用您分配的键盘快捷键。
hope this helps
希望这可以帮助
回答by Fati
There is a symbol before help menu on xcode which has Edit user script. On Un/Comment Selection under comments section change my $cCmt = "//"; to my $cCmt = "#"; or whatever your IDE works with. Then by selecting lines and command + / (It's my xcode default) you can comment and uncomment the selected lines.
xcode 上的帮助菜单前有一个符号,其中包含编辑用户脚本。在评论部分下的取消/评论选择更改我的 $cCmt = "//"; 到我的 $cCmt = "#"; 或任何您的 IDE 使用的。然后通过选择行和命令 + / (这是我的 xcode 默认值),您可以注释和取消注释所选行。