Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut to make a block comment in Xcode?

Tags:

comments

xcode

People also ask

How do I comment out a block of code in Xcode?

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.

How do I comment out multiple lines in Xcode C++?

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.

How do you comment a block in Swift?

There are two ways to add comments in Swift: // - Single Line comments. /*... */ - Multiline Comments.

How do you comment out code quickly?

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:

  1. Created Automator service using AppleScript

  2. Make sure "Output replaces selected text" is checked

  3. Enter the following code:

    on run {input, parameters}
    return "/*\n" & (input as string) & "*/"
    end run
    

enter image description here

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

  1. Open Automator
  2. Choose Services
  3. Search Run Shell Script and double click it

Add the below applescript in textarea

awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'

apple script for block comment

  1. 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.

New keyboard shortcut

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