Skip to main content

Overview

Xcode Shortcuts allow you to execute custom commands directly from Xcode using the cmd extension. You can create shortcuts that open files in external editors, run scripts, or perform any command-line operation using context from your current Xcode session.
Shortcuts are only available when a file is open in Xcode.

Prerequisites

Before configuring shortcuts, ensure you have granted the Xcode Extension permission:

Grant Xcode Extension Permission

Follow the installation guide to enable the Xcode Source Editor extension
Without the Xcode Extension permission, shortcuts will not be available in Xcode’s menu.

Creating a Shortcut

1

Open Shortcut Settings

Go to Settings > Xcode Shortcuts in cmd
2

Add New Shortcut

Click “Add Shortcut” to create a new shortcut
3

Configure the Command

Enter your command using any of the available variablesExample Commands:

Open file in VS Code at line number

code --goto "$FILEPATH${SELECTED_LINE_NUMBER_START:+:$SELECTED_LINE_NUMBER_START}"
This opens the current file in VS Code, jumping to the selected line if available.
echo "$FILEPATH" | pbcopy
Copies the current file path to your clipboard.
~/scripts/process-selection.sh "$SELECTED_TEXT" "$FILEPATH"
Executes a custom script with the selected text and file path.
open -a Terminal "$XCODE_PROJECT_PATH"
Opens a new Terminal window at your project directory.
swiftformat --config rules.swiftformat "$FILEPATH"
4

Name Your Shortcut

Give your shortcut a descriptive name that will appear in Xcode’s menu (e.g., “Open in VS Code”, “Copy File Path”)
5

Test the Shortcut

Click the button to test your shortcut and verify it works correctly
6

Save

Click “Save” to add the shortcut
7

Save

You should now find your shortcut in Xcode under Editor > cmd > shortcut name

Setting Up Key Bindings in Xcode

For quick access, it is recommended to assign keyboard shortcuts to your commands:
1

Open Xcode Settings

In Xcode, go to Xcode > Settings > Key Bindings
2

Find Your Shortcut

Search for your shortcut name in the key bindings list, or for cmd
3

Assign a Key Combination

Click on the shortcut and press your desired key combination.
4

(Optional) Resolve conflict

If your desired key binding conflicts with one already defined in Xcode (there’s a lot!), change or remove the conflicting one.
5

Verify

The key binding is now active. Test it in any open Xcode file.
Recommended: Set up key bindings for your most frequently used shortcuts to streamline your workflow.

Variable Reference

Use these variables in your shortcut commands to access Xcode context:
VariableDescriptionExample Value
$FILEPATHAbsolute path to the current file/Users/name/project/ContentView.swift
$FILEPATH_FROM_GIT_ROOTRelative path to the current file from the git root./project/ContentView.swift
$SELECTED_LINE_NUMBER_STARTFirst line of selection42
$SELECTED_LINE_NUMBER_ENDLast line of selection58
$XCODE_PROJECT_PATHThe path to the project active in Xcode/Users/name/swift-package /Users/name/app.xcodeproj

Using Conditional Variables

Some variables may not always be available (e.g., when nothing is selected). Use shell parameter expansion for conditional behavior:
# Only add line number if text is selected
code --goto "$FILEPATH${SELECTED_LINE_NUMBER_START:+:$SELECTED_LINE_NUMBER_START}"