One feature I miss in Xcode is the ability to duplicate a line of text with a simple two-key combo. Sure, I could select the line and then copy and paste, but it’s far too fiddly. For example, let’s say I wanted to add another line to this array:
I would have to type:
+ move to start of line
+ (shift-down) select line
+ (copy)
(down) (deselect)
+ (paste)
And I would end up with this.
That’s far too much work.
Digging through Xcode’s preferences I couldn’t find anything that would help, but then I stumbled on a page called Customizing the Cocoa Text System that showed me how this could work.
So, I created a directory:
mkdir ~/Library/KeyBindings
And created the file: ~/Library/KeyBindings/DefaultKeyBinding.dict
{
"^d" = ("moveToEndOfLine:",
"deleteToBeginningOfLine:", // line in kill buffer
"yank:", // put back what was deleted
"insertLineBreak:",
"moveToBeginningOfLine:",
"yank:"); // duplicate line
}
Restarted Xcode, and now a ^d (control d) duplicates the line. And I can easily add the new element to the array.
If you want to experiment with your own tweaks, there is a list of all selectors. You’ll need to restart Xcode after each change to test your changes. I ran into a small problem with Xcode doing auto-intent after i added a newline with insertNewline:. Luckily, insertLineBreak has the same effect, but avoids the problem of extra whitespace getting added on the duplicated line.
If you prefer to download the file: http://xinsight.ca/blog/files/DefaultKeyBinding.dict.txt
Permanent link to this post: http://xinsight.ca/blog/xcode-trick-creating-a-shortcut-to-duplicate-a-line/
Older: A long road to a simple solution
Newer: Why In-App-Purchase is a Mistake