在 Xcode 中移到文本的开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5241092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move to beginning of text in Xcode
提问by lkraider
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).
在这个例子中,按下一个键会将光标移动到“view”单词的开头(char | 是光标)。
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 <-
).
非常烦人的是,当前我必须按 3 个命令才能在代码块 ( cmd <-
, opt ->
, opt <-
)中到达文本的开头。
回答by iHunter
There's no standard way to accomplish it in XCode, so I've written an XCode plugin implementing this feature:
在 XCode 中没有标准的方法来完成它,所以我写了一个 XCode 插件来实现这个功能:
https://github.com/insanehunter/XCode4_beginning_of_line
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.
在幕后,它覆盖了 XCode 的源代码编辑器键盘操作调度方法,并以增强的方式实现行跳转的开始/结束。
Hope it helped.
希望它有所帮助。
回答by Nick K9
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."
从 7.3 开始(不确定何时添加),Xcode 似乎默认启用了此功能。“键绑定”首选项窗格中的 Cmd-left 现在绑定到“移至文本开头”。
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.
如果您已经复制了您的键绑定集,您可能还没有看到新的行为。如果没有,只需在“键绑定”首选项中搜索“开始”并根据自己的喜好进行调整。
回答by Matt Ball
Use option+ ←to jump across entire words.
使用option+←跳过整个单词。
(That means, hold the optionwhile pressing the left arrow key)
(也就是说,按住option左箭头键的同时按住)
回答by internet-nico
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.
我正在使用 Xcode 7.2 并且能够制作我自己的宏来自动执行您上面提到的那 3 个命令 ( cmd <-
, opt ->
, opt <-
)。您可以编辑IDETextKeyBindingSet.plist
定义所有键绑定的文件以添加您自己的。
Mine was located at /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist
我的位于 /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.
重新启动 xcode 并在您的首选项 > 键绑定选项卡中,搜索您的自定义宏并将其设置为cmd <-
. 现在它将运行这 3 个命令并将光标有效地移动到行中文本的开头。
回答by junhan
Actually, there is a faster and simpler way to achieve this as Mac OS supports some Emacs (or Unix) keybindings quite well.
实际上,有一种更快更简单的方法来实现这一点,因为 Mac OS 很好地支持一些 Emacs(或 Unix)键绑定。
You can use Ctrl-a
to jump to the beginning of a line.Which means, hold 'Ctrl' key and press 'a' on keyboard.
您可以使用Ctrl-a
跳转到一行的开头。这意味着,按住“Ctrl”键并按键盘上的“a”。
For your more information, it is also handy to useCtrl-e
: jump to the end of a lineCtrl-n
: move to next lineCtrl-p
: move to previous line
有关更多信息,使用Ctrl-e
以下命令也很方便:跳转到行尾Ctrl-n
:移至下一行Ctrl-p
:移至上一行
What's more, these fast keys can be used in terminal and text fields such as this StackOverflow answer editor on Mac OS, too.
更重要的是,这些快捷键也可用于终端和文本字段,例如 Mac OS 上的 StackOverflow 答案编辑器。
Hope that helps.
希望有帮助。
回答by ettore
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 ←:
我没有确切的解决方案,只是对 Xcode 4 的一个小改进。您可以为“向左移动表达式”(Xcode 首选项 > 键绑定)设置键盘映射,这比 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".
如果| 是光标位置,按“向左移动表达式”键会将其带到其左侧表达式的开头,因此在这种情况下位于“[NSString”之前。Opt ← 只会在“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.)
(旁注:我确实看到了一个名为“移至行尾”的操作命令和另一个名为“移至行首”的操作命令。根据我的经验,这些在 Xcode 4.0.2 中的行为完全相同:这看起来像我喜欢一个错误,除非我遗漏了什么。)
回答by davittarius
It seems that the simplest way, as already mentioned is
似乎最简单的方法,正如已经提到的
1) Go to the beginning of the line (Cmd + left arrow key)
1) 转到行首(Cmd + 左箭头键)
2) Jump to right word (Alt + right arrow key)
2) 跳转到正确的单词(Alt + 右箭头键)
3) Jump to left word (Alt + left arrow key)
3) 跳转到左边的单词(Alt + 左箭头键)
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.
不幸的是(Alt + 左箭头键)和(Alt + 右箭头键)忽略了注释、括号等,因此上述方法并不总是有效。如果这不是问题,那么对于 BetterTouchTool 用户来说,还有一种解决方案。可以创建一系列动作(命令),将一个接一个地调用,并将其分配给主页按钮。为此,请打开首选项,转到键盘选项卡,添加新快捷方式并将主页按钮分配给(Cmd + 左箭头键)。然后单击分配附加操作两次,第一次用于(Alt + 向右箭头键),第二次用于(Alt + 向左箭头键)键命令。
Hopefully this helps.
希望这会有所帮助。
回答by Mahmoud
Tested on XCode 10:
在 XCode 10 上测试:
Step1:Open the file located in the next path by a text editor
Step1:用文本编辑器打开位于下一个路径的文件
/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.
上一个键“ Move To First Char In Line”将满足您的要求,但我也提到了其他键。
Now save the file.
现在保存文件。
Step2:Close XCode and reopen it.
步骤 2:关闭 XCode 并重新打开它。
Step3:Navigate to keybinding in xcode preferences then assign your shortcut.
步骤 3:导航到 xcode 首选项中的键绑定,然后分配您的快捷方式。
XCode -> Preferences -> Key BindingsThen search for 'Move To First Char In Line' and assign your shortcut.
XCode -> Preferences -> Key Bindings然后搜索“Move To First Char In Line”并分配您的快捷方式。
回答by guoc
In this Xcode extension, Linex, the feature Line Beginningcan move to the first non-whitespace character in a line, and also toggle between that and the real beginning.
在这个 Xcode 扩展Linex 中,功能Line Beginning可以移动到一行中的第一个非空白字符,也可以在该字符和真正的开头之间切换。
Works for Xcode 9, perhaps Xcode 8.
适用于 Xcode 9,也许适用于 Xcode 8。