Meltemi
Meltemi

Reputation: 38359

Xcode shortcut for wrapping selected text in curly braces "{ }"

Any Xcode shortcut for wrapping a text selection in opening & closing brackets - { }, ( ) or [ ]?

Growing tired of removing the "}" that Xcode automatically enters after I type "{" in cases where I've already got code in the editor that wants to be inside the new brackets.

Upvotes: 6

Views: 4457

Answers (3)

Redwood
Redwood

Reputation: 69362

Here's an Xcode user script which should not wipe out new lines in the text.

#!/usr/bin/python
#
# Wraps selection in braces.
# Set Input to "Selection".
# Set Output to "Replace Selection".

tabChar = '\t' # Replace with spaces if desired
input = '''%%%{PBXSelectedText}%%%'''

print "{"
for line in input.splitlines():
  print tabChar + line
print "}"

See the Script Input Variables section of the Xcode Workspace Guide for more information on %%%{PBXSelectedText}%%% and the other available input variables.

EDIT: added support for indenting the code to be surrounded by a given amount. Right now the indent must be hard coded. It may be possible to get this value from, e.g., the Xcode preferences file, but I didn't go that far.

Upvotes: 3

Meltemi
Meltemi

Reputation: 38359

Apple released a Tech Q&A regarding this. Alas, doesn't handle closing curly but still seems better than moving all selected code to a single line.

Upvotes: 0

cdespinosa
cdespinosa

Reputation: 20799

You can uncheck the preference to automatically add the closing brace.

Or you could add this little script to your User Scripts:

alt text http://idisk.mac.com/cdespinosa/Public/Wrap%20in%20Braces.png

Upvotes: 0

Related Questions