Move to beginning of text in Xcode - xcode

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

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.

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.

Related

Is there any way of using normal keyboard text selection with a terminal on MacOS?

For the last ~10 years I've been used to manipulating text with the alt/⇧/⌘ keys. For instance, alt+← to jump back a word instead, alt+⇧+← to select that word, ⌘+→ to jump to the end of line, etc.
I understand that terminals have different conventions and historically came first, but I'm used to these shortcuts, they work great and work everywhere else. Is there any way of getting the same controls for selecting and manipulating text in the terminal?
I have seen the following questions:
How to move the cursor word by word in the OS X Terminal
How do I clear/delete the current line in terminal?
But they only address moving the cursor, and not selection.
Depending on the macOS version - your default shell may be zsh.
Shortcuts options are ctrl + a/e to move to beginning/end of line and esc + W/B to move one word backward/forward
Other popular alternatives are -
a. bash - with the shortcuts listed in the answer by #Hossein Amiri
b. iterm2 - From - https://coderwall.com/p/a8uxma/zsh-iterm2-osx-shortcuts
Put this in your .zshrc
bindkey "[D" backward-word
bindkey "[C" forward-word
bindkey "^[a" beginning-of-line
bindkey "^[e" end-of-line
And set iterm preferences.
Moving the cursor:
Ctrl+A Go to the beginning of the line (Home)
Ctrl+E Go to the End of the line (End)
Hold the Option key option and click on the current line = Jump Backwards
Ctrl+P Previous command (Up arrow)
Ctrl+N Next command (Down arrow)
Hold the Option key option and click on a previous line = Jump upwards
Ctrl+F Forward one character
Ctrl+B Backward one character
Alt+B Back (left) one word or use Option+→
Alt+F Forward (right) one word or use Option+←
Ctrl+xx Toggle between the start of line and current cursor position
More information : https://ss64.com/osx/syntax-bashkeyboard.html
You can't, I've beed looking for long time. This is why people use iTerm they have more flexibility with keyboard only.
https://iterm2.com/features.html
Copy Mode
Use the keyboard to make and modify selections.

XCode 5 -- Jump To Previous Cursor Location

