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

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