Coming from Eclipse and having been used to duplicate lines all the time, it\'s pretty strange finding out that Xcode has no such function. Or does it?
I know it\'s
As Xcode 4 makes this difficult to implement using key bindings, I have resorted to using Keyboard Maestro (Quickeys should work well too, but it's not fully Lion compatible). For e.g., here is my Keyboard Maestro shortcut (bound to ⌘-⇧-D):
http://bit.ly/thC6yf
I know that this thread is quite old but I found another solution using BetterTouchTool, you can execute the Ctrl+A, Ctrl+K, Ctrl+K sequence in one shortcut by configuring like this in BTT:
(Use the Attach Additional Action Button to append the second and third shortcut)
PS: Coming from Eclipse where Cmd+D is delete line ^^
The solution by damien.flament works great and you may need set output to "Discard Output" see the pic below
P.S: I don't know how to comment his answer.
As said above, close XCode and insert following to ~/Library/Application Support/Xcode/Key Bindings/ <user>.pbxkeys
inside <dict>
in <key>text</key>
section:
<key>^D</key>
<array>
<string>moveToLeftEndOfLine:</string>
<string>deleteToEndOfLine:</string>
</array>
<key>^K</key>
<array>
<string>selectLine:</string>
<string>copy:</string>
<string>moveToEndOfLine:</string>
<string>insertNewline:</string>
<string>paste:</string>
<string>deleteBackward:</string>
</array>
Start XCode and enjoy CTRL-SHIFT-D and CTRL-SHIFT-K.
If you want CTRL-D and CTRL-K, use ^d
and ^k
in key definitions instead. Ensure there are no duplicate key bindings in the file.
Works fine for me with XCode 3.2
To delete a line: Ctrl-A to go to the beginning of the line, then Ctrl-K to delete it, and another time Ctrl-K to remove the empty line. (I do not use Xcode very often, but I'm used to that in Emacs and other text inputs with Emacs-like bindings, and it seems to work in Xcode too.)
And to duplicate a line: I don't know of many programs that have a command for that, but usually I just use Copy+Paste - in Xcode it's CUA-like: Ctrl+A to go to the beginning of the line, Shift+↓ to select it, Command+C to copy and Command+*V to paste twice (once overriding the line and once appending to it).
(from a person that types and edits text all the time, so often in different programs, and occasionally gets pissed at having to distract himself with a dumb widget while making a little correction in a text input, that he just cannot avoid remembering these sequences and habits)
I tried the key bindings solution, but it I couldn't get it to work. However editing my XCode key bindings works like a charm. Here's how I made it.
This solution does not alter the contents of the Clipboard!
Open the XCode Key Bindings:
In the Edit User Scripts Dialog:
Duplicate Line Up:
using terms from application "Xcode"
tell first text document
set {startLine, endLine} to selected paragraph range
if startLine > 1 then
set theText to (paragraphs startLine through endLine)
set theText to (theText as string)
make new paragraph at beginning of paragraph (startLine) with data theText
set selected paragraph range to {endLine + 1, endLine + endLine - startLine + 1}
else
beep 1
end if
end tell
end using terms from
Duplicate Line Down:
using terms from application "Xcode"
tell first text document
set {startLine, endLine} to selected paragraph range
if endLine < (count paragraphs) then
set theText to (paragraphs startLine through endLine)
set theText to (theText as string)
(* delete (paragraphs startLine through endLine) *)
make new paragraph at beginning of paragraph (endLine + 1) with data theText
set selected paragraph range to {startLine, endLine}
else
beep 1
end if
end tell
end using terms from