Skip to content

Instantly share code, notes, and snippets.

@kvdesa
Last active June 26, 2024 15:08
Show Gist options
  • Save kvdesa/f9958d3249e174cb6cba51f42f99c7a7 to your computer and use it in GitHub Desktop.
Save kvdesa/f9958d3249e174cb6cba51f42f99c7a7 to your computer and use it in GitHub Desktop.
Add custom commands with shortcuts to Xcode

Add Custom Commands with Shortcuts to Xcode

To add custom key bindings in Xcode, you have to edit this file (su privileges required): '/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/IDETextKeyBindingSet.plist

To facilitate this, just add the following code to .bash_profile/.zshrc:

# Add custom commands to Xcode.
# Run this every time Xcode updates.
function addCustomCommandsToXcode() {
	PLB="/usr/libexec/PlistBuddy"
	PLIST="/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/IDETextKeyBindingSet.plist"
	$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down' string 'selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:, moveToEndOfLine:'" "$PLIST"
	$PLB -c "Add :'Custom Commands':'Delete Current Line' string 'deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToEndOfLine:'" "$PLIST"
}

Each line is a new custom command of your choice. The code above has commands for deleting the current line and duplicating the current selection.

Usage

NOTE: you will have to do this every time Xcode is updated!

  1. Call addCustomCommandsToXcode() from the terminal;
  2. Open Xcode and go to Xcode Preferences -> Key Bindings;
  3. Search for your custom commands and add a shortcut to them;
  4. ?
  5. Profit.
@jasonm23
Copy link

Very cool.

Any idea how I can get Ctrl-X Ctrl-S style bindings?! Preferences won't allow me to set them but I can see a few similar key sequences set.

I need this because XCode keeps throwing invisible ^S chars into the file I'm editing. As an Emacs user of about 30 years, C-x C-s is so ingrained into my muscle memory that I can't stop doing it. XCode keeps bugging me with inconvenient invalid character in source file messages.

@jasonm23
Copy link

addCustomCommandsToXcode
addCustomCommandsToXcode:3: no such file or directory: sudo /usr/libexec/PlistBuddy
addCustomCommandsToXcode:4: no such file or directory: sudo /usr/libexec/PlistBuddy

Works ok when I take sudo out of the func.

@kvdesa
Copy link
Author

kvdesa commented Sep 23, 2022

addCustomCommandsToXcode
addCustomCommandsToXcode:3: no such file or directory: sudo /usr/libexec/PlistBuddy
addCustomCommandsToXcode:4: no such file or directory: sudo /usr/libexec/PlistBuddy

Works ok when I take sudo out of the func.

Thanks for the fix!

@jasonm23
Copy link

Karabiner elements allowed me to not only capture the ^X ^S but also to remap them to save etc.

Free and open source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment