Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode duplicate/delete line

People also ask

How do I select a line in Xcode?

Shift + Control + Click First, hold Shift and Control (^) and then click where you want to select multiple lines and then start typing. One small caveat: unlike other text editors where you can click that same line to unselect the multi-line selection, Xcode doesn't support that yet.

How to delete a line in Xcode?

Delete Entire Line ( ⌘ + D ) To define custom keybindings, go to Xcode settings (press ⌘ + ,), navigate to the Key Bindings tab, and use the filter field to search for “Delete Line”. Double click into the Key field, then press your preferred key combo. I use ⌘ + D (for Delete Line).


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


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.

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://funkworks.blogspot.it/2013/03/republishing-of-wwwerasetotheleftcompos.html


Delete a line like eclipse CTRL+D (tested on Xcode 4.5.1) :

First of all, change these rights :

sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

sudo chmod 777 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/

Open /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist with Xcode himself and add this new entry :

deleteToBeginningOfLine:, moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToBeginningOfLine:

enter image description here

Restart Xcode and open Xcode > Preferences > KeyBindings. Find your macro and define a shortkey :

enter image description here


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.

This solution does not alter the contents of the Clipboard!

Open the XCode Key Bindings:

alt text

In the Edit User Scripts 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.
  4. If you do not want to restart XCode, you seem to have to remove and re-add the script. Be sure that you have "Output" set to "Discard Output", otherwise you will have a "(null)" in your source file
  5. I slightly modified the scripts a little bit more to have the right lines selected:

Duplicate Line Up:

using terms from application "Xcode"
tell first text document
    set {startLine, endLine} to selected paragraph range

    if startLine > 1 then
        set theText to (paragraphs startLine through endLine)
        set theText to (theText as string)
        make new paragraph at beginning of paragraph (startLine) with data theText
        set selected paragraph range to {endLine + 1, endLine + endLine - startLine + 1}
    else
        beep 1
    end if
end tell
end using terms from

Duplicate Line Down:

using terms from application "Xcode"
tell first text document
    set {startLine, endLine} to selected paragraph range
    if endLine < (count paragraphs) then
        set theText to (paragraphs startLine through endLine)
        set theText to (theText as string)
        (* delete (paragraphs startLine through endLine) *)
        make new paragraph at beginning of paragraph (endLine + 1) with data theText
        set selected paragraph range to {startLine, endLine}
    else
        beep 1
    end if
end tell
end using terms from

alt text


OK, so I had this working for a while and then suddenly it broke. Now I have combined from different posts here and found a solution that works for XCode 6.3.1.

  1. Go to /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/ with Finder.

  2. find the folder named Resources. Right Click it, press Get Info, unlock the sharing & permissions setting (By pressing the lock) and set it to Read & Write for all.

  3. Enter the folder and find the file IDETextKeyBindingSet.plist. Right Click it, press Get Info, unlock the sharing & permissions setting (By pressing the lock) and set it to Read & Write for all.

  4. Open the file with XCode (Default)

  5. Find deletions, right click and add a new row. Name it for example Delete Line. In the value field, type: selectLine:, deleteBackward: . (This can also be done for duplicate line: selectLine:,copy:,moveToEndOfLine:,insertNewline:,paste:,deleteBackward:)

  1. Start XCode, go to preferences / keybindings and search for Delete Line. Set shortcut. Enjoy.

To Delete Line in Xcode 10.1, please follow the steps of below screenshot to add a shortcut key.

enter image description here


As Xcode 4 makes this difficult to implement using key bindings, I have resorted to using Keyboard Maestro (Quickeys should work well too, but it's not fully Lion compatible). For e.g., here is my Keyboard Maestro shortcut (bound to ⌘-⇧-D):

http://bit.ly/thC6yf