94

I'm having issues with Xcode being quite unreliable when using the keyboard shortcut to comment or uncomment code +/. I can't seem to find a real pattern as to when it breaks, but it usually happens after building and running my code on the simulator. It will not work again until I edit something in any one of my code files. Even if I switch to another file in the main view, commenting via shortcut does not work. Other keyboard shortcuts, such as indenting +[ continue to work just fine.

Do other people have this issue, and if so, have you found a solution? I know it seems like a small complaint, but when running and then quickly trying to comment out a block of code to check the effect, it's a bit of an inconvenience.

I am using Xcode 6.1 (6A1052d) on OXS Yosemite 10.10 (14A389) and developing in Objective-C for iOS if that matters.

Stonz2
  • 6,306
  • 4
  • 44
  • 64
  • I had the same issue. Whenever it didn't work i'd manually do it (editor/structure/comment selection) – MendyK Oct 29 '14 at 15:12
  • Possible duplicate of [Keyboard shortcuts in Xcode stop working](http://stackoverflow.com/questions/7306656/keyboard-shortcuts-in-xcode-stop-working) – buildsucceeded Apr 05 '16 at 10:45

15 Answers15

131

For an Xcode 8, solution is

sudo /usr/libexec/xpccachectl

and restart your computer.

shim
  • 9,289
  • 12
  • 69
  • 108
CryingHippo
  • 5,026
  • 1
  • 28
  • 32
  • 1
    I believe it recreates cache for Xcode source editor extensions – CryingHippo Sep 11 '16 at 08:02
  • 2
    Does restart Mac was the only thing to do? – Szu Sep 20 '16 at 15:13
  • 3
    Did not work for me with Xcode 8.0. Loved this shortcut. – simioliolio Oct 17 '16 at 09:13
  • @simioliolio did you restart? – Roman Truba Nov 04 '16 at 11:56
  • 3
    Yes, restart is necessary (unfortunately...) – T'Pol Nov 06 '16 at 23:05
  • 1
    Happened just now for me on Xcode 8.1 - and this solution still works. Restart needed only for Xcode, not the computer. – turingtested Dec 14 '16 at 15:10
  • 40
    Worked for me. But these kind of hacks are unacceptable. XCode 8 is broken in many areas - for example, C++ editing is a disaster - even syntax highlighting doesn't work most of the times. Apple, get your shit together. Maybe stop rushing a buggy new OS every half a year and focus on increasing quality. – Florin Dec 19 '16 at 11:40
  • I followed this steps and restart my system and even x-code too but it doesn't work for me. Is there any other trick to re-solve this problem. I have x-code 8.x and **El-captain** OS. – dahiya_boy Jan 09 '17 at 05:15
  • 1
    "and restart your computer" is not an acceptable solution, Apple :/ – pkamb Jan 10 '17 at 22:10
  • This seems to only work temporarily. As the next day I have to redo this same command, every day. – Bot Feb 08 '17 at 20:14
  • 5
    Please answer what the command does if you can. I haven't been able to find docs for it, and don't really like running commands blind. – Jim Leask Mar 23 '17 at 19:56
  • Given the may upvotes I'm convinced this works in most cases - but not all. This problem has several duplicates, and in one of them I read that this feature can relatively easily get confused by nested comments like /* // */, or (as was the case with me) by text that's not even comment according to the C++ parser doesn't see as comment at all because it was in a string, like std::make_pair(true, "/*//*/") – ecotax May 16 '17 at 11:57
  • Works! Thanks :-D – Hernan Arber Sep 17 '17 at 09:22
114

Hit +] once and then +/ will start working again.

You can hit +[ to undo the indentation effect.

Update:

Please note- This solution was provided prior to Xcode 8 launch so it may not work for everyone. If you are still facing problem, kindly refer to @CryingHippo's answer instead of downvoting.

Community
  • 1
  • 1
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
31

I have done this numerous times with Xcode 8. Latest being Xcode 10.1 where I would just quit Xcode and go to Applications and rename Xcode to something else, e.g. Xcode 2, launch it, quit again and rename back. It starts working after that. No Mac restart or Terminal commands required.

It still works as of Xcode 10.1. I cannot believe Apple still hasn't fixed it properly.

Matt
  • 1,599
  • 14
  • 14
11

None of the above solutions worked for me with Xcode 8.0 (final). If you also have Xcode 8.1 beta installed like I do, try this: simply rename /Applications/Xcode-beta.app to something else, then restart Xcode 8. Oddly enough, that did the trick.

Oscar Hierro
  • 1,117
  • 1
  • 10
  • 15
  • Xcode 8: I didn't have the beta installed (but have had betas in the past). Renaming `Xcode.app` to something else, relaunching, quiting, then renaming it back to `Xcode.app` fixed it for me. Thanks! – xcvr Nov 01 '16 at 23:16
  • 2
    don't know why the heck this worked, but it did. (1) renamed to Xcode8.app. (2) opened Xcode. (3) closed Xcode. (4) renamed back to what it was before (Xcode8.0.app). the nice thing about this answer is that it doesn't require machine restart! – Sam Jan 10 '17 at 13:02
  • @Sam thanks, this worked for me as well, will suggest an edit to oscahie's answer, as it works for non-beta too – Kqtr Aug 09 '17 at 10:10
  • Didn't work for me in Xcode 11.3.1. – August Jul 10 '23 at 07:43
8

Oscahie's answer actually works for non-beta versions of Xcode as well. Here are the steps to follow, based on Sam's comment:

  1. Close Xcode
  2. In the Application folder, rename Xcode.app (or similar) to Xcode2.app. You might need to type in your password.
  3. Open Xcode, then close it.
  4. Rename Xcode2.app back to what it was before.
  5. (Optional) Spend 5 minutes wondering why this worked.

No need to reboot!

Kqtr
  • 5,824
  • 3
  • 25
  • 32
  • Feels great to look again for the solution to this problem a month later and find my own answer here! :D Still works folks. – Kqtr Sep 13 '17 at 21:05
4

I didn't have to do any of the above. A simple reboot fixed it.

user1366265
  • 1,306
  • 1
  • 17
  • 28
2

A simple workaround works good for me with following steps:

  1. Comment any line i.e. simply add "//" in front of a line.
  2. Press Command + "/" and it starts working.

It might help someone.

schinj
  • 794
  • 4
  • 19
2

In my code I had this sequence:
/*" BLABLA MY COMMENTS */
All the code under this was not "commentable".

Removed the " and it was ok !

Hope it helps!

Stonz2
  • 6,306
  • 4
  • 44
  • 64
Harold
  • 49
  • 4
2

I just had the same issue after setting up a new Mac with Xcode 10.1 using the German keyboard layout.

The solution was to disable the Keyboard shortcut in system settings for the help menu.

System settings > Keyboard > Shortcuts > App shortcuts.

There was a shortcut for all apps to show the help menu. If you have that, click the checkbox to disable it

Screenshot of Keyboard shortcuts in Mac system preferences

1

I am using Xcode 8.1 (not beta) and I had the same problem, not able to cmd+\ to comment. Restart Xcode not working for me but RESTART my Mac SOLVED the problem without a need to remove cache.

Ohmy
  • 2,201
  • 21
  • 24
0

I had this problem in Xcode 8.0. Even though I already move Xcode 8.1-beta somewhere else (It's at Application/Xcode otherversions/Xcode 8.1-beta.app). I have to delete it to the trash, restart Xcode 8.0, and cmd+/ somehow works again.

Hlung
  • 13,850
  • 6
  • 71
  • 90
0

Just quit xcode completely and restart. Worked for xcode 8.2

Akul Tomar
  • 47
  • 1
  • 4
  • I experience the problem in Xcode 13 / MBP 2021, this works for me temporarily but the issue comes back regularly – lorenzo Nov 12 '21 at 13:03
0

I was also facing this issue, when having multiple Xcode installed.

In which Xcode version you are you facing this problem, to solve this, the Xcode should be in the Application Folder. If it resides in the subfolder than move it to the Application folder directly.

Gopal Devra
  • 787
  • 9
  • 15
0

I found a pattern where toggle comments do not work. In fact, uncomment works, but comment don't, from keyboard as well as from the Editor menu. This happens in a specific file, suggesting there is something in text that prevents from working. I tested on XCode 10.1ß and on XCode10.2, on different machines.

Text before this line can be commented, text after cannot (I kept text exactly as is)

let alert = UIAlertController(title: "" /*"Teste texte"*/, message: "Love\nWe'll recommend more for you" /* Entrez des chiffres"*/, preferredStyle: .alert)  

It appears that the /* … */ inside the UIAlertController() is the cause. If I suppress both comments inside, everything works OK.

I filed a bug report. # 49907361

claude31
  • 874
  • 6
  • 8
-1

Nothing above worked, even after trying multiple times.

Just reinstalled new xcode using AppStore app, previously I installed xcode after downloading from developer.apple.com/downloads. that is .xip file

Wait for new update and install.

Another reason may be that xcode isn't in Application directory.

Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58