If you come from other text editors like VSCode or Sublime Text or from a Jetbrains IDE, you will know how customizable those tools are. You can customize almost anything from appearance to keybindings. The latter is the subject of this tip. One keybinding I use the most in PHPStorm, for instance, is the Duplicate Line keybinding. I haven’t found anything like that in Xcode; you have to select the whole line, copy it, tap on “enter” then “CMD+V” to paste it. Four steps to do such a trivial thing, gosh! Let’s see how to add a custom keybinding to do just that. First, you’ll have to open
IDETextKeyBindingSet.plist, located in your Xcode folder, and open it with root privileges.
# Open this file with VSCode sudo code /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
Once you put in your password, an XML file will be opened, and if you go through it, you’ll find some keywords. Go to the bottom of the file, and before the last closing </dict> tag, you can add this:
<key>Ibrahima Custom Key Bindings</key> <dict> <key>Duplicate Current Line</key> <string>selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:</string> <key>Delete Line</key> <string>selectLine:, deleteBackward:</string> <key>End Statement</key> <string>moveToEndOfParagraph:, insertNewline:</string> </dict>
As you can see, I’ve added three custom keybindings: the first one to duplicate the line the cursor is on, another one to delete the whole line and the last to end the statement, which just creates a new line at where the cursor is currently placed (so handy 🤗).
Now you can save the file, quit Xcode if it was opened, and open it again. Then go to the
Key Bindings tab of Xcode preferences window. Use the filter to search for the name of the keybindings you added previously in the
.plist file. You might resolve all the conflicts as well before using your new keybindings. After that, you can use these to delete/duplicate a line with a simple keystroke and voilà!
You can find below in the video the whole process.
What’s the catch 🤔?
You’ll need to do this process again when you install a new version of Xcode because the
.plist file we edited will be replaced by a new one after each Xcode installation (or update). I suggest saving all your custom keybindings somewhere so that you can reproduce the steps easily.
Hope this will help. Peace 👊