85

In previous versions of Xcode it was possible to create a key binding to delete the current line. There were different solutions and they are described for example here:

Xcode: Delete line hot-key

Xcode duplicate/delete line

http://bigdiver.wordpress.com/2009/09/11/configure-homeend-key-bidings-on-mac-os-x/

http://www.betadesigns.co.uk/Blog/2010/02/03/custom-xcode-shortcuts/

All solutions include modifying one of these files:

~/Library/Application Support/Xcode/Key Bindings/*.pbxkeys
~/Library/KeyBindings/DefaultKeyBinding.dict
~/Library/KeyBindings/PBKeyBinding.dict

A good resource for the existing shortcuts in Xcode 4 is http://cocoasamurai.blogspot.com/2011/03/xcode-4-keyboard-shortcuts-now.html . There are many listed regarding deletion, but none for "delete line".

BUT, these solutions do NOT work since Xcode 4.

Update: Issue is still the same as of Xcode 5.1.1

Update: Issue is still the same as of Xcode 6.1 GM Seed 2

Update: Still applies to Xcode Version 7.3 (7D175) as of 26th April 2016

Update: Six years later Xcode 8.3 has a built-in solution. See answer below.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Klaas
  • 22,394
  • 11
  • 96
  • 107
  • In the meantime I decided to create a keyboard macro with http://www.keyboardmaestro.com/ . – Klaas May 02 '12 at 00:35
  • I guess there are some solutions listed below. I'm personally using a Keyboard Maestro macro. – Klaas Oct 21 '12 at 21:15

13 Answers13

100

After a lot of search here and there, I came to the following solution:

Press and hold control key, and then press AKK.

(Note: second K deletes the empty line).

Actually it is a combination of two commands: Move to Beginning of Paragraph & Delete to End of Paragraph. You may also use/set other key(s) and also may use/set/change key bindings for other command(s) e.g. Move to Beginning of Line & Delete to End of Line. I used the above (default) key bindings as they suited me.

Edit:

To delete more than one consecutive lines, first press and hold control key, and then pressA (just to go to the beginning of the paragraph/line), then press and hold K till all the lines are deleted (this would work as the cursor remains at the beginning).

Just to make task easy (for me), I have set commandD for Delete to End of Paragraph and home/end to go to beginning/end of the paragraph.

Vasu
  • 4,862
  • 8
  • 42
  • 48
  • 2
    Nice solution, but I really looking for a single shortcut. If I want to delete 5 lines in TextWrangler for example it's only pressing five times cmd-d. Strangely enough cmd-d is not used in Xcode... – Klaas May 24 '12 at 18:48
  • @Klaas, please see my updated answer. It may not be what you are expecting, still its a good solution. – Vasu May 29 '12 at 06:50
  • as soon as your answer gets some upvotes, I'll accept it since Xcode 4 will not have this included anytime soon. – Klaas May 29 '12 at 14:49
  • xCode is terrible ! - Android developer using android studio. – M'hamed Jan 27 '17 at 21:05
  • longest shortcut ever see CTRL+A+K+K to delete current line – canbax Feb 21 '18 at 09:25
88

Looks like Xcode 8.3 finally added the Delete Line command

By default, no keyboard shortcut is assigned to it, so you will need to add one yourself in:

  1. Open Xcode > Preferences > Key Bindings
  2. Search for Delete Line
  3. Add a keyboard shortcut in the Key column (eg, controloptionD)

Xcode Key Bindings

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
59

Got it! This works in XCode 4.3 through 4.5 and requires no extra applications and is XCode specific.

This solution is basically the same as the Duplicate Line command described here:

Edit the plist file

It's at /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

Note that:

  • the linked answer suggests using XCode for this, but I find that this crashes XCode
  • this path is for Lion - it may vary for other OS versions
  • you may have to sudo or change permissions to edit this file
  • close XCode before editing

Add a new <dict> element

Add this text just ABOVE the close of the <dict> at the bottom of this file

    <key>Custom</key>
    <dict>
      <key>Delete Current Line In One Hit</key>
      <string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string> 
    </dict>

Note that:

  • This is inside the existing <dict> so there is a </dict></plist> after this
  • This associates the named action "Delete Current Line In One Hit" to the three key actions in the string. I played around with different options here to get the right combination. For instance, if you try moveToBeginningOfLine, deleteToEndOfLine instead of vice versa, then using it on empty lines will delete the line and the entire next line. Which is unpleasant

Save this and open XCode

Add a new key binding in XCode

See the screenshots for this in the linked answer or do this:

  1. Open XCode Preferences
  2. Select Key Bindings
  3. Click on All
  4. In the SEarch box type "Delete" and search for "Delete Current Line in One Hit"
  5. Add your new binding.

This should do it but for one caveat. I could never get XCode to save the new binding. The same problem is described here though my workaround had a slight variation:

  1. Create a duplicate of the Default bindings (hit the + button at the bottom left of the Bindings window and create a duplicate)
  2. Set the new binding in the duplicate
  3. Close the preference dialog (that's the variation!)
  4. Open preferences again and make a new duplicate of the old duplicate and delete the old duplicate.

What a rigmarole, eh? But it works. Now I have Ctrl+D mapped to delete the entire current line in XCode 4.3 on Lion.

Community
  • 1
  • 1
Rhubarb
  • 34,705
  • 2
  • 49
  • 38
  • Update: one limitation that I've come to find irritating with this solution is that it only deletes one line at a time, even if you have a whole lot selected. My natural tendency is to select 3 lines and hit ctrl+D and wipe them all - but it doesn't. – Rhubarb Nov 30 '12 at 19:45
  • Note that if you install a new version of XCode this won't be preserved whereas your usual key binding customizations will. (I "upgrade" by renaming the old XCode.app and installing the new one afresh). The actual key bindings are in ~/Library... so they are preserved, but this new entry is not – Rhubarb Nov 30 '12 at 19:47
  • Yes it works in Xcode 5 and saves the key binding so that might encourage others to think it's worth the bother :) – RedYeti Dec 18 '13 at 21:36
  • @Rhubarb To delete several lines at once by having selected text in those lines and just hitting the shortcut just add a `delete:` in front of the command sequence: `delete:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:`. Would be great if you edit this into your answer. – Maic López Sáenz Oct 13 '14 at 21:52
  • 2
    This works in Xcode 6.2 (6C86e), and I also didn't need the workaround to make Xcode "save" the key binding. Yay! :) – Will Hains Nov 20 '14 at 14:11
  • This method works perfectly in Xcode 6.4: I used it to create a "Duplicate Line" key binding. Thank you! – MustangXY Sep 09 '15 at 19:23
  • This solution has worked for me but every time I update XCode I lose the shortcut. What can I do? – lorenzo Oct 23 '15 at 12:15
  • Very worth the effort, especially if you're constantly switching between Xcode and Android Studio/Eclipse! – DiscDev Jan 17 '16 at 23:05
  • When you try to navigate to **/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist** and you don't see the **Contents** folder you might have to right-click and select **Show Package Contents**. – Mark Moeykens May 04 '16 at 16:54
  • Instead of `moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:` use `selectLine:, delete:` It's more simple and avoid malfunction. – RabbitHole May 18 '16 at 02:15
  • @Ethan Please see my answer for a solution on Xcode 8.3 ;) – Paulo Mattos May 10 '17 at 15:25
