Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard shortcut for comment blocks /* ... */ in Xcode

Tags:

comments

xcode

I am looking for a keyboard shortcut to comment out a selection of code. I already know about Command + / to add // in front of selected lines of code, but I want to be able to create /* ... */ comment blocks this way.

The reason I want a shortcut for /* ... */ is to be able to fold the comment blocks, which as far as I know you cannot do with the // comments.

So either I need a way to fold // type comments or a keyboard shortcut to create /* ... */ comments

any ideas?

like image 458
Michael Campsall Avatar asked Feb 04 '13 02:02

Michael Campsall


2 Answers

It isn't as good as I'd like it to be, but this is the best option I've come up with so far. I created a few custom code snippets in Xcode, each of which replaces a shortcut with an appropriately sized comment block.

For example, I have it replace the shortcut "com1" with an 80-character-wide comment block (for non-indented lines). That's the following code in the Snippet Library in Xcode:

/*******************************************************************************
<#comment#>
******************************************************************************/

For indented lines, I have shortcuts "com2", "com3", and "com4", which are 76, 72, and 68 characters wide (respectively).

======

Also, Xcode's documentation isn't that helpful when it comes to code snippets. To create a new code snippet, show the Utilities view (the pane on the right) and select the {} icon towards the bottom to show the Code Snippet Library. Select some code to add, and drag it over to the Code Snippet Library (by left-clicking and holding for a second and then dragging - as a Windows user it took me far too long to figure this out). This will create a new entry titled My Code Snippet at the very bottom of the Code Snippet Library, which you can mouse over and Edit to your liking.

======

EDIT: Here I have uploaded a zip file containing five code snippets. Levels 1 through 4 (com1, com2, com3, com4) are 80, 76, 72, and 68 characters wide, respectively. Level 0 is a single line comment block that does not automatically format width (I use it for single comments to maintain formatting).

EDIT 2: Also, user code snippets are stored in ~/Library/Developer/Xcode/UserData/CodeSnippets/.

like image 62
mbuc91 Avatar answered Sep 18 '22 15:09

mbuc91


i reposted your question on apple dev forums .... i am also looking for the answer. here is a response i got ..... havent tried to do it yet but will soon.

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 could shell out to an awk

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

which will, for any given selected text, put the comment delimiters before and after. Then add a shortcut to invoke this service.

like image 40
lozflan Avatar answered Sep 19 '22 15:09

lozflan