130

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

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

Sam
  • 7,252
  • 16
  • 46
  • 65
Summer
  • 1,494
  • 2
  • 10
  • 10
  • possible duplicate of [Xcode: Delete line hot-key](http://stackoverflow.com/questions/476953/xcode-delete-line-hot-key) – joshperry Nov 20 '11 at 00:25
  • 1
    this setup Works for 9.2 https://stackoverflow.com/a/34195483/1348522 – Zumry Mohamed Mar 29 '18 at 11:40
  • I'm continuously impressed with how bad XCode is given how much development Apple puts into it – Andy Ray Sep 05 '20 at 06:10
  • As in answered in [https://stackoverflow.com/a/75105314/3369207](https://stackoverflow.com/a/75105314/3369207) Xcode finally added this functionality in Xcode 14 using ⌘ + D, [please see the release notes here)[https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes] – byaruhaf Jan 13 '23 at 13:12

22 Answers22

111

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)

Scott Gardner
  • 8,603
  • 1
  • 44
  • 36
Tom Alsberg
  • 6,873
  • 3
  • 28
  • 14
  • 3
    This will work in all applications using Cocoa for text input, since its a feature of the Cocoa Text System. There are many more shortcuts like this, a list of them can be found here: http://www.hcs.harvard.edu/~jrus/Site/System%20Bindings.html :) – Ahti Nov 01 '11 at 13:08
  • 2
    'yank' should be ctrl + Y on modern Macs. – CarmeloS Dec 24 '13 at 13:02
  • Command-V to paste it and re-indent. *Control*-Y will also paste but not re-indent the pasted text. – Scott Gardner Jun 16 '15 at 12:10
93

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

johk95
  • 873
  • 10
  • 28
Frank Schröder
  • 954
  • 1
  • 7
  • 2
  • 5
    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 Aug 06 '10 at 23:29
  • 1
    @joshperry, anything that works with Xcode 4+? The `Duplicate` command (in `Edit -> Duplicate`) seems to be "always" grayed out (and not meant for duplicating line). – Blaz Apr 21 '12 at 13:18
  • I've used OS X since day one and never knew about key bindings. what a cool feature. Take a look at [this very nice GUI key bindings editor I happened upon](http://www.cocoabits.com/KeyBindingsEditor/). – Alex Gray Aug 31 '12 at 15:33
  • Found more information on [Apple's developer site](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/eventoverview/TextDefaultsBindings/TextDefaultsBindings.html). – aleclarson Feb 25 '14 at 18:24
  • sounded great, but seems like it didnt work for me in Xcode 5.0.2 :( – voghDev Jul 06 '14 at 18:19
  • For XCode 10.1, I had to use the following sequence of commands (for "^$D"): **moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, setMark:, moveToBeginningOfLine:, selectToMark:, yank:** – marcelosalloum Jan 09 '19 at 11:41
77

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

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Opena
  • 879
  • 6
  • 9
  • 5
    If you want to add a way to Duplicate a line, do the same, but when pasting the new value in the IDETextKeyBindingSet.plist file, paste this code "selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:". – Cla Apr 13 '13 at 18:43
  • +1 This solution worked but in Xcode 4.6.1 ^D conflicted with other existing key bindings. – bernardn May 12 '13 at 10:23
  • How can you get that for 'Duplicate current line'? – Markus Apr 11 '14 at 13:12
  • This is not working in xcode 6. Only first function is called from the list – Mrug Feb 25 '15 at 10:13
  • 1
    I love it, works like a charm for me under XCode 6.3.1. – cthulhu May 06 '15 at 12:25
  • This is the right answer! easy and works like a charm! – Jordi Puigdellívol Nov 20 '16 at 16:01
  • Delete Line works in Xcode 8.3.2 but there is an audible beep after executing the command. How do you get rid of that? – geoff May 31 '17 at 17:27
  • For XCode 10.1, I had to use the following string: **moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, setMark:, moveToBeginningOfLine:, selectToMark:, yank:** – marcelosalloum Jan 09 '19 at 11:40
  • Is there a way to make this duplicate the entire line, even if it's wrapped multiple times? Longer lines of code will only get until the end of the line with the line number and any wrapped code will not be duplicated – se_puede_dev Jan 09 '19 at 22:42
9

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

vivo
  • 333
  • 2
  • 8
leviathan
  • 11,080
  • 5
  • 42
  • 40
  • 2
    Not mentioned clearly above, there are existing scripts for Delete Line, Move Line Up, and Move Line Down. The accepted answer has the drawback that it only works if you have your keybindings set to XCode Default. Using scripts lets you keep your customized keybindings, since the keybindings for scripts are handled separately from those for the rest of XCode. – Pathogen Nov 04 '12 at 04:24
7

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.
Joakim
  • 3,224
  • 3
  • 29
  • 53
7

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

enter image description here

Gagandeep Gambhir
  • 4,225
  • 1
  • 29
  • 34
6

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

raheel
  • 1,407
  • 1
  • 15
  • 24
  • 4
    "Buy now for $36" ... what a steal ) lol... until I saw this on the Maestro site, I thought it might be a good solution – ycomp Feb 28 '12 at 18:01
  • @ycomp, here's a [free solution](http://stackoverflow.com/questions/10266170/xcode-4-duplicate-line). – Blaz Jan 27 '13 at 11:44
5

I know that this thread is quite old but I found another solution using BetterTouchTool, you can execute the Ctrl+A, Ctrl+K, Ctrl+K sequence in one shortcut by configuring like this in BTT:

enter image description here

(Use the Attach Additional Action Button to append the second and third shortcut)

PS: Coming from Eclipse where Cmd+D is delete line ^^

Community
  • 1
  • 1
Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
5

We are on Xcode 9.0 and the keybindings are still not working. Sigh.

I found this gist very helpful so reposting it here for future reference:

  1. Open /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
  2. Add the following XML snippet to this file just above the bottom </dict>
  3. Go to Xcode preferences -> Key Bindings -> Text tab -> Scroll till you see Duplication
  4. Click on Duplicate Current Line, add a shortcut for it, eg. Cmnd+D (resolve any duplicate bindings)
  5. Open Xcode

<key>Duplication</key> <dict> <key>Duplicate Current Line</key> <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string> <key>Duplicate Lines</key> <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string> <key>Delete Line</key> <string>selectLine:, deleteBackward:</string> </dict>

Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
4

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
  • 1,465
  • 15
  • 27
3

There's a solution for XCode4 line duplication over here

I've also added line deletion to the plist:

<key>Remove Line</key> <string>selectLine:, deleteBackward:</string>

Community
  • 1
  • 1
MartinMcB
  • 51
  • 3
3

No need to modify Xcode. You can just use the Xcode extension Linex

enter image description here

Alaeddine
  • 6,104
  • 3
  • 28
  • 45
3

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
  • 179,021
  • 58
  • 319
  • 408
  • 3
    Mouse click and even worser, triple? That's surely not the solution people wanting to use keyboard shortcuts is looking for. – User Sep 01 '12 at 15:08
2

This is possible using BetterTouchTool. CMD+D will duplicate a line. Create a new shortcut like this:

enter image description here

Use the Attach Additional Action Button to append the second and third shortcut

Adam Studenic
  • 2,115
  • 2
  • 25
  • 22
2

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.

Community
  • 1
  • 1
palaniraja
  • 10,432
  • 5
  • 43
  • 76
2

As said above, close XCode and insert following to ~/Library/Application Support/Xcode/Key Bindings/ <user>.pbxkeys inside <dict> in <key>text</key> section:

    <key>^D</key>                                                                                                      
    <array>                                                                                                            
        <string>moveToLeftEndOfLine:</string>                                                                               
        <string>deleteToEndOfLine:</string>                                                                                      
    </array>                                                                                                           
    <key>^K</key>                                                                                                      
    <array>                                                                                                            
       <string>selectLine:</string>                                                                               
       <string>copy:</string>                                                                                     
       <string>moveToEndOfLine:</string>                                                                          
       <string>insertNewline:</string>                                                                            
       <string>paste:</string>                                                                                    
       <string>deleteBackward:</string>                                                                           
    </array>

Start XCode and enjoy CTRL-SHIFT-D and CTRL-SHIFT-K.
If you want CTRL-D and CTRL-K, use ^d and ^k in key definitions instead. Ensure there are no duplicate key bindings in the file.

Works fine for me with XCode 3.2

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
askh
  • 101
  • 1
  • 3
1

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.

MonsieurDart
  • 6,005
  • 2
  • 43
  • 45
  • Hiya. I don't quite follow this, the pbxkey document is XML whereas the ~/Library/KeyBindings/PBKeyBinding.dict file described above is a different format entirely (looks a bit like JSON). There isn't a Root/text entry in the pbxkeys document. Is it because I'm using a later version? – Ben Clayton Feb 09 '11 at 12:34
1

Three step to delete current line (see too long, but do very fast :D)

Cmd + : move to end of line
Cmd + Delete : delete to begin of line
Delete : delete empty line

huynguyen
  • 7,616
  • 5
  • 35
  • 48
0

For Xcode 6.1 got to Xcode > Preferences > KeyBindings > Text And there is "Delete to End of Line" and assign a key you want.

SpyZip
  • 5,511
  • 4
  • 32
  • 53
0

XCodePlus delete line plugin, is a XCode that does this for you. It came pre installed with Alcatraz package manager...

https://github.com/payliu/XcodePlus

Joakim
  • 3,224
  • 3
  • 29
  • 53
0

Still there is no built-in way to duplicate lines, as of Xcode 9. And custom key bindings seem to have problems.

Update: Xcode 10.0 beta 6 (10L232m) is the same.

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
0

Go to this address :

/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet

and then copy IDETextKeyBindingSet.plist file in some where and then open copied file and insert

selectLine:, copy:, paste:, paste: like this picture enter image description here and save it and replace copied file in this address /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet

then restart xcode , open xcode prefences , go to keybinding tab , search for duplicate line(customized) and define short key for it like image below

[1]: https://i.stack.imgur.com/UTeZu.png

Khatereh
  • 1
  • 1
  • From Review: Hi, this post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment. – sɐunıɔןɐqɐp Oct 21 '19 at 10:52