A long time ago, I remember using Apple’s MPW environment to create software for the Mac. This environment was a lot like using terminals had been on the Unix machines back in college. This had its plusses and minuses but it was strange in one fashion: I could execute command-line scripts inside the text editor. This ability has returned in Xcode. (Well, I just noticed it recently due to a lucky accident. It may have been there all along.)
The default setup for Xcode uses Control-R (and variations with Option or Shift keys) to execute the currently selected text as a bash terminal command. If no text is selected, the entire line containing the insertion point is used. You can change which key does this in Xcode’s “Key Bindings” preference panel under the “Text Key Bindings” tab. (Note that the variations are listed in the bindings panel mentioned.)
This can have many small, cool uses. Here are a few off the top of my head:
- date
this will give you a timestamp like this one: “Fri Jul 17 14:04:02 EDT 2009″
- ls
“ls” will list directories of files directly into your text file – this can be useful if you are looking to put a file name into a file open command or something
If you have a useful command-line tool whose output would be useful in an Xcode text file, this will work for you.
What you can’t do is run programs that don’t stop or require further input, like TOP or SSH. For those, you’ll still need to use the terminal program supplied with your computer. If you do try this, you will cause problems for Xcode that will probably lead to it locking up or crashing.
I tried this with “top” and Xcode started having difficulty editing the file any further. Other things seemed to work, but it eventually crashed. As I figured out later, a “top” process had started. The second time, I found it in the process list and used “kill” on it from the real terminal. The moment I killed it, the Xcode window filled with a ton of output generated by TOP.
In another case, I tried to use the SQLite3 tool, but it just quit with this error: “Incomplete SQL”. So interactive tools don’t work for this.
Of course, its easy to capture the output of command-line tools for text files in different ways, so this is cool, but not really earth-shaking. It can make some work much quicker and easier. Enjoy.
