58

In Xcode 4, I have been sucessfully able to add new custom keyboard shortcuts to move lines around or to duplicate a line:

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

<key>My Custom Keys</key>
<dict>
    <key>Move Line Up</key>
    <string>selectLine:, cut:, moveUp:, moveToBeginningOfLine:, paste:, moveUp:</string>
    <key>Move Line Down</key>
    <string>selectLine:, cut:, moveDown:, moveToBeginningOfLine:, paste:, moveUp:</string>
    <key>Duplicate Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, yank:, insertNewline:, moveToBeginningOfLine:, yank:</string>
    <key>Delete Line</key>
    <string>selectLine:, delete:</string>
</dict>

But the Move Line Up/Down uses copy/paste, which I would like to avoid. Anyone knows how to move lines with yank, just like the Duplicate Line I found there: Xcode duplicate line

Community
  • 1
  • 1
dbernard
  • 1,073
  • 1
  • 11
  • 21
  • possible duplicate of [Manipulating text in XCode, moving one line](http://stackoverflow.com/questions/1380214/manipulating-text-in-xcode-moving-one-line) - one of the answers there has the official xcode 4 shortcut – jrturton Mar 01 '13 at 13:58

2 Answers2

169

Copied from https://stackoverflow.com/a/9078952/852828 as that isn't the accepted answer to the linked duplicate.

Xcode 4 has a new set of command for moving the line where the cursor is or the selected text with command + option + [ or ]

⌥⌘[ or ⌥⌘]

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 18
    For reference: the name of the commands are `Structure ▸ Move Line Up` and `Structure ▸ Move Line Down` in Key Bindings. – gklka Jul 27 '15 at 11:53
  • 3
    @gklka Thanks, this is what I was looking for (default key bindings usually don't work on foreign keyboard layouts, I had to change this one) – Sébastien Jan 27 '16 at 11:11
  • Thanks @gklka ! Your answer is still valid 8 years later – dorianDevTech Mar 16 '23 at 21:12
16

How to move lines in Xcode via the menu OR hotkeys:

enter image description here

If you are using a Windows keyboard, the commands would translate to:

  • ⌥⌘[ ..... Alt + Win + [
  • ⌥⌘] ..... Alt + Win + ]
Josh Withee
  • 9,922
  • 3
  • 44
  • 62