Xcode duplicate line

2019-01-03 19:57发布

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.

16条回答
We Are One
2楼-- · 2019-01-03 20:16

Adding Duplicate lines Works for Xcode version 7.1 , 7.3, 7.3.1 8.0 , 8.1, 8.2, 8.3 AND 9.0, 9.1, 9.2, 10.0, 10.1 as well

Give persiomission to edit the following files, Most of the time OS will not allow you to edit the files.

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

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

Edit the following file,

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

by writing the following command.

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

append the following xml snipets.

<key>Customized</key>
  <dict>
    <key>Duplicate Lines</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>
    <key>Duplicate Current Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
</dict>

screenshot of the edited file.

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-03 20:19

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 ;)

查看更多
甜甜的少女心
4楼-- · 2019-01-03 20:19

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. It was 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:

<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.

Based on what I have read, I set up my Xcode for these shortcuts and blogged about it at: http://jixte.com/2016/03/30/line-duplicate-and-deletion-in-xcode-7-x/

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

查看更多
干净又极端
5楼-- · 2019-01-03 20:19

Ok, I've came up with this:

selectParagraph:, delete:, yank:, moveToBeginningOfLine:, setMark:, yank:, moveToBeginningOfLine:, moveLeft:, selectToMark:
  • doesn't touch clipboard
  • works with multiple lines
  • selects added lines, so can be used multiple times without reselecting

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.

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

查看更多
你好瞎i
6楼-- · 2019-01-03 20:25

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
查看更多
对你真心纯属浪费
7楼-- · 2019-01-03 20:26

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>
查看更多
登录 后发表回答