16

I found a temporary workaround for Xcode 9.

Open the Xcode/Content/Frameworks/IDEKit.framework/Resources/IDETextK‌​eyBindingSet.plist and add the lines below.

It mimics the action which happens in Xcode 8 with limited support. It only can remove just one line but I'm satisfied somehow. I hope this will be fixed soon.

<key>Customized</key>
<dict>
    <key>Delete Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, moveDown:, deleteBackward:</string>
</dict>

After restart Xcode, you can see the customed item in Key Bindings like below.

enter image description here

Bind and use it!

Edit) I think below one is better than original one.

<string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string>
Wooseong Kim
  • 1,871
  • 2
  • 21
  • 19
  • 2
    Xcode 9 right answer over here. It Works Xcode 9 Version 9.0 (9A235) – uspython Sep 14 '17 at 07:55
  • Why did this break in Xcode 9? This worked for me kinda. I had to replace the value for the existing Delete Line string value instead of adding a customized key. – Alexander Smith Oct 06 '17 at 20:47
13

I use BetterTouchTool, it's pretty cool:

enter image description here

borisdiakur
  • 10,387
  • 7
  • 68
  • 100
  • Good alternative,modify plist not always good.and easy to lost setting.Help Apple will make it suitable. – Albert.Qing Dec 14 '14 at 04:23
  • Very good alternative to solve the problem. Unfortunately is not an atomic action so if you want to undo, you have to press Cmd Z two times (the two deletes). Also the window manager add on it's useful (I'd also recommend Spectacle). – Rudolf Real Dec 17 '14 at 13:52
  • 1
    BTT is an all around awesome tool. Also this method is pretty future proof as long as Apple has the actions that you can combine to do what you're looking for. – SuperDuperTango Feb 06 '15 at 16:57
  • 1
    I hadn't considered using BTT (as something I already use!) Much better than editing something inside the app. Plus you can sync configs between machines, which is something I need. – Relequestual Nov 17 '15 at 21:35
