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条回答
迷人小祖宗
2楼-- · 2019-01-03 20:28

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

查看更多
家丑人穷心不美
3楼-- · 2019-01-03 20:29
  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!

查看更多
放荡不羁爱自由
4楼-- · 2019-01-03 20:30

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

查看更多
The star\"
5楼-- · 2019-01-03 20:32

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.

查看更多
登录 后发表回答