Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xcode - Duplicate Line key binding

Xcode line duplicate

Bind keys to duplicate lines in Xcode

  1. Close Xcode

  2. Open below directory in Finder with Cmnd + Shift + G

/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/
  1. Open IDETextKeyBindingSet.plist with a text editor.

  2. Add this in:

<key>Duplication</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>
  1. Open Xcode and go to Xcode preferences -> Key Bindings -> Text tab -> Scroll till you see Duplication

  2. Click on Duplicate Current Line, add a shortcut for it, eg. Cmnd + D (remove any other bindings for this key)

@JoseM111
Copy link

JoseM111 commented Dec 1, 2019

Works great @Sidelobe and everybody else that had a version of doing this!

@norrisboat
Copy link

norrisboat commented Dec 11, 2019

Thanks!!

@barclayd
Copy link

barclayd commented Jan 1, 2020

This is awesome, thank you! Xcode should add this as an out of the box default

@manee1982
Copy link

manee1982 commented Jan 14, 2020

Thank You, works fine in xcode 11

@Mor4eza
Copy link

Mor4eza commented Jan 18, 2020

Works fine in xcode 11.3
Thanks

@bart96-b
Copy link

bart96-b commented Mar 2, 2020

xCode 11.3.1
Thanks!

@KhoaN56
Copy link

KhoaN56 commented Mar 20, 2020

I couldn't find Duplicate inside if the text tab in Key Bindings of Xcode 11.1 Preferences. 😓

@Sidelobe
Copy link

Sidelobe commented Mar 20, 2020

I couldn't find Duplicate inside if the text tab in Key Bindings of Xcode 11.1 Preferences. 😓

It won't show up until you add the new entry to the IDETextKeyBindingSet.plist file --- then it will show up:
(it's under "Custom Commands" here because I named the key like that in my edit of said .plist file)

image

@Sidelobe
Copy link

Sidelobe commented Mar 20, 2020

Since we have to repeat this procedure every time Xcode is updated 🙄 , I've created a shell script to (semi-) automate this task:

I've shared it here: https://gist.github.com/Sidelobe/2e8c0244b91f9a41a148e5d419e49772

Hope you guys find it as useful as it's been for me 😃

@alex1392
Copy link

alex1392 commented Apr 27, 2020

Works fine in Xcode 11.4.1
Thanks!

@tsubasahiroe
Copy link

tsubasahiroe commented May 21, 2020

Thanks!!

@alex011235
Copy link

alex011235 commented Jul 12, 2020

👍

@tispokes
Copy link

tispokes commented Nov 26, 2020

i use this in a shellscript (just a part of it) to insert automatically after xcode update

ex /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist <<eof
5 insert
<key>Duplication</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>
.
xit
eof

@zhonglaoban
Copy link

zhonglaoban commented Dec 17, 2020

nice!it worked when i reboot the Xcode.

@mitchtabian
Copy link

mitchtabian commented Mar 16, 2021

You are a god thank you

@damazus
Copy link

damazus commented Mar 20, 2021

it works
thank you

@georgevlahos
Copy link

georgevlahos commented Apr 9, 2021

Yes, you are God.

@hishd
Copy link

hishd commented Apr 28, 2021

It works... Thanku loads...!!!

@samerce
Copy link

samerce commented Jun 14, 2021

thank you!! and thanks to sidelobe for the variation i wanted!

@Maduki-tech
Copy link

Maduki-tech commented Jun 17, 2021

Thanks this works Perfekt. Very Easy to setup

@kennenfromchina
Copy link

kennenfromchina commented Oct 22, 2021

Thanks!

@Z3r0CooLE
Copy link

Z3r0CooLE commented Jan 8, 2022

Could move the file to a new location and replace with a symbolic link, set permission on the file for Xcode to read only. Or even the permissions alone would be enough to possibly prevent updates from overwriting the changes. But with a symbolic link it could still be replaced in an update and after you check for any breaking changes in the updated version you can just replace it with a fresh symbolic link.

@Sidelobe
Copy link

Sidelobe commented Jan 8, 2022

@Z3r0CooLE : I see what you mean -- but I figured the "check for any breaking changes in the updated version" would take me far longer than re-adding these changes to the new file ;-)

@khoilecontact
Copy link

khoilecontact commented Jan 11, 2022

It works well. Thanks!!

@Z3r0CooL619
Copy link

Z3r0CooL619 commented Jan 11, 2022

@Sidelobe not really.. because you should check for any breaking changes before inserting new lines into a file too.. while it’s not likely that you’d break it by adding a hot key property, it is possible some conflicting entry was added to the new file that you could break with the addition. Either way it’s best to check before blindly changing. Checking also takes only a second as there should be little to no changes in the file between updates aside from your own. A diff would give a handful at lines at most; one of those changes would be your change ironically saving you the time to remember/find and type it out since you could copy it from there. I’d say write a shell script to add those lines to that file but if you did this, you could also have it check for differences before doing so which would take even less time.

Honestly, adding the lines to the new file is the the more likely route to encounter a breaking change. If you were completely replacing the updated file with the modified file it would at least be removing any conflicting or breaking additions they added to the updated version which likely aren’t detrimental to the operation of the software to remove.. simply inserting into the file without seeing what might have changed first is actually worse than skipping the diff step from my approach. But if you enjoy wasting time and possibly causing headaches when creating a conflict the software isn’t expecting and has no way of reporting the cause of… sure skip the most important step of the process. I mean… you could even have the script make a temp new file with the additions then compare its hash to your previous.. if it matches rename the new file to the XCode path.

@ConfusedLolo
Copy link

ConfusedLolo commented Jan 14, 2022

Thanks!! It's such a shame that devs still have to search for it online and modify config file to enable this feature on their own, way to go Xcode.

@polanskiy
Copy link

polanskiy commented Jan 26, 2022

moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:
that's what i was looking for an hour, thanks!

@xgqfrms
Copy link

xgqfrms commented Mar 27, 2022

awesome!

@lifution
Copy link

lifution commented Apr 13, 2022

Amazing! 👍🏻

@Kadmiv
Copy link

Kadmiv commented Apr 23, 2022

<key>Duplicate Lines</key>
<string>selectLine:, deleteToEndOfLine:, undo:, yank:, moveToBeginningOfLine:, yank:</string>

<key>Duplicate Selection</key>
<string>deleteToEndOfLine:, undo:, yank:, moveToEndOfLine:, insertNewline:, yank:</string>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment