views:

3219

answers:

8

Coming from Eclipse and having been used to duplicate lines all the time, it's pretty strange finding out that Xcode has none such function. Or does it?

I know it's possible to change the system wide keybindings but that's not what I'm after.

TIA

A: 

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.

Paul Tomblin
+7  A: 

To delete a line: Control-A to go to the beginning of the line, then Control-K to delete it, and another time Control-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: Control+A to go to the beginning of the line, Shift+Down Arrow to select it, Command+C to copy and Command+Y 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)

Tom Alsberg
+15  A: 

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: http://flexgraphix.com/blog/?p=62

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://www.erasetotheleft.com/post/mac-os-x-key-bindings/

Frank Schröder
This is gold! I've been looking for this for a while now. Thanks!
Reed Olsen
Many internets to you sir.
Şafak Gezer
This should be the real answer. Btw - if you want to import the cmd-d (delete line) shortcut from Eclipse, you'll have to first change the Add Bookmark shortcut under the edit menu in XCode
Chris R
Totally jealous that this got 14 upvotes and my post (where this snippet came from) got two! http://stackoverflow.com/questions/476953/xcode-delete-line-hot-key/478583#478583 ;)
Typeoneerror
+1  A: 

Correct path (Snow Leopard, Xcode 3.2.1): ~/Library/Application Support/Xcode/Key Bindings/PBKeyBinding.dict

Zenedith
+1  A: 

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.

Mathieu Godart
A: 

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)
damien.flament
A: 

The solution by damien.flament works great and you may need set output to "Discard Output" see the pic below

Userscripts setting

P.S: I don't know how to comment his answer.

palaniraja
A: 

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.

Open the XCode Key Bindings:

alt text

In the Key Bindings Dialog:

  1. Duplicate the "Move Line Down" script and rename it
  2. Duplicate the "Move Line Down.scpt" file, rename the script, select file via (double click) in Script Editor
  3. Edit the script (Opens "AppleScript Editor") and remove the "delete (paragraphs startLine through endLine)" passage.

alt text

Colins