Xcode: Delete line hot-key
Asked Answered
F

8

19

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?

Flute answered 25/1, 2009 at 0:27 Comment(3)
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?Flute
Is this helpful? Xcode duplicate/delete linePrerogative
See https://mcmap.net/q/130433/-xcode-duplicate-delete-line to reproduce the eclipse's delete line CTRL+D behavior.Subvert
S
14

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.

Sillimanite answered 25/1, 2009 at 17:34 Comment(2)
Unfortunately, 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/*.idekeybindingsFlute
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.Garniture
F
30

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.

Flute answered 26/1, 2009 at 0:17 Comment(3)
I used Kailash's answer here: https://mcmap.net/q/130434/-how-do-i-create-a-delete-line-keyboard-shortcut-in-xcode-8-the-xcode-3-solutions-do-not-work-anymore and it worked for me in Xcode 4.Zitazitah
This is just god damn beautiful! :-D +1, also, working in XCode 4.5.2 for me.Conative
There's more information on customizing these key bindings to your desire on Apple's developer site.Garniture
S
14

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.

Sillimanite answered 25/1, 2009 at 17:34 Comment(2)
Unfortunately, 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/*.idekeybindingsFlute
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.Garniture
P
13

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.

Platinumblond answered 19/11, 2011 at 11:51 Comment(0)
C
8

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.

Catalonia answered 4/4, 2010 at 0:50 Comment(3)
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.Irrigation
In Xcode 4, this is under Xcode => Preferences... => Key Bindings, but I don't see 'Delete Line' there.Mime
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.Chapple
I
4
<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.
Inger answered 31/10, 2014 at 8:12 Comment(1)
+1. This works on Xcode 6. I just changed the key sequence to moveToBeginningOfLine:, deleteToEndOfLine:, deleteBackward:.Seventeenth
H
1

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>
Huck answered 17/6, 2017 at 11:27 Comment(0)
H
0

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:

Huambo answered 1/9, 2012 at 15:48 Comment(0)
H
0

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.

Here's a screenshot of what I ended up with

Horthy answered 9/1, 2016 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.