Moving and Copying Lines in Xcode

3 minute read Published:

Being a heavy Eclipse user, I’ve grown accustomed to its keyboard shortcuts for moving and copying lines of text. Xcode doesn’t have a regular editing command for this, but it does offer similar functionality via user scripts. The Move Line Up and Move Line Down scripts are already there, just waiting for you to bind keys to them. Go to the script menu, which is to the left of the Help menu, and select the Edit User Scripts… menu item. Expand the disclosure triangle next to Text, and you can set keyboard shortcuts by double-clicking in the column to the right of the script names. Below is a screenshot of what the dialog will look like if you follow all of the instructions in this post.

edit-user-scripts.png

As you can see, I’ve also added Copy Line Up and Copy Line Down scripts. Xcode doesn’t come with scripts for these commands, but you can pretty easily modify the existing move line scripts to create them. I’ve included the scripts I came up with below. These are just the Apple provided scripts with the delete lines removed and the text selection offsets changed to highlight the copied text.

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 {startLine, endLine}
    else
      beep 1
    end if
  end tell
end using terms from
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)
      make new paragraph at beginning of paragraph (endLine + 1)
        with data theText
      set theSize to (endLine - startLine)
      set selected paragraph range
        to {endLine + 1, endLine + theSize + 1}
    else
      beep 1
    end if
  end tell
end using terms from

You’ll need to save these scripts somewhere in your home directory. I put them in ~/Library/Application Support/Developer/Shared/Xcode, as that’s were other Xcode customization files go. You can then add the scripts by clicking on the plus in the bottom left corner of the Edit User Scripts window and selecting the Add Script File… menu item. You should also change the four drop down options on the right to be the same as the original move line scripts. Once you’ve added the scripts, you can bind keys to them like any of the other scripts.

If you’re going to make further modifications, you need to be sure you understand the difference between the “shell scripts” and “script files”. Shell scripts will have their contents displayed in the text view on the right side of the Edit User Scripts window. You can safely use the Duplicate Script option on them, because you have your own copy of the content in the XCUserScripts.plist file located in the previously mentioned directory. Script files on the other hand are stored in the main Xcode directory, so you need to manually copy and add them before making any modifications, just like I did for this example.

Finally I’ve noticed that the user scripts don’t block further input while they execute. If you’re trying to move multiple lines of text around, you can actually hit keys faster than the scripts finish executing, which may result in the group of lines being de-selected and getting jumbled up. Because of this, the user scripts are best suited for single lines or short range copies.