XCode 5 -- Jump To Previous Cursor Location - xcode

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

Related

How edit multiple lines on Xcode (shortcut)

I'm sorry, I don't find an already asked question for this problem...
in Xcode, what is the shortcut to edit multiples lines like the "CMD+click" of sublime text?
It is possible with Xcode 10+, see the wwdc2018 presentation of the functionality.
Ways to create multiple cursors:
⇧ Shift + ⌃ Control + Left Mouse Click: Will create a new cursor on every click
⇧ Shift + ⌃ Control + ↑: Will create a new cursor above
⇧ Shift + ⌃ Control + ↓: Will create a new cursor bellow
⌥ Option + drag: Will create a new cursor on every line you drag
preview:
For more information, see this answer
I'm not sure how helpful this is to you, but if you want to change a variable name, you can click it, hover over it until the triangle dropdown button appears, click it and select Edit All in Scope.
This allows you to change all instances of that variable name.
Multiline editing is supported in Xcode 10, Ctrl + Shift + click the lines you want to edit.
Select the line you want to edit, then press option + command for a moment
I had this same question a few weeks ago, if you use the Option Key you can use column select and edit multiple lines like in your picture.
Where the confusion comes in is it is used for multiple things. If you click the option key over a variable or text it shows a question mark and shows information about what you click on. However, if you hold the option key while before the line or not over text it shows up as a plus symbol and you can drag and select multiple lines and edit them at the same time.

Xcode keyboard shortcut: jump to test file?

