NOTE (2022-07-09): Xcode finally added this functionality in Xcode 14, please see release notes here:
New Features in Xcode 14 Beta 3
When editing code, the Edit > Duplicate menu item and its corresponding keyboard shortcut now duplicate the selected text — or the line that currently contains the insertion point, if no text is selected. (8614499) (FB5618491)
-
Close Xcode
-
Open below directory in Finder with Cmnd + Shift + G
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/
-
Open
IDETextKeyBindingSet.plist
with a text editor. -
Add this in:
<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>
-
Open Xcode and go to
Xcode preferences
->Key Bindings
->Text
tab -> Scroll till you seeDuplication
-
Click on
Duplicate Current Line
, add a shortcut for it, eg. Cmnd + D (remove any other bindings for this key)
Could move the file to a new location and replace with a symbolic link, set permission on the file for Xcode to read only. Or even the permissions alone would be enough to possibly prevent updates from overwriting the changes. But with a symbolic link it could still be replaced in an update and after you check for any breaking changes in the updated version you can just replace it with a fresh symbolic link.