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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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.