4

Is it possible to set a keyboard shortcut (or maybe add some menu item somewhere) to open currently edited file in external editor?

(Obviously I may do [ (right click in file tree → Show in Finder) / (right click in window title → select containing directory) ] → right click on file → Open With → "the app" — but it's too much steps.)

4 Answers 4

10

Huh, I've found it.

  1. Start Automator.app.
  2. Select "Service".
  3. Drag-n-drop "Run AppleScript" to workflow.
  4. Set "service receives" to "no input", and application is Xcode.
  5. Paste this code (replace MacVim with your editor)

    tell application "Xcode"
        set current_document to last source document
        set current_document_path to path of current_document
    end tell
    
    tell application "MacVim"
        activate
        open current_document_path
    end tell
    
  6. Save workflow.

  7. In System Preferences → Keyboard → Keyboard Shortcuts → Services → General, assign a shortcut.
  8. You're done!

I've borrowed AppleScript code from this project: Uncrustify Automator Services for Xcode 4.

2
  • +1 Thanks for sharing! Don't forget to accept your own answer :)
    – Anne
    Sep 6, 2012 at 23:01
  • I use tabs in XCode. The above code opens the file from the first tab, not from the current one. I asked a new SO question for this: stackoverflow.com/questions/22499621/…
    – devmake
    Mar 19, 2014 at 8:04
3

A better solution is to add a keyboard shortcut directly in the keyboard preferences pane:

  1. open "System Preferences" → "Keyboard" → "Keyboard Shortcuts" → "Application Shortcuts"
  2. click on the "+" plus sign
  3. select "XCode" as Application and type "Open with External Editor" as Menu Title
  4. set your shourcut (I'm using ⇧⌃E)
1
  • 1
    Or you could just use the "Key Bindings" tab of Xcode preferences
    – Fjölnir
    Mar 18, 2013 at 5:25
3

Because I use tabs, I was eager for a solution that wouldn't open a document from the first tab. I ended up with following:

Note: If Assistant editor is opened, the file from the Standard Editor (on the left side) is opened regardless which editor is active, but for me it's better than the first tab.

on run {input, parameters}

    set current_document_path to ""

    tell application "Xcode"
        set last_word_in_main_window to (word -1 of (get name of window 1))
        if (last_word_in_main_window is "Edited") then
            display notification "Please save the current document and try again"
            -- eventually we could automatically save the document when this becomes annoying
        else
            set current_document to document 1 whose name ends with last_word_in_main_window
            set current_document_path to path of current_document
        end if
    end tell

    tell application "MacVim"
        if (current_document_path is not "") then
            activate
            open current_document_path
        end if
    end tell

    return input
end run
1

I really liked the solution that Fjölnir made, so to expand on how to do that.

  1. Choose the Menu: "Xcode" -> "Preferences..."
  2. Click the "Key Bindings" Tab on the Preferences Window
  3. In the "Filter" search box enter External
  4. Double click on the "Key" column next to the "Open with External Editor (File Menu)" command.

keybinding preferences

If you are using MacVim for this, you can improve the load time for the external editor by configuring it to stay running after the last window closes.

  1. Choose the Menu: "MacVim" -> "Preferences..."
  2. In the "General" tab set "After last window closes" to "Hide MacVim"

MacVim settings

Now when I press ⌥E the window pops open, I make my change, :q, and focus returns to Xcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.