Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

brackets

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.

like image 428
Meltemi Avatar asked Nov 23 '09 18:11

Meltemi


1 Answers

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.

like image 145
Lawrence Johnston Avatar answered Nov 15 '22 06:11

Lawrence Johnston