views:

2893

answers:

5

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?

+5  A: 

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.

Ashley Clark
+3  A: 

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.

Typeoneerror
A: 

@TypeOneError - how do you find the available keys which work? If I try the exact same .dict file as you gave, except I use an ^$L instead of ^$K, then these key combinations don't get picked up by XCode. I don't get it, since ^$L is not already in use.

What do you mean by "after some experimentation"? Does this mean that getting this to work is a bit of a trial-and-error?

The "$" is a shift. You are pressing Control + Shift + L correct? By "experimentation" I merely meant trying out actions until it functioned exactly how I envisioned. Read the "erasetotheleft.com" post that was linked in the "accepted" answer.
Typeoneerror
Hi, thanks for your quick response. Yes I've read that useful page and understand what $, ^, ~ and @ mean. When I substitute your ^$D for my preferred ^$L, then *all* key combinations in the dict file are ignored. Even in the erasetotheleft.com page there is some remaining uncertainty around order of key modifiers, so that's two issues I can't see a documented understanding in the public domain for. If anyone can shed light on this it would be most appreciated.
Gaaah! Now it works. I will never know why it wasn't working yesterday - all I've done is sleep the Mac and now XCode is obeying everything I throw at it in the dict file, exactly as you've written up. Great! Thanks for your help.
+6  A: 

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.

mclin