Is there a shortcut to make a block comment in Xcode?
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.
Try command + /.
So, you just highlight the block of code you want to comment out and press those two keys.
UPDATE Xcode 12 / macOS Big Sur:
Currently the Mac App Store version of the BlockComment for Xcode doesn't show up under Xcode > Preferences > Key Bindings. This issue has been resolved and GitHub version can be used instead.
UPDATE June 2017:
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.
ORIGINAL ANSWER:
None of the above worked for me on Xcode 7 and 8, so I:
-
Created Automator service 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
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.
Now with xCode 8 you can do:
⌥ + ⌘ + /
to auto-generate a doc comment.
Source: https://twitter.com/felix_schwarz/status/774166330161233920
There is now an Xcode plugin that allows this: CComment.
The easiest way to install this is to use the amazing Alcatraz plugin manager for Xcode.
EDIT Apple 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 radars for API for plugins you'd like to write or see.
UPDATE: Xcode 8 Update
Now with xcode 8 you can do:
⌥ + ⌘ + /
Note: Below method will not work in xcode version => 8
Very simple steps to add Block Comment functionality to any editor of mac OS X
- Open Automator
- Choose Services
- Search Run Shell Script and double click it
Add the below applescript in textarea
awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'
- Save script as
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 Shortcut and click Add button.
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.