I'm looking for a way to map some hot-keys to "delete the line that my cursor is on" in Xcode. I found "delete to end of line" and "delete to beginning of line" in the text key bindings, but I am missing how to completely delete the line no matter what I have selected. TextMate has this functionality mapped to Ctrl+Shift+D and I'd like the same thing if possible. Any ideas?
-
Not to say that edit wasn't apt, but so far in using this site, I've only read titles of posts (esp. when I'm browsing "new" posts without any of "my tags" applied. Hence, putting the title of the app in question would get the attention of Xcode programmers. Thoughts? – typeoneerror Jan 25 '09 at 15:42
-
Is this helpful? [Xcode duplicate/delete line](http://stackoverflow.com/questions/551383/xcode-duplicate-delete-line/4032063#4032063) – leviathan Nov 07 '10 at 21:39
-
See http://stackoverflow.com/a/13049587/1771222 to reproduce the eclipse's delete line CTRL+D behavior. – Opena Oct 24 '12 at 13:16
8 Answers
Thanks for the help, Ashley. After some experimentation I mapped my favorite TextMate commands (duplicate line, delete line). I created the file ~/Library/KeyBindings/PBKeyBinding.dict and added the following:
{
"^$K" = (
"selectLine:",
"cut:"
);
"^$D" = (
"selectLine:",
"copy:",
"moveToEndOfLine:",
"insertNewline:",
"paste:"
);
}
The added "deleteBackward:" backs up one line after removing the line's content. You could probably just use "selectLine:" as well.

- 55,990
- 32
- 132
- 223
-
I used Kailash's answer here: [http://stackoverflow.com/questions/5834096/how-do-i-create-a-delete-line-keyboard-shortcut-in-xcode-4-the-xcode-3-solution](http://stackoverflow.com/questions/5834096/how-do-i-create-a-delete-line-keyboard-shortcut-in-xcode-4-the-xcode-3-solution) and it worked for me in Xcode 4. – Matt Becker Jun 28 '12 at 17:26
-
This is just god damn beautiful! :-D +1, also, working in XCode 4.5.2 for me. – Endophage Dec 12 '12 at 20:26
-
There's more information on customizing these key bindings to your desire on [Apple's developer site](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/eventoverview/TextDefaultsBindings/TextDefaultsBindings.html). – aleclarson Feb 25 '14 at 18:26
You can set up a system-wide key binding file that will apply to all Cocoa apps.
To do what you want it should like like this:
In your home folder, Library/KeyBindings/DefaultKeyBinding.dict
{
"^D" = (
"moveToBeginningOfLine:",
"deleteToEndOfLine:",
);
}
I believe if you only want it to apply to Xcode you can name the file PBKeyBinding.dict
instead but I didn't try that myself. You can read more about this system here and here.

- 8,813
- 3
- 35
- 35
-
3Unfortunately, Xcode4 doesn't seem to care about these files any longer. You can only map one command to one key combo in ~/Library/Developer/UserData/KeyBindings/*.idekeybindings – typeoneerror Aug 12 '11 at 18:57
-
It's been 4 years and your two links are unsurprisingly dead. [Here's newer information on custom key bindings from Apple's dev site.](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/eventoverview/TextDefaultsBindings/TextDefaultsBindings.html) – aleclarson Feb 25 '14 at 18:27
As I don't always work on the same xcode I prefer not to install scripts.
Xcode uses some sub-set of emacs commands. I use this approach to quickly delete a line. ^k (control-k) deletes from the cursor to the end of the line. Doing it twice also deletes the carriage return and takes up the next line. ^a takes you to the start of the line.
So to delete a complete line from the beginning you can use ^a^k^k.

- 3,317
- 2
- 21
- 20
I was looking for a solution to this, and I tried Ashley Clark's, but it turns out there's an easier option using an included User Script called delete Line.
- Open the weird menu to the left of 'help' that looks like a scroll.
- Choose Edit User Scripts...
- Click the Key Bindings tab
- Expand the Text section
- Double click the ⌘ column next to 'Delete Line' and type your hotkey. It may warn you that you stole it from some other command but that's fine.
Done! You can do the same for Move Line Up and Move Line Down if you're an Eclipse junkie like me.

- 3,599
- 1
- 20
- 18
-
Just a note that I tried 'move line up' and 'move line down', but unfortunately they're so glitchy and sluggish that they're really not worth using. – Matt Rix Dec 20 '10 at 20:55
-
In Xcode 4, this is under Xcode => Preferences... => Key Bindings, but I don't see 'Delete Line' there. – ma11hew28 Jun 18 '11 at 20:42
-
Nice and easy, but you DO have to remove the default usage of ⌘d (preferences, key bindings, edit->sort->add to bookmarks). After the initial load, it's quick enough. – SMBiggs Aug 29 '12 at 15:28
<key>Custom Keyword Set</key>
<dict>
<key>Delete Current Line In One Hit</key>
<string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string>
</dict>
I suggest to create your customized dictonary in your file IDETextKeyBindingSet.plist.
So:
- close Xcode;
- open Terminal;
- sudo nano /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist
- add new custom section, for instance code in top;
- save, exit and open Xcode;
- [Xcode > Preferences > Key Binding]
- search “Delete..” and create the new shortcut.

- 21,000
- 15
- 120
- 146
-
+1. This works on Xcode 6. I just changed the key sequence to `moveToBeginningOfLine:, deleteToEndOfLine:, deleteBackward:`. – jweyrich Nov 11 '14 at 13:53
For Xcode 9.0(beta), inserting customized key dictionary into IDETextKeyBindingSet.plist working fine for me.You need to restart XCode if already open and after next launch you will find new customized shortcuts under the KeyBindings menu.
<key>Customized</key>
<dict>
<key>Delete Rest Of Line</key>
<string>deleteToEndOfLine:</string>
<key>Delete Line</key>
<string>moveToBeginningOfLine:, deleteToEndOfLine:</string>
<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, moveToBeginningOfLine:, paste:</string>
</dict>

- 1,006
- 7
- 12
This works for me (Xcode 4.4.1):
Same steps like described here: Xcode duplicate line (Halley's answer)
But instead of:
selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:
Use:
selectLine:, moveToBeginningOfLine:, deleteToEndOfLine:
If you're having trouble in modern Xcode (which I was) solution for this in Xcode 7.2 is to do what Opena mentioned here with screenshots or in text form via Velthune's answer. Since I wanted a more direct command I simplified the command to:
selectLine:, delete:, moveToBeginningOfLine:
Of course in the Xcode's Preferences >> Key Bindings, you can just find the command double-click under the Key column and give it your own binding of Ctrl+Shift+D.

- 1
- 1

- 61
- 1
- 6