45

I know I can bind keys to "Move to beginning of line", but this ignores the indentation.

What I'm looking for is to move to the beginning of text on a line, so that:

CGRect example = CGRectMake(view.frame.origin.x,
                            view.frame.origin.y,|

pressing a key will move the cursor to the beginning of the "view" word in this example (char | is cursor).

It is extremely annoying that currently I have to press 3 commands to get to the beginning of the text when inside a code block (cmd <-, opt ->, opt <-).

1
  • I'm looking for the same thing! All the Java-based IDE's do this when you are in the middle or at the end of the line: 1st CMD+LEFT_ARROW takes you to beginning of first word on line, 2nd one takes you to actual beginning of line, 3rd time back to first word, and so on. It makes it so fast and easy to hop around text, I wish Xcode 4 would do the same thing!!
    – jpswain
    Commented Oct 11, 2011 at 20:38

9 Answers 9

44

There's no standard way to accomplish it in XCode, so I've written an XCode plugin implementing this feature:

https://github.com/insanehunter/XCode4_beginning_of_line

Under the hood it overrides XCode's source editor keyboard action dispatch method and implements beginning/ending of line jumps in enhanced way.

Hope it helped.

5
  • @MikeClark Oh, thank you! Your words are the best motivation for me ever!
    – iHunter
    Commented May 18, 2012 at 12:38
  • @Tim It should work. Are you using the latest version from master branch?
    – iHunter
    Commented Mar 26, 2014 at 18:47
  • @iHunter Ah, my apologies. I am using Alcatraz package manager. Just had to uninstall and reinstall to rebuild it on the latest master.
    – Tim
    Commented Mar 26, 2014 at 22:56
  • 2
    @physicalattraction Yes, works perfectly. In case of troubles try downloading latest version from github and building it again - there are Xcode compatibility UDIDs added with each major Xcode release.
    – iHunter
    Commented Apr 22, 2015 at 14:36
  • My understanding is that plugins are not supported with Xcode 8 - please correct me if I'm wrong.
    – RenniePet
    Commented Feb 6, 2017 at 10:41
12

As of 7.3 (not sure when it was added), Xcode seems to have turned this on by default. Cmd-left in the Key Bindings preference pane is now bound to "Move to Beginning of Text."

Personally, this drives me bonkers, so I've changed it back to "Move to Beginning of Line." But for people who want this behavior, it's good to know that there's now a built-in way to get it.

If you've copied your Key Bindings Set, you may not be seeing the new behavior yet. If not, just search for "beginning" in the Key Bindings preferences and adjust them to your liking.

1
  • 1
    Just tested this in Xcode 8.2 and it works great. Make sure to delete the key bindings for Cmd+Left and Home and and the Shift+Cmd+Left and Shift+Home bindings for Move to Left End of Line Extending Selection and reassign those to Move to Beginning of Text Extending Selection. Really weird defaults, Apple. Commented May 29, 2018 at 17:18
10

Use option + to jump across entire words.

(That means, hold the option while pressing the left arrow key)

3
  • Yes, I am aware, but this will not move to the beginning of the line as I asked, it will move word by word.
    – lkraider
    Commented Mar 9, 2011 at 3:59
  • @lkraider: I don't see a way to do this in XCode (or OS X in general, since that's where most of the cursor movement shortcuts are inherited from). XCode 3.2 keyboard shortcuts.
    – Matt Ball
    Commented Mar 9, 2011 at 4:07
  • Yes, I was afraid of that. On that note, there doesn't seem to be any difference between the beginning of line and beginning of paragraph, both bindings do the same thing.
    – lkraider
    Commented Mar 9, 2011 at 4:25
7

Actually, there is a faster and simpler way to achieve this as Mac OS supports some Emacs (or Unix) keybindings quite well.

You can use Ctrl-a to jump to the beginning of a line. Which means, hold 'Ctrl' key and press 'a' on keyboard.

For your more information, it is also handy to use
Ctrl-e: jump to the end of a line
Ctrl-n: move to next line
Ctrl-p: move to previous line

What's more, these fast keys can be used in terminal and text fields such as this StackOverflow answer editor on Mac OS, too.

Hope that helps.

3
  • These are really useful, but don't seem to work for me in Xcode 7.2 ? Either way, they work as Home/End usually in other apps, which means they would ignore indentation too.
    – lkraider
    Commented Dec 10, 2015 at 20:18
  • As far as I know and all the Mac I've used, these keybindings work well. Besides, I don't quite understand "ignore indentation too". Do you mean it doesn't work when there are indentations?
    – junhan
    Commented Dec 10, 2015 at 23:45
  • "ignore indentation too" -> the same as I mean in the original question, where it should move to the beginning of text, not the beginning of the line.
    – lkraider
    Commented Dec 11, 2015 at 13:30
