Skip to content

Instantly share code, notes, and snippets.

@kvdesa
Created March 14, 2025 13:11
Show Gist options
  • Save kvdesa/de4d35b1cdf10f2bfa49267174018d4c to your computer and use it in GitHub Desktop.
Save kvdesa/de4d35b1cdf10f2bfa49267174018d4c to your computer and use it in GitHub Desktop.
Add custom commands to Xcode.app and all Xcode-*.app instances, use this every time you update/download a new Xcode.
# Add custom commands to Xcode.app and all Xcode-*.app instances.
# Run this every time you update/download Xcode.
function xcodeAddCustomCommands() {
PLB="/usr/libexec/PlistBuddy"
# First, update the standard Xcode.app if it exists
if [[ -d "/Applications/Xcode.app" ]]; then
echo "Xcode.app found, adding commands"
PLIST="/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/IDETextKeyBindingSet.plist"
if [[ -f "$PLIST" ]]; then
# Add the "Duplicate Current Lines Down" command
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down' string 'selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:, moveToEndOfLine:'" "$PLIST"
# Assign Command+D
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down':KeyEquivalent string 'd'" "$PLIST"
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down':ModFlags integer 1048576" "$PLIST"
# Add the "Delete Current Line" command
$PLB -c "Add :'Custom Commands':'Delete Current Line' string 'deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToEndOfLine:'" "$PLIST"
# Assign Command+Backspace
$PLB -c "Add :'Custom Commands':'Delete Current Line':KeyEquivalent string $'\x7F'" "$PLIST"
$PLB -c "Add :'Custom Commands':'Delete Current Line':ModFlags integer 1048576" "$PLIST"
fi
fi
# Then, update each Xcode-*.app
for xcodeFolder in /Applications/Xcode-*.app; do
if [[ -d "$xcodeFolder" ]]; then
echo "$xcodeFolder found, adding commands"
PLIST="$xcodeFolder/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/IDETextKeyBindingSet.plist"
if [[ -f "$PLIST" ]]; then
# Add the "Duplicate Current Lines Down" command
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down' string 'selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:, moveToEndOfLine:'" "$PLIST"
# Assign Command+D
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down':KeyEquivalent string 'd'" "$PLIST"
$PLB -c "Add :'Custom Commands':'Duplicate Current Lines Down':ModFlags integer 1048576" "$PLIST"
# Add the "Delete Current Line" command
$PLB -c "Add :'Custom Commands':'Delete Current Line' string 'deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToEndOfLine:'" "$PLIST"
# Assign Command+Backspace
$PLB -c "Add :'Custom Commands':'Delete Current Line':KeyEquivalent string $'\x7F'" "$PLIST"
$PLB -c "Add :'Custom Commands':'Delete Current Line':ModFlags integer 1048576" "$PLIST"
fi
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment