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条回答
Juvenile、少年°
2楼-- · 2019-01-03 20:05

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.

查看更多
走好不送
3楼-- · 2019-01-03 20:07

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>
查看更多
Rolldiameter
4楼-- · 2019-01-03 20:08

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>
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-03 20:09

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

查看更多
地球回转人心会变
6楼-- · 2019-01-03 20:13

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

查看更多
混吃等死
7楼-- · 2019-01-03 20:14
<key>Duplicate Lines</key>    
<string>selectLine:, copy:, moveToBeginningOfLine:, paste:</string>

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

查看更多
登录 后发表回答