9

Rhubarb's answer is correct and works but one thing, there is more simple command in plist file. Follow all instructions of Rhubarb's but

instead of

<string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string>

use below

<string>selectLine:, delete:</string>

This is more clear and throw away any possibility of malfunction.

I want to write this as a comment, but my "reputation" doesn't allow me to do that.

Community
  • 1
  • 1
RabbitHole
  • 336
  • 3
  • 6
  • Mixing your solution with [this answer](http://stackoverflow.com/questions/551383/xcode-duplicate-delete-line#answer-27171722) solves the problem. Thanks indeed – marcelosalloum Feb 27 '15 at 17:50
8

try this:

  1. First open Xcode->Preferences

  2. Select Key Bindigs

  3. search Delete Paragraph

  4. Change Delete Paragraph to your shortcut

enter image description here

Bary Levy
  • 584
  • 5
  • 6
  • 1
    I have no idea why others are not using this SIMPLE solution. Please vote for it as the others are so much more complex. This one is also how you SHOULD do it: through a key binding – checklist May 09 '17 at 07:13
  • Nothing happens execute `Delete Paragraph` in Xcode Version 9.0 (9A235). Did you test it in Xcode 9? – Wooseong Kim Sep 15 '17 at 01:59
3

If you are a Keyboard Maestro user, just create a macro that does the following:

  • Command Left Arrow
  • Shift Down Arrow
  • Delete

I assigned it to Command-D, and made it available only in Xcode.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Paul Waldo
  • 1,131
  • 10
  • 26
1

An easy solution for XCode 5 and 6:

Install alcatraz: http://alcatraz.io/ then use it to install the "XCodePlus delete line" plugin:

Window > Package manager

There are lots of other awesome plugins as well.

Simon Epskamp
  • 8,813
  • 3
  • 53
  • 58
1

It looks as though XCode now uses a plist file versus a dict file, and the new format doesn't support adding multiple commands to a key binding. There also doesn't appear to be a way to add custom commands to bind to either. The only way I've found to get something working is by setting a key binding for the separate actions, such as ^D for deleteToEndOfLine:, then ^L for moveToBeginningOfLine:, giving you ^L+^D to give you the combined effect.

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • There are already some defaults to handle this. ^k for "delete to end of line", cmd-delete for "delete to beginning of line". – Klaas May 11 '11 at 12:22
  • In the regular (Global) keybindings.dict file we put separate commands into an array, and that works. – McUsr Feb 08 '14 at 09:43
1

If you don't mind having that key binding in other apps, you could try setting it in the general Cocoa key bindings. This has an explanation.

LaC
  • 12,624
  • 5
  • 39
  • 38
  • 2
    I tried this already, but the global key bindings do not work in XCode 4. I tested them in TextMate and they work there. So I did the right modifications. – Klaas May 11 '11 at 12:12
1

seems as though it can't be done now... feel free to correct me if I am wrong... but here is my new solution...

go to xcode preferences, then the key bindings tab. duplicate the default set, so that you wont be mad at me when the suggestion that i make breaks something that you will use.

find 2 keys that are adjacent and not used for any commands that you use... ";","'" perhaps.(I don't use command n for new file, so I picked n,m)

set the first key to "Move to Begging of Line" and the second key to "Delete to end of line"

then use that sequence to delete a line...

alternate would be set something to select line, then just use delete.

Grady Player
  • 14,399
  • 2
  • 48
  • 76
-1

First open Xcode->Preferences

Select Key Bindigs

Then search Delete

Change the Delete The End Of Line to your shortcut(I prefer CMD + D)

Thats AllHere is the screenshot

LoGoCSE
  • 558
  • 5
  • 13