Xcode duplicate line

Today I wrote a very small plugin for Xcode 5 that delete and duplicate lines and selections.

You can download it from my GitHub repo: XcodeKit Plugin - https://github.com/ptfly/XcodeKit


Update 2020

Works on Xcode versions: 11.3.1 too

Works in Xcode versions: 7.1, 7.3, 7.3.1, 8.0, 8.1, 8.2, 8.3, 9.0, 9.1, 9.2, 10.0, 10.1, 10.2.1

Instruction

  1. Give permissions to edit the following files. Most of the time OS will not allow you to edit the files.

for 11.3.1 version

sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources//IDETextKeyBindingSet.plist

sudo chmod 777 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/

below 11.3 versions


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

sudo chmod 777 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/
  1. Edit the the file, by writing the following command: (Open it in sublime or text editor if you can)

for 11.3.1

open /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist

for below versions

open /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
  1. Append the following xml snippets:
<key>custom</key>
<dict>
    <key>Duplicate Current Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
    <key>Duplicate Lines</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>
    <key>Delete Line</key>
    <string>selectLine:, deleteBackward:</string>
</dict>

Screenshots

enter image description here

and Restart Xcode and go to Preferences - Key Bindings, search for the command we typed (Duplicate Lines&Duplicate Current Line).

enter image description here

Thats it.

For unix people to note :-

sudo chmod 755 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/ 

Ok, I've came up with this:

Version for XCode 11:

selectParagraph:, delete:, undo:, moveRight:, yankAndSelect:
  • doesn't touch clipboard
  • works with multiple lines
  • selects added lines, so can be used multiple times without reselecting
  • fixed selection in XCode 11
  • works better with undo

Since I'm applying this change after every XCode update, I've made a script for that. Possibly it can cause some damage, but I've tried to make it relatively safe. It makes a copy of the file, and then replaces line with its copy and additional command using perl. Requires XCode to be installed at /Applications/Xcode.app

cp /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist ~/Desktop/IDETextKeyBindingSet.plist; sudo perl -pi -e 's/\<string\>selectWord:\<\/string>/'$'\<string\>selectWord:\<\/string\>\n\t\t\<key\>Duplicate Selection\<\/key\>\n\t\t\<string\>selectParagraph:, delete:, undo:, moveRight:, yankAndSelect:\<\/string>/g' /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

Older version (I'm not sure if the new one is compatible with older versions, so I'll keep it here):

selectParagraph:, delete:, yank:, moveToBeginningOfLine:, setMark:, yank:, moveToBeginningOfLine:, moveLeft:, selectToMark:

and script for older version to avoid manual file editing:

cp /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist ~/Desktop/IDETextKeyBindingSet.plist; sudo perl -pi -e 's/\<string\>selectWord:\<\/string>/'$'\<string\>selectWord:\<\/string\>\n\t\t\<key\>Duplicate Selection\<\/key\>\n\t\t\<string\>selectParagraph:, delete:, yank:, moveToBeginningOfLine:, setMark:, yank:, moveToBeginningOfLine:, moveLeft:, selectToMark:\<\/string>/g' /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

edit: minor fix edit2: changed to paragraph selection edit3: added version for XCode 11


  1. Go to this folder which contains dark side of the force:

    • Xcode 4.2 or prior: /Developer/Library/PrivateFrameworks/IDEKit.framework/Resources
    • Xcode 4.3 or later: /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources.
  2. Open IDETextKeyBindingSet.plist.

  3. Add a new dictionary and a new command item as the screenshot below (name them what you want): Editing the plist

    That's: selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:

  4. Restart Xcode and go to Preferences - Key Bindings, search for your command.

  5. Set a key combination for the command: Key Bindings

  6. Finally unleashed the power of key bindings on Xcode... Enjoy it!