6. Comments By selecting a few lines of code (pressing ⇧+↑ or ⇧+↓), you can comment a bunch of lines in a single shot with the ⌘ + / shortcut. If the lines are already comments, the same shortcut will uncomment them.
Is there a way to comment various lines at the same time in Xcode IDE? Selecting which lines to comment, then clicking somewhere et voilá.. the /* ... */ characters wrapping the code. Thank you.
There are two ways to add comments in Swift: // - Single Line comments. /*... */ - Multiline Comments.
PDT allows you to quickly and easily comment and uncomment code by selecting a line or a block of text and tagging it as a comment. Comments can be added to single lines of code (Ctrl + /) or blocks of code (Ctrl + Shift + /).
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
Add the below applescript in textarea
awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'
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.
It looks that on macOS Monterey the Xcode block comment toggle key combination has been changed to command ⌘ + '
Edit: Xcode 13.2 has returned to the previous key combination. command ⌘ + ⇧ + 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With