开发者

Move to beginning of text in Xcode

I know I can bind keys to "Move to beginning of line", but this ig开发者_StackOverflow社区nores 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 <-).


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.


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.


Use option + to jump across entire words.

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


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.


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.


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.)


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.


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.

Move to beginning of text in Xcode


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