Is there an Xcode keyboard shortcut to jump from MyObject.m to MyObjectTests.m, and vice versa? If so, what is it?
I haven't found a way to do this. Feature request material I think.
At the bottom of "Project Navigator" you can type your filename in the search area to dynamically search for files you would be interested in. They will be filtered as you type.
If you always place your ClassTests next to its Class, you can use Control + 5 followed by Up/Down and Return. Three keystrokes. Not ideal but works.
Command + Shift + O works fairly well. It is an open quickly command, so you can type in the name of the file and get a list of:
MyObject.m
MyObject.h
MyObjectTests.m
It remembers what you typed last too, so next time you Command + Shift + O (in the same XCode instance) you can now down arrow twice and return to get from source to test file. Works in XCode 6.3 / 6.4; I am not sure about other versions.
If you already know the name of the test file where the relevant tests are located, then of course the above answers work well. But if you don't know where the tests are (such as when you inherited an old code base or it's been more than 2 days since you wrote it), there are two ways to do it.
The keyboard shortcut ctrl-cmd-up will take you to the "generated interface" (a leftover from the objc days) for that file, but pressing it a second time will take you to the associated test file
Use the assistant editor, which can be turned on either through the little menu in the top right corner, or through the keyboard shortcut ctrl-opt-cmd-enter. This will usually show the test file for an associated file (or vice versa if you're looking a test file!)

Hidden Features of Xcode 4

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Now that Xcode 4 is officially released it's time for a follow up to my previous question: Hidden Features of Xcode
So what are they? What are those hidden little Xcode 4 hints & tips that you can't live without?
Please limit your tips to those that are new and specific to Xcode 4!
If you like your code to look as good as it runs, you've undoubtedly used #pragma mark - and #pragma mark <name> to provide a nice visual grouping in the Xcode class dropdown list. Xcode 4 now combines these into a single #pragma mark - <name>.
More on pragma mark.
shift-cmd-opt click on a symbol lets you tell Xcode exactly where in the UI you want the navigated-to file to open.
cmd-ret makes the version editor / assistant (the right hand pane) go away
shift-cmd-Y to hide the debugger
cmd-0 to hide the navigator (left pane)
cmd-[1..n] switch between navigators on left
cmd-opt-[1..n] switch between utilities (thing generally on right)
ctrl-[1..n] switch between various related files in the editor
Overall, Xcode 4 tries to keep things consistent w/the hot keys. Cmd is for mainline commands. Adding modifiers is for analogous commands focused on one particular sub-feature.
(These are just the four that come to mind as the my most pounded upon shortcuts that I'm using constantly! I'm sure it'll change over time as my workflow is refactored into the new hotness.)
Xcode 4 adds a new "Code Snippets" feature, accessible via View > Utilities > Code Snippet Library. To add a new code snippet, select some text in the editor view and drag it into the snippet library. You can either drag a snippet out and drop it in your code to use it, or, much more conveniently, assign a completion shortcut to it.
When you type in that series of letters, it will automatically show up in the code completion pop-up menu as you type your code. For example, among many others, I have a snippet set up so that typing "svk" will expand to setValue:<#value#> forKey:<#key#>. This can save a lot of typing and/or fishing around in the autocompletion menu for the methods and other code expressions you use the most. Xcode ships with a bunch of built-in snippets that come in very handy as well.
New shortcut I use most often: triple-finger swipe (up or down) to swap counterparts (between the interface and implementation files). No more hand acrobatics!
Thanks to https://twitter.com/xctotd/status/48148271759241216
shift-option-click on a file in the Groups and Files list Project Navigator, or hold shift-option while choosing a file from the Jump Bar.
You'll get a pop-over from which to pick where to open the file. New or existing tab, new or existing split (Assistant) editor, new window—every possible option is there.
Tabs in Xcode 4 are awesome. Each one maintains its own separate layout, so you can have multiple tabs with different things in them and have each one lain out appropriately.
I've been setting up my projects with three layouts—one for the project object, one for each nib (showing the owner's class's sources), and one for each class.
There is one bug here: As of 4.0, Xcode will reset the state (turning off any Assistants) of the active tab when you reopen the project. The workaround is to switch to a tab with no Assistants for Xcode to close before you close the project. I have filed this as Radar number 9178441.
ctrl-6 pops down within-file segment of jump bar (enables keyboard-controlled jump to methods)
cmd-{/} select previous / next tab
cmd-J navigate to any editor (but why oh why doesn't TAB then cycle between them?)
Editor ShortCuts
Cmd + Ctrl + up/down Jump between
.h & .m Files.
Cmd + Ctrl + Left/Right Jump between Previous
Edited Files/Next Edited files.
Layout Shortcuts
Cmd + 0 hide or show navigators
Cmd + opt + 0 hide or show inspectors
Cmd + Shift + Y hide or show debugger console.
Navigators Shortcuts
Cmd + 1 Project
Cmd + 2 Symbol
Cmd + 3 Search
Cmd + 4 Issue
Cmd + 5 Debug
Cmd + 6 BreakPoint
Cmd + 7 Log
Tab Switching & Jumping
Cmd + { Previous Tab
Cmd + } Next Tab
Cmd + j Select location to jump
Shift + Option + ClickFile Select location to open file
Cmd + Alt + Option + Click Select location to open Method
Inspectors
Cmd + Opt + 1 File Inspector
Cmd + Opt + 2 Quick Help
Cmd + Opt + 3 Identity Inspector
Cmd + Opt + 4 Attribute Inspector
Cmd + Opt + 5 Size inspector
Cmd + Opt + 6 Connection Inspector
Build Related
Cmd + B Build
Cmd + Shift + K Clean
Cmd + . top
Code Folding
Cmd + Alt + Left Minimize Current Method/Class/Interface
Cmd + Alt + Right Expand Current Method/Class/Interface
Cmd + Alt + Shift + Left Minimize All Method/Class/Interface
Cmd + Alt + Shift + Right Expand All Method/Class/Interface
Here's the first. Tabs! Xcode 4 has replaced Xcode 3's useful but rarely used "favorites bar" with persistent tabs. This means you can keep a working set of commonly used documents in your tab bar.
Not exactly a hidden feature as such - but the fact that the tabs are persistent is perhaps easily missed.
If you're having any sort of trouble with the indexing of your project (e.g. symbols not showing up in autocompletion), open the Organizer window, click the Projects tab, and use the "Delete" button to delete the derived data for your project. This will force a rebuild of the project's index, as well as tossing out any built products.
Some useful hotkeys for working on smaller screens are:
cmd-shift-y - Hide and show the console/debug area
cmd-0 - Hide and show the file navigation
You can now associate groups in the Project Navigator with folders on disk. It always bugged me before that the Xcode 3 file view may not represent the actual structure of the project on disk.
Create a new group, then with the new group highlighted, open the File Inspector. In the identity section on the right just below the 'Path' is a small icon, click this to open the file browser dialog allowing you to select the directory for this group.
If you hold the FN key then you can scan through your document with the cursorkeys, without moving the cursor
Jump bar:
if your write outside function:
// FIXME: A Bug
// TODO: Later
// ???: ???
// !!!: !!!
// MARK: This a mark
Will like:
Also you can search:
Click with Option(With Shift at the same time, you can choice another window), you can see a dialog letting you specify where the file should open:
Source control:
If you use svn or git, discard change is easy:
Interface buidler
In xcode 4.2, you can jump to interface builder here:
Click a ui object with Shift, it will show a list of all objects under the point.
Try this:
Select a ui object, hold Option, then move mouse over other objects. It will like:
Other
Hotkey: Shift+Command+O Open Quickly
Try:
#error message
#warning message
And read:
Writing and Editing Source Code
Text Editor:
Cmd-clicking on a class name will take you to the implementation (or header) file of that class, cmd-clicking on an object will take you to where the object is declared.
Alt-clicking will open a documenation popup for the object you clicked on.
Adding frameworks: You cannot simply do this by right-clicking on the project or a group (like in Xcode 3). You need to go to the project view, select your target and go to "Build Phases" tab. Frameworks can be managed under the "Link Binary with Libraries" section.
Setting target dependencies: Similar to the above, in the "Target Dependencies" section of the "Build Phases" tab.
Keep your archives safe -prior to installing Xcode 4 (from 3). You will lose them (most likely) and with them lose your ability to symbolicate your crash logs.
Delete action-BEWARE: "Delete" (instead of "Remove Reference Only") directly erases the file from your drive. It doesn't go into the Trash, so you cannot recover it.
cmd-J jumps to the editor, but if you have more than one editor open you get an overlay that has all of the open editors. you can use cmd-→ and cmd-← to switch between the open editors.
Also, if you are having a hard time finding an option or property for anything the utilites window is probably your best bet:
hide and show utilities: cmd-opt-0
Things that are commonly hidden here that aren't intuitive:
data model entity properties.data model propertiesxib properties
You don't need to declare IBAction - methods in .h File. & You can directly connect it in Interface Builder.
Example:
-(IBAction)btnLogInTapped:(id)sender {
// place following method in .m file of your code. save file. Open the
// .xib file of viewController
// click on files owner
// click on connection-inspector.
// you can view your methods under "received actions"
}
You don't need to declare variables, if you are creating property for it.
Example:
// a property declared in .h File of your view controller.
#property (nonatomic, retain) LogInVCtr *nxtLogInVCtr;
// synthesize above property in .m File
#synthesize nxtLogInVCtr;
// or
#synthesize nxtLogInVCtr = _nxtLogInVCtr;
// and you are done, you don't need to declare variable for it.
One of my favorites is the "Open Tab" behavior preference. If you go to "Behaviors" in Preferences, you can set it to go to a specific tab whenever a run starts. You can name the tab, something like "Debug" and Xcode4 will create it for you if it doesn't exist, and take you there when you start debugging. You can configure this tab just how you like it for debugging sessions, and it will stick that way after you close it, so whenever you do a run Xcode4 will be set up just how you want it, in a separate tab. You can even tear this tab off to a separate window, in which case Xcode4 will open a new specially configured "Debug" window each time you start a run! This feature is great because it keeps your regular code editing tab or window uncluttered with the log and debugger panes. Simply close the "Debug" window when you're done running and you can be back to your clean screen of code.
In XCode3, There was an option for "Rename the Project". Now, you just need to click twice ( second click slowly ) to rename the project & Its done.
To Limit the Number of Concurrent Build Operations
This is a (somewhat) hidden preference which allows you to define the number of concurrent compile tasks:
defaults write com.apple.dt.Xcode IDEBuildOperationMaxNumberOfConcurrentCompileTasks 4
To Disable Indexing in the IDE
This one disables code sense indexing, which can save a lot of resources in large projects:
defaults write com.apple.dt.XCode IDEIndexDisable 1
to reenable:
defaults delete com.apple.dt.XCode IDEIndexDisable
Note that you should quit Xcode, set the preference values as desired, changes take effect when you restart Xcode.
If you prefer using vim/emacs running on, say, Terminal, or any other external editor, you can just create your xcode project, add files and whatnot and then switch to editing said files using whichever edit suits you best; whenever you switch focus to an xcode4 window, the modified files will be reloaded( was also the case with Xcode3 ) and you can build / run your application.
Two kinds of Build location specification using XCode4 Preferences.
Location Specified By Targets - 'Build' Directory is located within your project directory.
Location in Derived Data ( Recommended by XCode, as you can see in screen shot ) - 'Build' Directory is located under DerivedData location specified ( e.g. here in my case, /Users/digicorp/Library/Developer/XCode/DerivedData )
Please look at following screen shots.
QUICK HELP
Ctrl + Option / Alt + f
Find the Quick help. For example,
#property (nonatomic, retain) NSString *strQuery;
My Text cursor is at NSString & if I press Ctrl + Option + f then It will show a quick documentation in the inspector/utilities.

Move to beginning of text in 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.

Xcode auto-completion - replace text keystroke?

Say we have a TestClass with the 2 methods, -getSomeString and -getAnotherString, and we are editing the following code and the cursor is in the location shown:
NSString *aString = [TestClass get<cursorIsHere>SomeString];
Say I want to change it to use -getAnotherString. If I bring up the auto-completion popup and select the other method via hitting enter or tab, I'm left with:
NSString *aString = [TestClass getAnotherStringSomeString];
i.e., it doesn't replace the existing text but rather just inserts.
Is there a special keystroke to make it replace the remaining text?
See IntelliJ for reference.
I don't think that there is a one step operation to achieve this. My suggestion would be similar to Thomas Templemann, but rather than two steps of forward word select and then Delete, I would expand to the desired autocomplete, by bouncing on Control + . and then hit Option + forward delete, which kills to the end of the word.
I don't think so. I have always used the following
double click on getSomeString
press Escape (or your autocomplete key)
find replacement method
that double click step has never really bothered me, but I would be interested if anyone knows better!
My work-around for this problem is this: Since the cursor will be right after the inserted text, I just press Shift-Option-Rightcursor, which selects the word past the cursor, then I hit the Delete key.
You can use the Tab key to perform "replace" instead of "insert" when choosing the method from the auto-complete popup in IntelliJ 9 (don't know if it's available in previous versions).

Resources