6

I'm using Xcode 7.2 and was able to make my own macro to automate those 3 commands you mentioned above (cmd <-, opt ->, opt <-). You can edit the IDETextKeyBindingSet.plist file which defines all the key-bindings to add your own.

Mine was located at /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

Add the following to create your own macro

<key>Custom Keys</key>
<dict>
    <key>Move to non-whitespace beginning of line</key>
    <string>moveToBeginningOfLine:, moveSubWordForward:, moveSubWordBackward:</string>
</dict>

Relaunch xcode and in your Preferences > Key Bindings tab, search for your custom macro and set it to cmd <-. Now it will run those 3 commands and effectively moves your cursor to the beginning of the text in the line.

3
  • That's great to know! Added to mine too. Just have to remember to edit that file when updating Xcode.
    – lkraider
    Commented Jan 14, 2016 at 16:36
  • Thanks, this works for everything except for when the line starts with a symbol (like [). The cursor is positioned right after the symbol, like: [|NSString ....]
    – Radu Cugut
    Commented Feb 9, 2016 at 12:40
  • I get an authentication error when I try to update this file, and I'm too new to Mac / Xcode development to know how to edit the file with admin authentication. Could you perhaps give me a suggestion?
    – RenniePet
    Commented Feb 6, 2017 at 10:51
2

I don't have an exact solution, just one slight improvement for Xcode 4. You could set up a keyboard mapping for "Move Expression Left" (Xcode prefs > Key Bindings), which is a little more than Opt ←:

s = [NSString string] |

if | is the cursor position, pressing a "Move Expression Left" keystroke will bring it to the beginning of the expression on the left of it, so in that case before "[NSString". Opt ← will only move before "string".

(Side note: I do see an action command called "Move to Left end of line" and another one called "Move to beginning of line." To my experience, these do exactly the same behavior in Xcode 4.0.2: this looks to me like a bug, unless I'm missing something.)

1
  • 1
    Interesting, could be useful, not sure. I actually find it curious they go to the trouble of implementing these complex expression parsing cursor movements, but manage to keep the simple stuff out.
    – lkraider
    Commented May 30, 2011 at 23:18
2

It seems that the simplest way, as already mentioned is

1) Go to the beginning of the line (Cmd + left arrow key)

2) Jump to right word (Alt + right arrow key)

3) Jump to left word (Alt + left arrow key)

Unfortunately (Alt + left arrow key) and (Alt + right arrow key) ignores comments, brackets, etc and therefore the method above will not always work. If this is not a problem, then there is also one solution for the BetterTouchTool users. One can create a sequence of actions (commands) that will be called one after another and assign it to Home button. To do so, open the preferences, go to Keyboard tab, Add new shortcut and assign Home button to the (Cmd + left arrow key). Then click Assign additional actions twice, first one for the (Alt + right arrow key) and the second one for the (Alt + left arrow) key commands.

Hopefully this helps.

1

Tested on XCode 10:

Step1: Open the file located in the next path by a text editor

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

Add the next elements to the end of the file:

<key>Sublime Commands</key>
<dict>
    <key>Cut Current Line</key>
    <string>selectLine:, cut:</string>
    <key>Copy Current Line</key>
    <string>selectLine:, copy:</string>
    <key>Duplicate Current Line</key>
    <string>selectLine:, copy:, moveToBeginningOfLine:, paste:, moveToEndOfLine:</string>
    <key>Delete Current Line</key>
    <string>selectLine:, deleteToEndOfLine:, moveToEndOfLine:</string>
    <key>Move To First Char In Line</key>
    <string>moveToBeginningOfLine:, moveSubWordForward:, moveSubWordBackward:</string>
</dict>

The previous last key 'Move To First Char In Line' will do your request, but I mentioned also other keys.

Now save the file.

Step2: Close XCode and reopen it.

Step3: Navigate to keybinding in xcode preferences then assign your shortcut.

XCode -> Preferences -> Key Bindings Then search for 'Move To First Char In Line' and assign your shortcut.

enter image description here

0
0

In this Xcode extension, Linex, the feature Line Beginning can move to the first non-whitespace character in a line, and also toggle between that and the real beginning.

Works for Xcode 9, perhaps Xcode 8.

Not the answer you're looking for? Browse other questions tagged or ask your own question.