Skip to content

Instantly share code, notes, and snippets.

@skarol
Created November 22, 2021 12:50
Show Gist options
  • Save skarol/6bec980705243e99995594862fa60855 to your computer and use it in GitHub Desktop.
Save skarol/6bec980705243e99995594862fa60855 to your computer and use it in GitHub Desktop.
Xcode duplicate current line up/down

Xcode duplicate line up/down

Bind keys to duplicate lines in Xcode

  1. Open with text editor
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist

If you are using multiple versions of Xcode you have to open this file for each of them. Remember to change /Applications/Xcode.app to path where your Xcode is located.

  1. Add thise lines:
<key>Duplication</key>
<dict>
    <key>Duplicate Current Line Up</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:, moveUp:, moveToEndOfLine:</string>
    <key>Duplicate Current Line Down</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
</dict>
  1. Open Xcode and Xcode preferences ⌘ Command + ,Key Bindings and search for duplication

  2. Click on Duplicate Current Line Up, add a shortcut for it, eg. (if you want same shortcut as visual studio code) ⌥ Option + ⇧ Shift + (remove any other bindings for this key)

  3. Click on Duplicate Current Line Down, add a shortcut for it, eg. (if you want same shortcut as visual studio code) ⌥ Option + ⇧ Shift + (remove any other bindings for this key)

@alexandercerutti
Copy link

alexandercerutti commented May 20, 2025

That's great, thank you!
Sadly, this doesn't cover the fact of duplicating whole blocks of code (@mesqueeb)

I started studying the code for such commands (https://developer.apple.com/documentation/appkit/nsstandardkeybindingresponding) and then asked AI, and I found out a bit how these works.

Apparently, deleteToEndOfLine: actually cuts the content into a "kill buffer", which yank: is able to read and paste from.

Btw, with some genuine help of Copilot, we came up with this solution, that includes both single lines and blocks.

    <key>Duplicate block Down</key>
    <string>selectLine:, copy:, moveRight:, paste:</string>
    <key>Duplicate block Up</key>
    <string>selectLine:, copy:, moveToBeginningOfLine:, paste:</string>

It seems to be working pretty good like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment