Xcode adds comment characters in the first column?

Whenever I use the Xcode keyboard shortcut Command + / for commenting line(s) of code in a Swift/Objective-C source file, the // characters are always placed in the first column. I would much prefer it be at the current indentation.

I guess this is a personal preference, but does anyone know if there's config for it:

For e.g., If I have this code snippet:

    ...
    print("something")
    ...

and I invoke the keyboard shortcut Command + / with the caret positioned on the line containing print statement, I get:

    ...
//      print("something")
    ...

I'd prefer the following behavior instead:

    ...
    print("something")
    ...

I invoke the keyboard shortcut Command + / with the caret positioned on the line containing print statement, I should get:

    ...
    // print("something")
    ...

I guess this is a personal preference, but does anyone know if there's configuration for it?


You can use the Comment Here extension. After downloading:

  1. Restart Xcode
  2. Go to Xcode -> preferences -> key bindings
  3. Search for "Comment Selection" and remove the key binding for it
  4. Search for "Comment Here" and add the key binding "command /" for the toggle comments option.
  5. Your comments are now pretty :)

Using the mentioned keyboard shortcut, the comment character // always appear in the first column irrespective of the indentation level. There is no built-in mechanism to configure this behaviour.