Almer/Blank Labs

From FlashDevelop to xCode : Duplicate Line

So over the past month or so, I’ve decided to delve into the wonderful world of Objective-C and Cocoa. I’ve been using xCode as my primary IDE, and so far I love it. However, one nagging issue about xCode is that there is no “Duplicate Line” or “Delete Line” hotkey as there is in FlashDevelop, which I’ve been using for years for most of my AS development. While I *could* try to get used to the old “Option” + drag method or copy/paste, I found myself spoiled by the quick hotkey action that FD offers (which by default is Ctrl+D for duplicating and Ctrl+Shift+D for deleting. There’s also a handy “transpose” hotkey, which I won’t go into). So doing some research, I came across a couple of options. One option requires that you create a User Script in xCode and assign whatever hotkey to that. You can find more information HERE. I decided not to use that solution because there was no “Delete line” code source, and I frankly wasn’t in the mood to learn Ruby in order to customize my own script (yet). The second option is to create a Dictionary file and customize your script very easily using the Mac OSX Key Binding documentation as a reference. Here’s what you do: - Create a new .dict file in the following directory : home/Library/KeyBindings/PBKeyBinding.dict Note : If the KeyBindings directory does not exist, you’ll need to create it. - Add the following code:
{
"^$K" = (
"selectLine:",
"cut:"
);

"^$D" = (
"selectLine:",
"copy:",
"moveToEndOfLine:",
"insertNewline:",
"paste:",
"deleteBackward:"
);
}
- Save the file, restart xCode if it was already open. So with the above code, placing the cursor within any line then hitting the key combo ‘Control + Shift + K’ will delete that entire line, and hitting ‘Control + Shift + D’ will duplicate it. Thanks to TypeOneError for posting the initial info on this. One thing that I did change from the original code is that I added a “deleteBackward” command at the end of the Duplicate Line section. This will place the cursor back at the end of the line just created, rather than at the beginning of a new line (which is more like what FD’s behavior is). The obvious beauty of this is that you can recursively hit the hotkey combo and duplicate lines without having to place your cursor back up into the previous one. You can further customize or add other bindings by referencing the Mac OSX Key Bindings documentation. I may delve into this a bit more and see about a ‘Transpose’ hotkey, which switches placement of the last two selected lines of code, but I’m in no hurry.