I would like to jump to the previous cursor position within a file in XCode 5 (with either a keyboard hotkey or a drop-down menu path).
This SO post worked for XCode 3: Does XCode have a cursor navigation stack like Visual Studio?
But for XCode 5, I could not find this functionality here: http://cloud.github.com/downloads/Machx/Xcode-Keyboard-Shortcuts/Xcode_Shortcuts.pdf, nor in the Apple Documentation here: https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/xcode_help-command_shortcuts/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010560-CH1-SW1
EDIT
Sorry I wasn't clear earlier. I do not want to switch between files. As the answers below state, that is what ctrl+cmd+left/right does. I want to switch between cursor positions within a file. For example:
I am working in MyViewController.m.
I write a function -(void)doStuff at line 178.
In -(void)doStuff, I realize that I need to update an #property to be readwrite.
I go to the top of MyViewController.m to line 10. I add the line #property (nonatomic, strong, readwrite) NSMutableArray *myArray;
Now I want to return to finish writing my -(void)doStuff method. What is the best way to get there? This is when I'd like to say "Go to the last text cursor position."
As Owen commented on your post, I believe the behaviour you want is to Go Forward/Backward, which is a behaviour represented by those two arrows at the top left of the text entry field:
In the pdf you linked these actions actually have shortcuts to the keys cmd+control+arrow, where the arrow key you press represents forward/backward motion through your XCode behaviour (left being back, right being forward).
A useful trick I found is to bind opening the Document Items menu (by default Ctrl + 6) to something easier to type for me. Then when I'm about to, say, jump to some method definition (that may or may not be in the same file I'm already in), I first open the Document Items menu then just hit Enter. That marks my current location in the navigation stack. It also marks the line the cursor is at in the current method, too.
It takes a little training to remember to mark your place, but being able to open that menu and hit Enter quickly from the keyboard makes it less painful to force Xcode to mark your navigation within a single file.
cmd+ctrl+arrow can work in this situation. But, it depends on how you change cursor locations.
If you use a file navigation control to change cursor location, cmd+ctrl+arrow will work. ctrl+6 switches to a method within a given file. If you start on line 178 then ctrl+6 to switch to another method on line 10, cmd+ctrl+arrow will return your cursor to line 178.
If you manually change cursor location, cmd+ctrl+arrow will not work. If you start on line 178 then scroll to line 10, cmd+ctrl+arrow will not return your cursor to line 178.
I was searching for this for so long time and what works for me is:
⌃ + 2 (Ctrl + 2 - Shows previous history) then click ↓ - this will jump back to previous definition
⌃ + 3 (Ctrl + 3 - Shows next history) then click ↓ - this will jump to next definition
This will only work if you navigate through your class using ⌃ + 6 to jump between definitions.
Unfortunately Xcode does not comes up with this feature . but If you are going to different line for copy content to paste to previous location then tricky using (Undo and Redo) Command + Z and then Shift +command +Z shortcut key one short simulate same effect.
Ctrl-command-leftKey does exactly that. (It's called "Go Back" in the documentation).

Change how home and end keys work in XCode

When I press the End key I expect it to bring me to the end of the line. Likewise when I press the Home key I expect it to bring me to the beginning of the line. In Xcode though, these navigate to the top and bottom of the page. How can this be changed?
Xcode > Preferences > Key Bindings > Text Key Bindings
Then find these two:
Move to beginning of line
Move to end of line
Assign Home and End to these respectively.
If you're doing this, don't forget to update Move to Beginning/End of Line Extending Selection, too and assign ⇧ ShiftHome and ⇧ ShiftEnd, respectively. This will continue the desired behaviour when you have ⇧ Shift pressed.
You will also have to remove the conflicts that appear.
FYI: ⌘ Cmd+→ and ⌘ Cmd+← are the way things are normally done on a Mac. If you are coming from Windows I would try to adjust to this. If you want to jump between words, you can use Alt+→, Alt+←.
Go to Xcode -> Preferences... -> Key Bindings, tab Text Key Bindings, and change them to whatever you want.
If you're after 'smart home', where pressing the Home key takes you to the start of the text on that line, and a second-press takes you to the start of the line... then you'll be wanting this:
https://github.com/insanehunter/XCode4_beginning_of_line

Xcode History (back/forward) Keyboard Shortcut?

Back Arrow Broken In Xcode?
Is there keyboard command to go back/forward one file at a time.
Xcode 3.2 changed the behavior of Cmd-Opt <-- and -->. As noted in another question, they do not operate on a file level like in previous versions of Xcode, but on an "edit point", making it cumbersome to flip through a list of files (the quick way to do it, since Xcode does not support Tabs, as in Eclipse).
Rob Keniger found a partial solution that he noted in another post:
"Hold down the option key while you click the forward/backward arrows ..."
But this requires fiddling with the mouse. Is there any way to do it with the keyboard only?
EDIT:
Later versions of Xcode (v7.2+) now have a Navigate menu (and it changes the keyboard shortcuts yet again):
In XCode 4.4 the following sequence works:
Cmd + Ctrl + ←
and it operates on a file level.
On Xcode 8 I use 2 fingers left / right
Beginning in XCode 3.2.3, the following key sequence works:
Cmd + Option + Shift + ←
Without the Shift key, it just goes back to the previous cursor position. Adding Shift jumps to the previous file (next file, in combination with →).
Navigation shortcuts for Xcode

How to move the cursor word by word in the OS X Terminal

I know the combination Ctrl+A to jump to the beginning of the current command, and Ctrl+E to jump to the end.
But is there any way to jump word by word, like Alt+←/→ in Cocoa applications does?
Out of the box you can use the quite bizarre Esc+F to move to the beginning of the next word and Esc+B to move to the beginning of the current word.
On macOS (all versions) the following keyboard shortcuts work by default.
ALT+F to jump Forward by a word.
ALT+B to jump Backward by a word.
Note that you have to make set the Option key to act like the Meta key. You can do this in Terminal by accessing preferences (CMD+,) and selecting Profiles -> Keyboard. In iTerm2 Pselect rofiles -> Keys -> General and select "Option key as Esc+."
Additionally some Emacs-style key bindings for simple text navigation seem to work on bash shells. You can use:
CTRL+F to move forward by a char
CTRL+B to move backward by a char
CTRL+A to jump to start of the line
CTRL+E to jump to end of the line
CTRL+K to kill the line starting from the cursor position
ALT+D to delete a word starting from the current cursor position
CTRL+W to remove the word backwards from cursor position
CTRL+Y to paste text from the kill buffer
CTRL+R to reverse search for commands you typed in the past from your history.
CTRL+S to forward search (works in ZSH for me but not bash)
Here's how you can do it
By default, the Terminal has these shortcuts to move (left and right) word-by-word:
esc+B (left)
esc+F (right)
You can configure alt+← and → to generate those sequences for you:
Open Terminal preferences (cmd+,);
At Settings tab, select Keyboard and double-click ⌥ ← if it's there, or add it if it's not.
Set the modifier as desired, and type the shortcut key in the box: esc+B, generating the text \033b (you can't type this text manually).
Repeat for word-right (esc+F becomes \033f)
Alternatively, you can refer to this blog post over at textmate:
http://blog.macromates.com/2006/word-movement-in-terminal/
Switch to iTerm2. It's free and much nicer than plain old terminal. Also it has a lot more options for customization, like keyboard shortcuts.
Also I love that you can use cmd and 1-9 to switch between tabs. Try it and you will never go back to regular terminal :)
How to set up custom keyboard preferences in iterm2
Install iTerm2
Launch and then go to preference pane.
Choose the keyboard profiles tab
You will either need to copy the profile to something new and then delete the arrow key shortcuts such as ^+ Right/Left or if you don't care about a backup just delete them from the default profile.
Next make sure your modified profile is selected (starred)
Now choose the keyboard tab (very top row)
Click on the plus button to add a new keyboard shortcut
In the first box type CMD+Left arrow
In the second box choose "send escape code"
In the third box type the letter B
Repeat with desired key combinations. escape+B moves one word to the left, escape+f moves one word to the right.
you may also wish to set up cmd+d to delete the word in front of the cursor with escape+d
I often hit the wrong button (cmd / control / alt) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.
Actually there is a much better approach. Hold option ( alt on some keyboards) and press the arrow keys left or right to move by word. Simple as that.
option←
option→
Also ctrle will take you to the end of the line and ctrla will take you to the start.
I have Alt+←/→ working: open Preferences » Settings » Keyboard, set the entry for option cursor left to send string to shell: \033b, and set option cursor right to send string to shell: \033f. You can also use this for other Control key combinations.
Use Natural Text Editing preset!
Essentially it binds, among other key sequences, Option + LeftArrow to ^[b sequence and Option + RightArrow to ^[f
This works in fish and bash, as well as in psql terminal.
Actually it depends on what shell you use, however most shells have similar bindings. The bindings you are referring to (e.g. Ctrl+A and Ctrl+E) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.
Here's a look of default bindings for Bash:
Most Important Bash Keyboard Shortcuts
Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named ".bashrc" that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:
bind -f ~/.bash_key_bindings
~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.
Let me show you some excerpts of my .bash_key_bindings file:
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
set show-all-if-ambiguous on
set bell-style none
set print-completions-horizontally off
These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).
"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert
These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).
"\C-dW": kill-word
"\C-dL": kill-line
"\C-dw": backward-kill-word
"\C-dl": backward-kill-line
"\C-da": kill-line
This is quite interesting. If I hit Ctrl+D alone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).
I placed jumping one word forward on Ctrl+F and one word backward on Ctrl+B
"\C-f": forward-word
"\C-b": backward-word
As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.
So if you are not happy with the default bindings, feel free to customize them as you like. Here's a link to the bash manual for more information.
Hold down the Option key and click where you'd like the cursor to move
If you happen to be a Vim user, you could try bash's vim mode. Run this or put it in your ~/.bashrc file:
set -o vi
By default you're in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is w or b, and the usual movement keys also work.
If you check Use option as meta key in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and ⌥F (Alt+F) and ⌥B (Alt+B) respectively.
I'd recommend reading From Bash to Z-Shell. If you want to increase your bash/zsh prowess!
As of Mac OS X Lion 10.7, Terminal maps Option-Left/Right Arrow to Esc-b/f by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.
In Bash, these are bound to Esc-B and Esc-F.
Bash has many, many more keyboard shortcuts; have a look at the output of bind -p to see what they are.
Under iterm2's Preferences > Profile > Keys, you click the + below Key Mappings and record a new shortcut. For Action, select Send Escape Sequence and type b or f for backwards and forwards respectively.
When I tried to record one for (Ctrl+←), I noticed in the Keyboard Shortcut field that the arrow never showed up. Turns out I had to disable the default mac's System Preferences > Keyboard > Shortcuts > Mission Control shorcuts first to get things to work, as they'll override iterm2's default shortcuts. Should be true for the standard terminal app, too.
For some reason, my terminal's option+arrow weren't working. To fix this on macOS 10.15.6, I opened the terminal app's preferences, and had to set the bindings.
Option-left = \033b
Option-right = \033e
For some reason, the option-right I had was set up to be \033f. Now that it's fixed, I can freely skip around words in the termianl again.
Here's the CLI way to do so, verified it works on bash.
Add the following to your ~/.inputrc:
# macOS Option + Left/Right arrow keys to move the cursor wordwise
"\e\e[C": forward-word
"\e\e[D": backward-word
The advantage of this method is that it is terminal application agnostic - doesn't matter whether you use Terminal.app, iTerm2, or any other application.
Inspiration got from this other answer.
New answer for iTerm2 Build 3.3.4 users:
Step 1: (macOS X) System Preferences > Keyboard > Shortcuts tab > Select Mission Control (left panel) > Uncheck shortcuts that labeled as "Move left a space" and "Move right a space"
Step 2: (iTerm2 Build 3.3.4) Preferences > Profiles > Select * Default (left panel) > Keys tab > Delete both "⌥->" and "⌥<-" entries > Set both "Left Option (⌥) Key:" and "Right Option (⌥) Key:" to Esc+
No messing around with shell profiles, no messing around with inferior masOS (default) Terminal, no awkwards Esc+F/B, rinse & repeat non-sense.
Done deal!!!
Enjoy this tip, my fellow PROGRAMMERS!
As answered previously, you can add set -o vi in your ~/.bashrc to use vi/vim key bindings, or else you can add following part in .bashrc to move with Ctrl and arrow keys:
# bindings to move 1 word left/right with ctrl+left/right in terminal, just some apple stuff!
bind '"\e[5C": forward-word'
bind '"\e[5D": backward-word'
# bindings to move 1 word left/right with ctrl+left/right in iTerm2, just some apple stuff!
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'
To start effect of these lines of code, either source ~/.bashrc or start a new terminal session.
Just check the "Use Option as meta key" option in Terminal > Preferences > Settings > [profile] > Keyboard, as mentioned here already by #cris-page.
Note however, that in macOS Catalina (10.15) and newer, zsh becomes the default shell for newly added users: its default configuration considers only whitespaces as word-boundaries, whereas the old bash makes meta-left/right jump to the nearest non-alphanumerical character (similar to B/W as opposed to b/w for those familiar with vim):
v----v- bash jumps here
$ vim some-folder/what.txt_<- jump left twice from here
^---^- zsh jumps here by default
(similar motions are true for meta-backspace as well)
There are more than one ways to make zsh command line editor navigation work similarly to bash's - here is one such method:
# Place in your profile init script, e.g. `~/.zshrc`
autoload -U select-word-style
select-word-style bash

Resources