问题
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 possible to change the system wide keybindings but that's not what I'm after.
回答1:
The whole point is NOT to use the Cmd-C
/Cmd-V
shortcuts. I have the same issue coming from IntelliJ, and being able to just duplicate lines with Cmd-D
and delete them with Cmd-Y
is a big time saver.
It's been bugging me ever since. However, it looks like someone else has found a solution that works.
In short, create a file ~/Library/KeyBindings/PBKeyBinding.dict
with the following content and restart Xcode.
{
"^$K" = (
"selectLine:",
"cut:"
);
"^$D" = (
"selectLine:",
"copy:",
"moveToEndOfLine:",
"insertNewline:",
"paste:",
"deleteBackward:"
);
}
This will create two shortcuts: Ctrl-Shift-K for deleting the current line and Ctrl-Shift-D for duplicating the current line. Please note that this will only work if you are NOT using a custom key binding set in Xcode. Switch to "XCode Default" and things should work. Tested on XCode 3.2 on Snow Leopard.
More information on Mac OS X key bindings: http://funkworks.blogspot.it/2013/03/republishing-of-wwwerasetotheleftcompos.html
回答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)
回答3:
Delete a line like eclipse CTRL+D (tested on Xcode 4.5.1) :
First of all, change these rights :
sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist
sudo chmod 777 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/
Open /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist
with Xcode himself and add this new entry :
deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:
Restart Xcode and open Xcode > Preferences > KeyBindings. Find your macro and define a shortkey :
回答4:
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 the "Move Line Down" script and rename it
- Duplicate the "Move Line Down.scpt" file, rename the script, select file via (double click) in Script Editor
- Edit the script (Opens "AppleScript Editor") and remove the "delete (paragraphs startLine through endLine)" passage.
- If you do not want to restart XCode, you seem to have to remove and re-add the script. Be sure that you have "Output" set to "Discard Output", otherwise you will have a "(null)" in your source file
- I slightly modified the scripts a little bit more to have the right lines selected:
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
回答5:
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
回答6:
OK, so I had this working for a while and then suddenly it broke. Now I have combined from different posts here and found a solution that works for XCode 6.3.1.
Go to
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/
with Finder.find the folder named Resources. Right Click it, press Get Info, unlock the sharing & permissions setting (By pressing the lock) and set it to Read & Write for all.
Enter the folder and find the file IDETextKeyBindingSet.plist. Right Click it, press Get Info, unlock the sharing & permissions setting (By pressing the lock) and set it to Read & Write for all.
Open the file with XCode (Default)
Find deletions, right click and add a new row. Name it for example Delete Line. In the value field, type:
selectLine:, deleteBackward:
. (This can also be done for duplicate line:selectLine:,copy:,moveToEndOfLine:,insertNewline:,paste:,deleteBackward:
)
- Start XCode, go to preferences / keybindings and search for Delete Line. Set shortcut. Enjoy.
回答7:
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 ^^
回答8:
Triple click to select the whole line, command-c to select and command-v twice to duplicate it. It's not an Xcode function, it's just part of the OS.
回答9:
Use user scripts.
There are scripts to move and delete lines yet. You have to define key binds for those scripts (menu Scripts -> Edit User Scripts..., under xCode 3.2).
To duplicate line, you have to make your own script. But it's very simple ! Duplicate the "Move Line Down" script and remove the line which delete selected text :
delete (paragraphs startLine through endLine)
回答10:
There's a solution for XCode4 line duplication over here
I've also added line deletion to the plist:
<key>Remove Line</key>
<string>selectLine:, deleteBackward:</string>
回答11:
No need to modify Xcode. You can just use the Xcode extension Linex
回答12:
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.
回答13:
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
回答14:
This is possible using BetterTouchTool. CMD+D will duplicate a line. Create a new shortcut like this:
Use the Attach Additional Action Button to append the second and third shortcut
回答15:
To Delete Line in Xcode 10.1, please follow the steps of below screenshot to add a shortcut key.
回答16:
If you already have customised your Xcode key bindings, you'll have to edit the file: ~/Library/Application Support/Xcode/Key Bindings/<user>.pbxkeys
. Just add the two arrays (described above) to the Root/text
dictionary.
回答17:
Three step to delete current line (see too long, but do very fast :D)
Cmd + → : move to end of line
Cmd + Delete : delete to begin of line
Delete : delete empty line
回答18:
We are on Xcode 9.0 and the keybindings are still not working. Sigh.
I found this gist very helpful so reposting it here for future reference:
- Open
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
- Add the following XML snippet to this file just above the bottom
</dict>
- Go to Xcode preferences -> Key Bindings -> Text tab -> Scroll till you see Duplication
- Click on Duplicate Current Line, add a shortcut for it, eg. Cmnd+D (resolve any duplicate bindings)
- Open Xcode
<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>
回答19:
For Xcode 6.1 got to Xcode > Preferences > KeyBindings > Text And there is "Delete to End of Line" and assign a key you want.
回答20:
XCodePlus delete line plugin, is a XCode that does this for you. It came pre installed with Alcatraz package manager...
https://github.com/payliu/XcodePlus
回答21:
Still there is no built-in way to duplicate lines, as of Xcode 9. And custom key bindings seem to have problems.
Update: Xcode 10.0 beta 6 (10L232m) is the same.
回答22:
Go to this address :
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet
and then copy IDETextKeyBindingSet.plist file in some where and then open copied file and insert
selectLine:, copy:, paste:, paste: like this picture enter image description here and save it and replace copied file in this address /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet
then restart xcode , open xcode prefences , go to keybinding tab , search for duplicate line(customized) and define short key for it like image below
[1]: https://i.stack.imgur.com/UTeZu.png
来源:https://stackoverflow.com/questions/551383/xcode-duplicate-delete-line