Xcode: Piping selected text to external programs

This is a repost of https://discussions.apple.com/thread/253025487

Hi,

How do you pipe selected text in Apple Xcode out to an external program, replacing it with the output from that program? For example, in MacVim, you can type

!}command

to pipe the rest of the current paragraph to command, replacing the original text with the output of that command, which is hella useful if you’ve written a lot of filters for tasks like automatic text reflowing or automatic source code reformatting. How do you do this in Apple Xcode?

I am running Apple Xcode Version 10.1 (10B61) [the highest-numbered, most-recent version of Xcode compatible with Apple macOS Version 10.13.6 “High Sierra”].

’preciate it, Austin

Finally found a solution thanks to two things that have changed since I originally posted this 5 years ago:  (0.) Have since migrated to macOS ‘Ventura’ after my previous 2010 MacBook Pro broke down; and (1.) I didn’t have Claude AI around back in 2021 to find the following solution for me:

Claude.ai’s solution using Automator:

  1. In Automator, select Quick Action;
  2. With “Workflow receives current” set to Automatic (text) by default, and with “in” set to Any application by default, select Output replaces selected text;
  3. Add (drag) Run Shell Script to the workflow;
  4. With “Shell:” set to your desired shell (/bin/ksh in my case), and with “Pass input:” set to to stdin by default, replace the default “cat” with the name of your external filter (in my case “f”) [note: make sure your PATH in your desired shell includes the directory containing your filter (in my case f); otherwise you’ll have to specify the absolute path to your external filter]; and
  5. Save your Automator action, and “Save quick action as:” whatever (in my case, I used “f,” in spite of the lowercase name not following Apple’s convention of having all menu items have initial caps).

You should now be able to invoke your external filter by selecting text in Xcode and going to Xcode ⇀ Services ⇀ f (or whatever the name of your external filter is).

Assigning a keyboard shortcut to run f (or whatever your external filter is):

  1. Go to System Settings ⇀ Keyboard ⇀ Keyboard Shortcuts…;
  2. In the sidebar select Services;
  3. Expand/explode the “Text” line, and to the right of f (or whatever the name of your external filter/Automator quick action is), double-click on “none”;
  4. Type your preferred keyboard shortcut to run your external filter (in my case I used option-F to run my f, because command-F was already used by Xcode for “Find…,” shift-command-F was already used by Xcode for “Find in Workspace…,” option-command-F was already used by Xcode for “Find and Replace…,” and control-command-F was already used by Xcode for “Enter Full Screen”).

You should now be able to invoke your external filter on selected text from within Xcode using your preferred keyboard shortcut (option-F in my case).

Tested all the above in Xcode 15.2 under macOS “Ventura” version 13.7.8.

H/t Claude AI for figuring out all the above for me; I was this close to throwing up my hands and migrating to Sublime Text (which I’ve been procrastinating doing for five years due to Sublime Text’s poor/nonexistent rendering support for RTL languages such as Hebrew and Arabic, something whereat Xcode excels)

Xcode: Piping selected text to external programs
 
 
Q