72

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.

BigJoke
  • 855
  • 1
  • 7
  • 14
  • Still today, with Xcode 7, the short answer is **NO**. We all know the 'CMD + /' shortcut (comment/uncomment). I'm still waiting for a non-plugin shortcut for selecting a block and wrapping it with /* ... */ – bauerMusic Dec 16 '15 at 10:00
  • Best method to use block comment in Xcode http://stackoverflow.com/a/38368766/1556386 – Baig Jul 14 '16 at 08:15

14 Answers14

145

Command + Shift + 7

or

Command + /

This inserts // in front of every selected line.

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
9

Xcode Version 6.1

Shortcuts:

To Comment: Select the code to be comment, ⌘ + /

To uncomment: Select the code an press ⌘ + /

Also see taskbar Editor>Structure "(un)Comment Selection" as user 2137640 pointed out.

Carl
  • 91
  • 1
  • 1
6

I've been using the Command + Shift + 7 for years, but now in Xcode 13.1 it just started to open the help menu instead of commenting the selected block.

Apparently the new command for commenting a selection of code is:

Command + Shift + ?

Edit:

Another solution is to deselect "Show Help menu" in settings > keyboard > App Shortcuts

enter image description here

Jacob Ahlberg
  • 2,352
  • 6
  • 22
  • 44
5

The answer is actually very simple if you want to comment a continuous sequence of lines. Select the consecutive lines, then simply press ⌘ /.

I have no idea how to comment multiple non-consecutive lines. Maybe somebody else can help with this, if it is even possible (I doubt it.)

Simon
  • 31,675
  • 9
  • 80
  • 92
KZcoding
  • 1,417
  • 4
  • 16
  • 26
4

There is such a function in the menu. Try: Edit->Insert Text Macro->C->Comment Section. It will create a (multi-line) comment around the currently selected text.

However, using the menu is probably often slower than manually inserting the comments. But, you can also bind a key to the menu item via the preferences key binding settings.

dmeister
  • 34,704
  • 19
  • 73
  • 95
4

For single line Comment use ⌘+/
For multiline Comment you need to install CComent plugin

to install plugin manager use alcatraz

Rinkesh
  • 3,150
  • 28
  • 32
  • 1
    @EladG not the same. `cmd + /` will use multiple single-line comments (`// one line text...`), but the CComment plugin will use a single multi-line comment (`/* multi-line text... */`), just like in C. – Alejandro Iván Jan 19 '16 at 15:20
3

new Xcode 14: Keys: Command + '

enter image description here

oskarko
  • 3,382
  • 1
  • 26
  • 26
2

In Turkish keyboard cmd + *

You can use uncomment and multiline comment.

1

in Xcode 7.x

Shortcuts:

To Comment: Select the code to be comment, ⌘ + /

To uncomment: Select the code an press ⌘ + /

kvra13
  • 1,019
  • 2
  • 10
  • 16
0

Got it! You have to un-indent the lines so the comments are the first character on the line, not a tab or whitespace - then pressing CMD+/ works. Just tried it in XCode 5.1.1, works like a charm.

guest
  • 1
0

This worked !

Higlight the desired lines then press Command + /

The same rule goes for uncommenting.

Edwin O.
  • 4,998
  • 41
  • 44
0

For xcode 8 ->
sudo /usr/libexec/xpccachectl
restart your computer

Adeel
  • 859
  • 6
  • 9
0

For XCode 14 and a german keyboard layout it's command - ß

0

For me, with italian keyboard layout but english language on XCode 14.3, the multi-line (selection) comment shortcut is

⌘ '

Andrea Bellitto
  • 363
  • 2
  • 9