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. 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. Cmd + D (remove any other bindings for this key)

@ShehanMadushanka

This comment has been minimized.

Copy link

@ShehanMadushanka ShehanMadushanka commented Nov 3, 2018

Working fine 👍

@othyn

This comment has been minimized.

Copy link

@othyn othyn commented Nov 14, 2018

Perfect, thank you!

@cosmindolha

This comment has been minimized.

Copy link

@cosmindolha cosmindolha commented Nov 24, 2018

Thank you!

@Sidelobe

This comment has been minimized.

Copy link

@Sidelobe Sidelobe commented Nov 30, 2018

Personally, I think these shortcuts should not "trash" the clipboard, i.e. the clipboard's contents should be left untouched through such an operation. Also, being able to apply the duplication to whole paragraphs is extremely useful to me.

The following will keep your clipboard intact and works for multiple lines / paragraphs:

<key>User</key>
<dict>
    <key>Duplicate Current Lines Down</key>
    <string>selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:</string>
    <key>Delete Current Line</key>
    <string>deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:</string>
</dict>
@Ahmad-Atef-Ali

This comment has been minimized.

Copy link

@Ahmad-Atef-Ali Ahmad-Atef-Ali commented Jan 4, 2019

Awesome!
💯 💯

@almatri

This comment has been minimized.

Copy link

@almatri almatri commented Jan 12, 2019

Thanks @Sidelobe, This is the right way to do it,

It is really annoying when the clipboard keeps changing when duplicating something on Xcode

@byassine52

This comment has been minimized.

Copy link

@byassine52 byassine52 commented Feb 7, 2019

Amazing❗️ This should be built in into Xcode decades ago❗️

@gertjanjansen

This comment has been minimized.

Copy link

@gertjanjansen gertjanjansen commented Mar 14, 2019

Thanks!

@mtmccrea

This comment has been minimized.

Copy link

@mtmccrea mtmccrea commented Mar 31, 2019

Thanks @Sidelobe!
Here's a variation to also Duplicate Current Lines Up. And for both duplicate up/down, select the resultant duplicate lines so you can more quickly navigate to either the beginning or end fo the duplicated portion.

    <key>User</key>
    <dict>
        <key>Duplicate Current Lines Down</key>
        <string>selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yankAndSelect:</string>
        <key>Duplicate Current Lines Up</key>
        <string>selectParagraph:, delete:, yankAndSelect:, moveToBeginningOfText:, yankAndSelect:</string>
        <key>Delete Current Line</key>
        <string>deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:</string>
    </dict>
@mkimi-lightricks

This comment has been minimized.

Copy link

@mkimi-lightricks mkimi-lightricks commented Apr 8, 2019

Every Xcode update /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist should be copied from the old Xcode version to the installed one. Did anyone find a good solution for that?

@fake-car

This comment has been minimized.

Copy link

@fake-car fake-car commented Aug 7, 2019

Amazing

@Lodo4ka

This comment has been minimized.

Copy link

@Lodo4ka Lodo4ka commented Aug 7, 2019

thx for keybindgs

@ivanbogomoloff

This comment has been minimized.

Copy link

@ivanbogomoloff ivanbogomoloff commented Aug 22, 2019

works fine thnx

@JoseM111

This comment has been minimized.

Copy link

@JoseM111 JoseM111 commented Dec 1, 2019

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

@norrisboat

This comment has been minimized.

Copy link

@norrisboat norrisboat commented Dec 11, 2019

Thanks!!

@barclayd

This comment has been minimized.

Copy link

@barclayd barclayd commented Jan 1, 2020

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

@manee1982

This comment has been minimized.

Copy link

@manee1982 manee1982 commented Jan 14, 2020

Thank You, works fine in xcode 11

@Mor4eza

This comment has been minimized.

Copy link

@Mor4eza Mor4eza commented Jan 18, 2020

Works fine in xcode 11.3
Thanks

@bart96-b

This comment has been minimized.

Copy link

@bart96-b bart96-b commented Mar 2, 2020

xCode 11.3.1
Thanks!

@KhoaN56

This comment has been minimized.

Copy link

@KhoaN56 KhoaN56 commented Mar 20, 2020

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

@Sidelobe

This comment has been minimized.

Copy link

@Sidelobe 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

This comment has been minimized.

Copy link

@Sidelobe 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

This comment has been minimized.

Copy link

@alex1392 alex1392 commented Apr 27, 2020

Works fine in Xcode 11.4.1
Thanks!

@tsubasahiroe

This comment has been minimized.

Copy link

@tsubasahiroe tsubasahiroe commented May 21, 2020

Thanks!!

@alex011235

This comment has been minimized.

Copy link

@alex011235 alex011235 commented Jul 12, 2020

👍

@tispokes

This comment has been minimized.

Copy link

@tispokes 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.