Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Key-bindings to XCode

Tags:

xcode

ios

swift

I am trying to add a group of custom key-bindings to Xcode 6. I've looked up in the past posts and discovered to do this you need to edit the Default.idekeybindings file located in your Xcode -> UserData -> KeyBindings -> Default.ideakeybindings

I have edited this according to how it was done in the previous versions, but when I go into the preferences -> keybindings within Xcode I am not able to find the custom action I defined. I would ideally like to make a one key "copy line", "cut line", "duplicate line" and some other useful keybindings I've grown to like while using Sublime and Atom. I've included my .ideakeybindings edited file contents below for reference to what I'm trying to do.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>My Custom Actions</key>
    <dict>
        <key>Delete Current Line/Selection</key>
        <string>deleteToEndOfLine:, deleteToBeginningOfParagraph:</string>
        <key>Duplicate Line</key>
        <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:</string>
    </dict>
</dict>
</plist>
like image 410
Unome Avatar asked Nov 19 '14 00:11

Unome


People also ask

How to create shortcut Xcode?

You can add shortcuts using Xcode > Preferences > Key Bindings. Search for the command-name using the field at the top-right, then double click in the key field and type the key you want to use.

How do I change the shortcut in Xcode?

For Xcode only , you can't change Command + Click to Control + Click. If you want to change your MAC Shortcut Key then you can Change it from Goto System Preference -> Keyboard -> Modify Keys then update your shortcut key according to your preference.

How do you assign key bindings?

To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.

Why use keyboard shortcuts?

Efficiency. Using keyboard shortcuts instead of the mouse enables you to work faster, more efficiently and with increased precision, thereby saving you time and improving your productivity. As well, for those with mobility or vision disabilities, keyboard shortcuts are indispensable.


2 Answers

I did this on Xcode 7:

  • Close Xcode
  • Open the file /Applications/Xcode-Beta.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist with sudo privileges (be sure to use the correct app-directory for the version of Xcode you are using---for Xcode 6.3 beta mine was Xcode-Beta.app)
  • Add your custom commands, save and then start Xcode.
  • Set key bindings in Xcode preferences

I added this section:

<key>My Custom Commands</key>
<dict>
    <key>Insert New Line Below</key>
    <string>moveToEndOfLine:, insertNewline:</string>
    <key>Insert New Line Above</key>
    <string>moveUp:, moveToEndOfLine:, insertNewline:</string>
    <key>Duplicate Current Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
    <key>Delete Current Line</key>
    <string>selectLine:, delete:</string>
    <key>Cut Current Line</key>
    <string>selectLine:, cut:</string>
    <key>Copy Current Line</key>
    <string>setMark:, selectLine:, copy:, moveToEndOfLine:, swapWithMark:</string>
    <key>Paste At Beginning Of Line</key>
    <string>moveToBeginningOfLine:, paste:</string>
</dict> 

As this gets overwritten every time a new version of Xcode is installed, a user settings location for the custom commands would really be appreciated if someone knows where to add them in the user dir.

like image 103
Melodius Avatar answered Sep 20 '22 13:09

Melodius


I've been using the following for years because I came from Eclipse camp and do like "Duplicate Current Line Up" and "Duplicate Current Line Down" very much. Note "Xcode.app" could be "Xcode62.app" or "Xcode63.app" depending on how you renamed it.

sudo vim /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

<key>Customized</key>
<dict>
    <key>Duplicate Current Line Up</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:, moveUp:</string>
    <key>Duplicate Current Line Down</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
    <key>Delete Current Line</key>
    <string>deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:</string>
</dict>

Restart Xcode | Preferences | Key Bindings | All

search "Duplicate"

search "Current"

then input your favorite shortcut key bindings.

I learnt this trick from somewhere in SO - thx a lot to the original poster. It works on Xcode 6.3 latest beta, Xcode 6.2, 6.1 and previous Xcode versions.

like image 20
Golden Thumb Avatar answered Sep 20 '22 13:09

Golden Thumb