155

There is a Duplicate command in the Edit Menu (with a default shortcut of D), but it is (as Halley pointed out) meant for duplication in the Interface Builder part of Xcode.

So, how do you (easily) duplicate a line in Xcode 4?


Related question (with a working answer) for Xcode 3 ... and which does not work for Xcode 4.


Why not just copy & paste?

Because it is tedious and entails too much hand-acrobatics:

either (1): moving to line beginning and then pressing ⇧^E, then copying with ⌘C, moving to new line, alligning cursor, and finally pasting with ⌘V;

or (2): ^A (set cursor to line begining), ^SPACE (set mark), ^E (set cursor to line ending), ⇧^W (Select to Mark; customized), copy, new line, etc.

As Frank Schröder (in the related question) put it:

The whole point is NOT to use the Cmd-C/Cmd-V shortcuts.

Community
  • 1
  • 1
Blaz
  • 3,548
  • 5
  • 26
  • 40
  • 1
    Select the line and copy paste? – Daniel Apr 22 '12 at 17:19
  • 3
    @Dani, CP is just too much work (and line duplication isn't something for which need arises only rarely). :) I've updated the question. – Blaz Apr 22 '12 at 19:58
  • 1
    Command-D is for control duplication in Interface Builder. – Hailei Apr 23 '12 at 16:01
  • @Halley, this is good to know. And is it the equivalent of ⌥-mouse-drag (which also copies interface elements)? – Blaz Apr 23 '12 at 16:26
  • @courteous I think so. Modifier Keys section in http://disanji.net/iOS_Doc/#documentation/DeveloperTools/Conceptual/IB_UserGuide/GestureGuide/GestureGuide.html says that "pressing the Option key during drag-and-drop operations copies the selected objects instead of moving them." – Hailei Apr 23 '12 at 16:33
  • @Halley, I was just wondering whether there was *any difference* between the two (⌘D and ⌥-mouse-drag) with regard to outlets, etc. – Blaz Apr 23 '12 at 16:45
  • For the latest **Xcode 9.3**, see my [accepted answer](https://stackoverflow.com/questions/39816443/xcode-9-x-line-duplication-and-deletion) – duongel Apr 18 '18 at 13:56
  • And third good reason I can think of is not over writing the clipboard which might be needed to edit the new duplicate lines. Its pretty simple and handy in Android-Studio. – Pervez Alam Nov 11 '20 at 11:24
  • In Xcode 14 the key Binding duplicate (Edit Menu) exists – maxmitz Jan 05 '23 at 09:49
  • As @byaruhaf noted in January with xcode 14, cmd-D with nothing selected duplicates the current line. – MiB Mar 04 '23 at 05:33
  • In addition in xcode 14, if you select multiple lines with the shift and arrow keys for example, cmd-D will duplicate all selected lines. – MiB Mar 04 '23 at 06:00

22 Answers22

198
  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!

Kaptain
  • 1,358
  • 12
  • 20
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • 1
    I don't know why but it crashes my Xcode. Note that the file is located here `/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/` and you need to change permissions recursively `sudo chmod 777 -R *` otherwise you cannot edit this file... – Mars Robertson Apr 28 '12 at 22:55
  • 3
    @MichalStefanow, the location that you wrote is actually **the same location** that `/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources` alias points to. Otherwise, I've successfully followed Halley's steps on version 4.3.2 ... I suggest you backup up the original file and then open it in Xcode itself (and not in TextEdit). Also, regarding the permissions: changing them (only for the file itself; no recursion needed) in Finder worked just fine. – Blaz Apr 29 '12 at 11:13
  • 29
    Big thanx! For lazy users - **selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:** – Vlad Tsepelev Jun 08 '12 at 11:59
  • 15
    If your xCode crashes! Close xCode, open file with any text editor and add your dictionary: `Custom Duplicate Current Line selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward: ` – Vlad Tsepelev Jun 08 '12 at 12:04
  • 2
    Works for me in XCode 4.4.1! Had some trouble changing permissions (finder or chmod 777 and start with sudo from terminal didn't work, for some reason). I copied the file to somewhere else, edited it, and overwrote the original one, that worked. – User Sep 01 '12 at 15:33
  • In Xcode 4.5 works but I could not change the permissions. I used sudo nano console text editor to edit a file. – Rafał Sroka Oct 04 '12 at 09:03
  • In Xcode 4.5.1, I added the new command in the plist, but it did not show up in the Key Bindings dialog. The plist file was successfully saved, though. – Rich Apodaca Oct 22 '12 at 20:39
  • chmod command in @MichalStefanow 's comment doesn't work. Put -R before 777. `sudo chmod -R 777 *` – trkaplan May 01 '13 at 15:00
  • 1
    what is this magical syntax? where do these commands "moveToEndOfLine, insertNewLine, etc" come from? – tmsimont Jul 05 '14 at 14:41
  • These are actually Objective-C methods of NSResponder class. See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html – Hailei Jul 15 '14 at 02:08
  • 1
    For some reason in Xcode 7.1.1 the `insertNewLine:` statement doesn't work. Use `insertLineBreak:` instead: `selectLine:, copy:, moveToEndOfLine:, insertLineBreak:, paste:, deleteBackward:` – Ivan Caravanio Dec 03 '15 at 10:06
  • 2
    I am android dev and when I started to trying ios dev I realized how android studio (or intellij idea) is good. – IliaEremin Jul 05 '16 at 18:16
  • 1
    I had to add this instead to simulate sublime text's `Duplicate Line` functionality in Xcode 9.2: `selectLine:, copy:, moveToEndOfLine:, moveToBeginningOfLine:, paste:, insertLineBreak:, deleteBackward:, moveLeft:` – Alex Jun 06 '18 at 18:16
  • This configuration works the best way for me: `moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:` – Kaptain Jul 11 '18 at 11:20
  • This will **overwrite** your copy/paste buffer! Use `yank` instead. – lenooh Dec 20 '18 at 21:09
  • 1
    For XCode 10.1, I had to use the following string: **moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, setMark:, moveToBeginningOfLine:, selectToMark:, yank:**. Big thanks to @tmsimont – marcelosalloum Jan 09 '19 at 11:39
127

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/ 
Dominic
  • 62,658
  • 20
  • 139
  • 163
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
  • It works like a charm! I edited the `IDETextKeyBindingSet.plist` and added the above dictionary and then mapped the keys in Preferences of XCode. – Randika Vishman Jul 04 '16 at 07:51
  • 1
    For XCode 10.1, I had to use the following string: **moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, setMark:, moveToBeginningOfLine:, selectToMark:, yank:**. Big thanks to @tmsimont – marcelosalloum Jan 09 '19 at 11:39
  • Unfortunately IDETextKeyBindingSet.plist disappeared in 11.3.1. Does anybody know where to find it (or some modern stuff which replaced it)? – nrx Jan 15 '20 at 11:12
  • sudo chmod 755 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/ to reset the folder permissions and chmod 644 for the file for unix noobs like me. – Dirk Bester Apr 30 '20 at 19:07
  • This worked fine all the way thru v13.1. I just tried it with v13.2 and it didn't work. Specifically, it's the key mapping for Duplicate Current Line that I couldn't get to work properly. – fsb Dec 14 '21 at 16:04
  • I just googled how to duplicate line in xcode. Was expecting a simple answer like command + D like in IntelliJ. This however looks like shooting fish in a barrel. – NewestStackOverflowUser Dec 15 '21 at 22:08
  • For debugging, check whether the plist is malformed with `plutil ` and check whether you use xcode-beta or xcode. – Fabian Mar 26 '22 at 17:34
32

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

Rychu
  • 1,028
  • 1
  • 9
  • 20
20

EDIT: Aug 16, 2022 After every update to Xcode I have to fix this duplicate line issue. Again did it today. The entry for line duplication from the IDETextKeyBindingSet.plist file always gets deleted but not the other custom entries. But in the Xcode's key bindings my shortcut remains. So every time I have to open the IDETextKeyBindingSet.plist file and add these lines under the <key>Insertions and Indentations</key> section under <dict>, and restart Xcode to make line duplication shortcut work.

<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToBeginningOfLine:, paste:, moveToEndOfLine:</string>

EDIT: Jul 12, 2020 After update to Xcode 11.5 again my duplicate line key entry from the plist file was gone. I had to add it again. The key binding was still there though.

EDIT: Sep 17, 2019 After update to Xcode 10.3 again my duplicate line key entry from the plist file was gone. I had to add it again. The key binding was still there though.

EDIT: May 28, 2019 After update to Xcode 10.2 again my duplicate line key entry from the plist file was gone. I had to add it again. The key binding was still there though.

EDIT: Dec 23, 2018 After update to Xcode 10.1 all my shortcuts broke again. But something different was that after updating the plist file for line duplication, I didn't have to define my key bindings, they were already there. Strange.

EDIT: OCT 14, 2018 Xcode 10: Somebody high up at Apple really loves copy/paste, and makes sure that with every version of Xcode update, the line duplication is removed from everyone's install. We developers have been extremely frustrated by this nonsense going on in Xcode since forever, the only IDE with no line duplication option. I just updated to Xcode 10, and as you guessed it - all my shortcut keys are removed along with my line duplication functionality. However, it is fixable like in the original answer. A slight difference this time is that you will need the following line:

<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToBeginningOfLine:, paste:, moveToEndOfLine:</string>

EDIT: Xcode 9: I had to redo my shortcuts and line duplication and deletion, but good thing is that now you can edit IDETextKeyBindingSet.plist again. This high up Apple guy must be on vacation when they released this version.

EDIT: Xcode 8: Buggy, you can't modify the IDETextKeyBindingSet.plist file at all. Xcode resets it on start. This high up guy at Apple is really determined to make us use copy/paste instead of simple line duplication like in all other IDEs out there.

EDIT: Xcode 7.1 and still there are no shortcuts for line duplication and deletion. This is the only IDE I have ever worked with with no such shortcuts. And I can't imagine life of a developer without these two critical shortcuts.

Here is how it can be done:

First we need to modify a plist file that lives inside the Xcode.app bundle (needs sudo to edit)

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

Then look for <key>Insertions and Indentations</key> and under the <dict> tag add the following:

<key>Insert New Line Below</key>
<string>moveToEndOfLine:, insertNewline:</string>
<key>Insert New Line Above</key>
<string>moveUp:, moveToEndOfLine:, insertNewline:</string>
<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>

Then find the and these inside the section <key>Deletions</key> under the <dict> tag:

<key>Delete Current Line</key>
<string>selectLine:, delete:</string>
<string>selectLine:, delete:</string>

enter image description here

Now Open/Restart Xcode, go to Preferences and go to the “Key Bindings” tab. Use the search bar to filter and look for the new shortcuts. Then click in front of the shortcut description to assign it a shortcut of your liking. Make sure your shortcut doesn't conflict with existing shortcuts.

enter image description here

zeeshan
  • 4,913
  • 1
  • 49
  • 58
  • 1
    I think it finally works! Today I went to the edit shortcuts and selected it. It claimed the default is now Cmd-D. Although it had no binding. But Binding Cmd-D now actually works as expected. No longer do we have to run scripts to rebind the goddamn thing after an update? Although maybe it will blank out again. Although at least it's no longer complete stupidity with a trip through command scripting etc. It just actually worked and duplicated as expected. What a wondrous time to be alive. – Dirk Bester Dec 18 '22 at 03:54
  • @DirkBester I haven't tested it yet but if it does, that's great. But can't understand what took Apple forever to fix this. – zeeshan Dec 19 '22 at 18:53
17

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

peerless
  • 558
  • 3
  • 11
  • I just downloaded and installed this - You can use System preferences to set keyboard shortcuts for the commands too. Very nice! – Orion Edwards Jan 27 '14 at 21:01
  • Best solution there is because this one won't break you current clipboard. To add keybindings: System preferences -> keyboard -> shortcuts -> app shortcuts -> '+'. There choose Xcode and menu title for example 'Delete Selection / Line' – Sjoerd Perfors Feb 18 '14 at 09:12
  • I just installed this. AWESOME, for some reason though, my "Duplicate" menu under edit is greyed out. Even though I have selected a line in the editor, cursor and have tried selecting whole line. – John Ballinger Mar 13 '14 at 22:51
  • it doesn't work for me. The "Duplicate" is greyed out and keyboard shortcut won't work also. – SquareBox Jan 03 '18 at 03:45
15

@Hailei's answer was pretty good, but had the unfortunate side-effect of clobbering the clipboard contents (leaving it populated with the contents of the duplicated line), which is precisely the part of the manual copy/paste approach that I was trying to avoid.

So, I changed the command sequence to this:

moveToBeginningOfLine:,
deleteToEndOfLine:,
yank:,
insertNewline:,
moveToBeginningOfLine:,
yank:

The 2 yank:s might not work as intended if you have NSTextKillRingSize set to something aside from 1, but I'm pretty sure 1 is the default, and you have to do some non-trivial work to change it (it requires adding an entry to ~/Library/Preferences/.GlobalPreferences.plist).

Kelan
  • 2,296
  • 1
  • 15
  • 17
  • I suggest using the command `defaults write -g NSTextKillRingSize 1` instead of playing with the GlobalPreferences.plist. ;) – dbernard Jan 11 '13 at 20:31
  • @amas It's likely you need to copy the file somewhere else before you modify it then overwrite the original file, unless you want to risk changing permissions on parent folder(s). – nipponese May 30 '13 at 14:44
  • this almost works, but for some reason a bunch of whitespace gets added to the end of the duplicated line.. any idea why? – tmsimont Jul 05 '14 at 14:21
  • 5
    if fixed it with this modification : `moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, setMark:, moveToBeginningOfLine:, selectToMark:, yank:` – tmsimont Jul 05 '14 at 15:10
9

Why duplicate a single line? Duplicate whole paragraph is better. My sequence:

Duplicate Current Paragraph

selectParagraph:,
delete:,
yank:,
moveToBeginningOfParagraph:,
yank:,
moveUp:,
moveToEndOfParagraph:

Delete Current Paragraph

selectParagraph:,
delete:,
moveUp:,
moveToEndOfParagraph:

Works fine in Xcode 4.6

BB9z
  • 2,432
  • 1
  • 30
  • 36
  • Agreed, manipulating paragraphs is superior to lines. Everyone, make your editor narrow enough to wrap text and try to use the line commands. – Logan Moseley Dec 12 '13 at 16:50
  • How small is your window that your code is wrapping. Consider breaking your code up into smaller lines if that is happening so often. – quemeful Aug 01 '16 at 19:24
9

I'm wondering why nobody has mentioned using along with the arrow keys () to both move the cursor and to select entire lines (by adding ).

I regularly copy entire lines (duplicate) by moving the cursor to the beginning of a line using + (instead of ctrl+A) and then use ++ (instead of ctrl++E) to select the whole line to the end and then copy/paste.

I've run into other threads where noone seems to use these keyboard shortcuts. They are global in OS X and I use them all the time! Much less hand-acrobatics ;)

Baldur
  • 227
  • 4
  • 12
  • This requires much more "hand-acrobatics" than duplicating with only 1 action/key combination... it's the way I duplicate text in a text editor or webbrowser, yes, but I expect a bit more from IDE. – User Nov 16 '13 at 10:07
  • 1
    And also, with duplicate line shortcut you don't overwrite the content of the clipboard. Many times I you copy something, duplicate code and want to paste it - with your method you have to copy it again. – User Nov 16 '13 at 11:13
  • Well yes. But less than the way suggested by the OP. I can agree that a single command would be best. I just thought I would point out an alternative. – Baldur Nov 16 '13 at 11:21
  • 1
    this is still better than nothing out-of-the-box. i wish that cmd+C/X with no selection would just copy/cut the whole line like other editors do... – manroe Jun 02 '15 at 21:22
  • @Baldur This is what I do as well. cmd+left cmd+shift+right cmd+c cmd+right enter cmd+left cmd+p. At 7 keystrokes it can easily be done in less than 3 seconds, however, it's still quite tedious relative to ctrl+d like in many text editors. It's unfortunate this isn't build in to xcode, or that in general macs and apple software don't have better hot keys. – csga5000 Sep 07 '15 at 22:27
  • Seems this is the most legit solution without butching config files which in some way speeds up the action. Still wondering why it doesn't work out-of-the-box like in some same-class IDEs (greetings Android Studio). – kkaun Oct 15 '19 at 18:31
  • The point here is to be productive. You copy a snippet that then replaces a part of some line that you just duplicated without affecting the snippet in the copy buffer. Select, Cmd-C, click, Cmd-D (Duplicate), select, Cmd-V. Yes, everyone knows how to tediously type your 5 billion commands and move the mouse around a lot. We are instead looking for Duplicate Line. Something you will use multiple times a day while coding. – Dirk Bester May 22 '20 at 21:10
5

Xcode finally added this functionality in Xcode 14 using ⌘ + D , please see the release notes here:

When editing code, the Edit > Duplicate menu item and its corresponding keyboard shortcut now duplicates the selected text — or the line that currently contains the insertion point, if no text is selected. (8614499) (FB5618491)

Video Demo

byaruhaf
  • 4,128
  • 2
  • 32
  • 50
  • I just stumbled over this myself in xcode 14.2, one of the first times using it as an editor. I've only used the CLI Developer tools before. – MiB Mar 04 '23 at 05:29
  • In addition, if you select multiple lines with the shift and arrow keys for example, cmd-D will duplicate all selected lines. – MiB Mar 04 '23 at 05:59
4

Just to complement Hailei's answer, the commands that worked the best for me were:

selectLine:, copy:, moveRight:, moveLeft:, insertNewline:, paste:, deleteBackward:

His suggested commands were copying the desired line two lines below, for me.

Rod
  • 424
  • 2
  • 7
  • 17
4

Duplicate menu item and its corresponding keyboard shortcut now duplicate the selected text

Xcode 14 Beta 3 Release Notes

New Features in Xcode 14 Beta 3 When editing code, the Edit > Duplicate menu item and its corresponding keyboard shortcut now duplicate the selected text — or the line that currently contains the insertion point, if no text is selected. (8614499) (FB5618491)

nkalvi
  • 91
  • 1
  • 3
3

I would consider revising the command sequence to:

selectLine:,
copy:,
moveToEndOfLine:,
insertNewline:,
deleteToBeginningOfLine:,
paste:,
moveToBeginningOfLine:, 
deleteBackward:

The added deleteToBeginningOfLine maintains the indenting of the duplicated line.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
user570753
  • 46
  • 2
  • This worked great once I realized I had to exchange the (good looking) newlines with a space after the comma. – McUsr Feb 08 '14 at 12:14
2

insertLineBreak needed at least for me to make copy on new line, without it i got copy on same line

Vladimir Kruglov
  • 196
  • 1
  • 19
2

I had a big trouble to unlock .plist file on MacOS Sierra and Xcode 9, even @Tim decision didn't work for me. If you have the same problem, go to terminal and do this:

cd /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/
sudo chmod -R 777 *
nano IDETextKeyBindingSet.plist

Few other possible ways to do this are also described here.

Then in nano editor click ^W and type 'insertions' to jump to 'Insertions and Indentations' part. Just under <dict> tag paste this xml-snippet:

<key>Duplicate line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertNewLine:, paste:, deleteBackward:</string>

Press control + X, then Y, then Enter to save the file in nano editor.

UPD: I came up with a better decision, which adds an additional line before duplication (useful when you want to duplicate a line which has an another line just below it):

<key>Duplicate line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertLineBreak:, moveUp:, paste:, moveToBeginningOfLine:</string>
joliejuly
  • 2,127
  • 1
  • 21
  • 24
1

Here is my Delete (CTRL+D) and Duplicate (CMD+SHIFT+D) entries from my IDETextKeyBindingSet.plist. (in /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources )

I just set these up in Xcode 4.6.1 (based on answer from user570753 above and elsewhere)

    <key>Custimozed</key>
<dict>
    <key>Delete Current Line</key>
    <string>deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:</string>
    <key>Duplicate Current Line</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, deleteToBeginningOfLine:, paste:, moveToBeginningOfLine:, deleteBackward:</string>
</dict>
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
Rob
  • 1,037
  • 1
  • 13
  • 20
1

As for Xcode 11 none of the above worked for me. The first answer had a weird behavior as it was inserting the line after the next line. The following answer, however, worked for me.

<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>
Ben
  • 953
  • 12
  • 27
1

in Xcode 14

1- Open Setting in Xcode ((Command + ,))

2-Find Edit Menu section in KeyBinding tab

enter image description here

3- Select Duplicate row and assign a shortcut key

Milad Ahmadi
  • 1,019
  • 12
  • 19
  • A few days before your post @byaruhaf noted cmd-D with nothing selected duplicates the current line in xcode 14. – MiB Mar 04 '23 at 05:31
0

Any sublime text users that would like to match sublime's duplicateLineUp and duplicateLineDown in XCode, please add the following to your IDETextKeyBindingSet.plist file

<key>Line Duplications</key>
<dict>
    <key>Duplicate Current Line Above</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:, moveUp:</string>
    <key>Duplicate Current Line Below</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>
</dict>
internet-nico
  • 1,637
  • 19
  • 17
0

In case anyone else is denied access to edit IDETextKeyBindingSet.plist you can give yourself read/write permissions by:

sudo chmod 777 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
Tim
  • 1
0
<key>Duplicate Lines</key>    
<string>selectLine:, copy:, moveToBeginningOfLine:, paste:</string>

Duplicate line(s) supported. (This works in Xcode 9.2)

Luminoid
  • 11
  • 1
0

For XCode 13.

Map "Vim Mode" to ^⌘V.

Then: ^⌘V yyp ^⌘V

Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
0

this works 100% like IntelliJ IDEA and Android Studio

Read accepted answer and add this

selectLine:, copy:, moveToBeginningOfLine:, paste:, moveToEndOfLine:

instead of

selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:
Nasib
  • 1,173
  • 1
  • 13
  • 23