limp_chimp
limp_chimp

Reputation: 15163

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

I'm writing ANSI-compatible C code, and hence I can't use the line (//) comment. I'm using Xcode. In Sublime Text and Eclipse, and I think most other IDEs, there are separate keyboard shortcuts for line comments and block comments (/**/). However, I don't see that in Xcode - in fact, I don't even see a menu option to add a block comment. Is it simply not supported in Xcode? That would certainly seem to be a lame decision if so.

Upvotes: 195

Views: 242172

Answers (22)

gkhantaskin
gkhantaskin

Reputation: 24

Xcode 15.0.1 default comment selection shortcut is + *.

Upvotes: 0

Brogrammer
Brogrammer

Reputation: 472

UPDATE: Xcode 14+

On Spanish keyboards now works by pressing cmd + '.

Upvotes: 10

Furqan Ahsan
Furqan Ahsan

Reputation: 51

On a German keyboard, this works by pressing cmd + ß.

Upvotes: 5

e.john
e.john

Reputation: 81

UPDATE Xcode 14

  1. Open Xcode Preferences menu.
  2. Go to "Key Bindings" and enter "comment" into the filter area.

enter image description here

  1. Select the row "Structure > Comment" and enter your shortcut.

Upvotes: 6

Diogo Souza
Diogo Souza

Reputation: 420

Based on Baig's reply, I created a shortcut to comment and uncomment part of a line. This works on Xcode 13.2.1 and MacOS 12.0.

  1. Open Automator
  2. select Quick Action
  3. Search for Run AppleScript
  4. Select text on Workflow receives current dropdown
  5. Check Output replaces selected text
  6. Add this script
on run {input, parameters}
    set s to (input as string)
    if s contains "/*" then
        return text 3 thru -3 of s
    else
        return "/*" & s & "*/"
    end if
end run
  1. Tap play and save as Block Comment
  2. On Settings -> Keyboard -> Shortcuts, select App Shortcuts and tap +
  3. Use title of script as Menu Title (ie "Block Comment")
  4. Choose desired shortcut (I did command+option+shift+/)
  5. On Xcode select text to comment and press shortcut keys
  6. To uncomment, select text from /* to */ and press shortcut keys

Upvotes: 6

rockdaswift
rockdaswift

Reputation: 9983

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

Upvotes: 22

Nikola Milicevic
Nikola Milicevic

Reputation: 2975

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.

Upvotes: 49

Stefan Vasiljevic
Stefan Vasiljevic

Reputation: 4389

Try command + /.

So, you just highlight the block of code you want to comment out and press those two keys.

Upvotes: 283

Martin Braun
Martin Braun

Reputation: 12589

If you have a keyboard layout that requires you to also press the shift key (i.e. cmd + shift + 7 on a German keyboard), the shortcut won't work and open the help menu, instead.

Apple's "Think Different" in its fullest extent ...

You can define your own shortcut to make it work, if you go to Xcode > Preferences > Key Bindings:

Changing comment selection key map in Xcode

Upvotes: 1

Paras Gupta
Paras Gupta

Reputation: 825

In XCode 10 (and up) use Option + Command + Slash (that is ⌥ + ⌘ + /)

to write a beautiful comment for your function or class like below:

XCode screenshot of doc comment

Upvotes: 11

Ashim Dahal
Ashim Dahal

Reputation: 1147

In xcode 11.1 swift 5.0

select the code you would like to add block comment then press + + /

enter image description here

Upvotes: -4

Baig
Baig

Reputation: 4995

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.

Upvotes: 15

A. Buksha
A. Buksha

Reputation: 840

I modified the code of Nikola Milicevic a little bit so it also remove comment block if code is already commented:

on run {input, parameters}
    repeat with anInput in input
        if "/*" is in anInput then
            set input to replaceText("/*", "", input as string)
            set input to replaceText("*/", "", input as string)

            return input
            exit repeat
        end if
    end repeat
    return "/*" & (input as string) & "*/"
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

Hope this will help someone.enter image description here

Upvotes: 12

Daniel Bauke
Daniel Bauke

Reputation: 1218

If you're looking a way to convert autogenerated comment from Add Documentation action (available under cmd-shift-/) you might find it useful too:

function run(input, parameters) {
  var lines = input[0].split('\n');
  var line1 = lines[0];
  var prefixRe = /^( *)\/\/\/?(.*)/gm;
  var prefix = prefixRe.test(line1) ? line1.replace(prefixRe, "$1") : ""

  var result = prefix + "/*\n";  
  lines.forEach(function(line) {
    result += prefix + line.replace(prefixRe, "$2") + '\n';
  });
  result += '\n' + prefix + ' */';
  return result;
}

Rest the same as in @Charles Robertson answer:

Automator

Services

Upvotes: 0

Charles Robertson
Charles Robertson

Reputation: 1820

@Nikola Milicevic

Here is the screenshot of the indentation issue. This is very minor, but it is strange that it seems to work so well, in your example visual.

I am also adding a screenshot of my Automator set-up...

Thanks

enter image description here

enter image description here

Update:

If I change the script slightly to:

enter image description here

And then select full lines in XCode, I get the desired outcome:

enter image description here

enter image description here

Upvotes: 0

fzwo
fzwo

Reputation: 9902

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.

Upvotes: 22

Beninho85
Beninho85

Reputation: 3311

Now with xCode 8 you can do:

+ + /

to auto-generate a doc comment.

Source: https://twitter.com/felix_schwarz/status/774166330161233920

Upvotes: 42

Arthur C. Clarke
Arthur C. Clarke

Reputation: 17

in Macbooks, you can use shift + cmd + 7 to comment a previously highlighted block

Upvotes: -2

Fati
Fati

Reputation: 11

There is a symbol before help menu on xcode which has Edit user script. On Un/Comment Selection under comments section change my $cCmt = "//"; to my $cCmt = "#"; or whatever your IDE works with. Then by selecting lines and command + / (It's my xcode default) you can comment and uncomment the selected lines.

Upvotes: 0

Julio
Julio

Reputation: 41

Cmd + Shift + 7 will comment the selected lines.

Upvotes: 0

lozflan
lozflan

Reputation: 853

i managed to get this working well via an automator task and have used shortcut to bind it to key combination ctrl+option+command+b. all i have to do is highlight the code i want block commented in xcode and press the above keys and the selected text is block commented out using /* ... */.

i use code folding a fair bit so the reason i wanted this functionality was so i could easily fold down a block of commented code ... code commented the usual way using // wont fold.

im not familiar with using mac automator but i simply followed the instrux in the following wwdc video

in the WWDC 2012 video Session 402 - Working Efficiently with Xcode ( from around 6 minutes in) there's a description of how to use the Mac OSX Automator to add a service to manipulate selected text. The example shown in the video is to remove duplicates in a selection of text using the shell commands sort and uniq. Using this approach you do the same but you enter the following command instead of what he does in the video

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

(note there are meant to be 2 asterisks in the previous line that for some reason are not showing .... they do show up in the screenshot below so copy that as the correct command to enter)

you should end up running a shell script like this

screenshot

this will, for any given selected text, put the comment delimiters before and after.

when you save it you should get options to name it (i called it blockcomment) and also to assign a keyboard shortcut

then you should be able to open xcode, select some text, right click, the context menu, and the name you gave to this script should show near the bottom

simply click the name and the script will run and block comment the selected code or use the keyboard shortcut you assigned.

hope this helps

Upvotes: 1

Ohmy
Ohmy

Reputation: 2221

You can assign this yourself very easily, here goes a step by step explaination.

1.) In you xCode .m file type the following, it does not matter where you type as long as it's an empty area.

/*
*/

2.)Highlight that two lines of code then drag and drop onto 'code snippet library panel' area (it's at the bottom part of Utilities panel). A light blue plus sign will show up if you do it right.

enter image description here

3.) After you let go of your mouse button, a new window will pop up and will ask you to add name, short cut etc; as shown. As you can see I added my shortcut to //. So every time I want a block comment I will type //. Hope this helps

enter image description here

Upvotes: 8

Related Questions