Once I start editing my code and adding for loops or if then statements my code indentation is whacked because the previous code maintains its former indentation instead of adjusting automatically.
In Visual Studio you can highlight code and select 'format selection' to fix these issues - is there anything similar in Xcode?
Before Xcode 9 (for Xcode 9 see default shortcut below)
select text
right-click
there's a "Re-indent selection" item in popup menu
In XCode 4 there is one more step:
select text
right-click
Click on "Structure"
there's a "Re-indent selection" and other options in popup menu
Or using the default shortcut:
select text
press ctrl + i
select text
press Control-I (the default keyboard shortcut)
You can do this automatically in Xcode 11
In Xcode 4.6.3, choose Editor > Structure > Re-Indent.
Shortcut: Control + I
(that's 'i', for 'indent')
This works on current selection. If no text selected, choose Edit > Select All (⌘+A) and then perform the re-indent.
(I'm more clearly restating the other answers)
These solutions re-indent text to the default xCode behaviors. To change these behaviors, go to xCode (v.4) > Preferences > Text Editing. Then to Indentation Tab.
Xcode 9.2:
You can select text and press ctrl + i to re-indent code.
You can do it in different ways, but these two are the easiest
select text ( ⌘ +a) :
Using shortcut -> (control)^ + i
Click on "Editor" -> Click on "Structure" -> Click on "Re-indent"
Sometimes setting Xcode global preferences is not enough for ^ + i to do its job accordingly. The reason is that project settings override global ones. You can set indentations for a concrete project or even a file.
Select project (or file) in the Navigation Pane. Then in File Inspector go to Text Settings section and set desired number of tabs or spaces for indents.
To quickly re-indent the whole file:
⌘+a
⌘+x
⌘+v
Edit: No longer working in Xcode 11
You can try my extension Swimat
https://github.com/Jintin/Swimat
It support format on selection.
Simple install by brew cask install swimat
Related
Is it possible to 'Open Quickly' in the right pane of the Assistant Editor? This would increase productivity by like 20x
Yes it's possible, but depending on your Xcode version you may need to adjust the default behavior in Xcode's preferences.
Use ⌘ command+⇧ shift+O to open the "Open Quicky" input box, and then:
Use ⌥ option+Enter to open the file in the right editor.
Use ⌥ option+Shift+Enter to select where you want to open the file using the arrow keys.
On Xcode 12, the default action of ⌥ option+Enter is to open the file in a new "Tab" (which are new in this release, and different from a "Window Tab"). This is still configurable in the preferences.
On Xcode 11, you can split the editor as much as you want. This means ⌥ option+Enter by default will open the file in the editor next to where your cursor currently is.
This behavior is configurable in Xcode's Preferences, under Navigation:
To have ⌥ option+Enter open the selected file in the editor on the right of the Xcode window, select "Optional Navigation: Uses Second Editor".
Make sure that Uses Focused Editor is selected in Preferences > Navigation:
This way, you can open it by focusing the Assistant Editor, pressing ⌘ command+⇧ shift+O to open the Open Quicky input box, and just press Enter to open the selected file, instead of ⌥ alt+Enter.
An even easier way is to hold down the Option key and then click on the file. That will bring open the chosen file in the Assistant Editor. This works great when you TDD your Swift :) You can have your tests on the left and option click on the implementation to open on the right.
Cool thing!
Just to extend the answer you can switch between more then two Tabs or open it new editor tab cool isn't it?
• Open multiple assistant editor .
• ⌘ command+⇧ shift+O.
• Hold ⌥ option and press Enter on the selected item.(Used to open any file in Assistant Editor)
• You can also use ⌥ option+⇧ shift+Enter to toggle more and
open new tab if you're not on stacked editor.
• The multiple selection window will appear
Note: Make sure you have all editor stacked selected
In addition to Guillaume's answer, be mindful that the editor is not in the single editor mode (I don't know what it should be called officially though) that you can disable by selecting this button, otherwise all navigations become "Uses Focused Editor":
EDIT: misread your question above answer is good, just make sure you click into that pane before trying those commands
Sounds like your looking for some hotkeys, check here for a full list
http://spin.atomicobject.com/2014/03/23/xcode-keyboard-shortcuts/
http://iphonedev.tv/blog/2014/9/15/14-xcode-time-saving-shortcuts-memorize-and-improve-your-productivity
http://nshipster.com/xcode-key-bindings-and-gestures/
but to open assistant editor use ⌘ command + ⌥ option + ↵ return
in the past (older version of xcode) I had a plus sign in the top right of an editor to add a tab. Then in the new tab I could click on the second file and I had 2 files open in 2 tabs.
My plus sign is gone (I guess with my last update).
How to rollback this + button or how to have tabbed editors in Xcode 6.4 (6E35b) ?
You can show the tab bar (with the + button you mention) from the View menu:
Alternatively you can press CMD+T to open a new tab, which will automatically show the tab bar.
File -> New -> Tab and the + sign will appear
You can use -- Command + T or you can follow this also
Is there a way to remap Tab key in order to produce a block indent in Xcode (like Visual Studio)?
I added a key binding Shift+Tab to override ⌘+[ and it is working.
Even if I change ⌘+] to Tab and resolve the mapping conflicts, it doesn't work.
I'm using Xcode 6.1.1
Don't use the Key Bindings tab of Xcode's preferences. Go to the Text Editing tab. Within that tab, there's an inner tab control. Select "Indentation". There's a pop-up menu labeled Tab Key with options Indents in leading whitespace, Indents always, and Inserts tab character. It sounds like you want Indents always.
If you want to remap the standard Xcode's Cmnd+[ to Tab, you can do the following:
1) Install a program called "Karabiner".
2) Go to Preferences ~> "Misc and Uninstall"
3) In "Custom Setting" section click on the "Open private.xml" button
4) Open the file and replace its content with the following code:
<?xml version="1.0"?>
<root>
<item>
<name>Tab instead of Command+[</name>
<identifier>private.swap_tab_and_cmnd+[</identifier>
<autogen>
__KeyToKey__
KeyCode::TAB, ModifierFlag::NONE,
KeyCode::BRACKET_RIGHT, ModifierFlag::COMMAND_L | ModifierFlag::NONE
</autogen>
</item>
</root>
5) Go to Preferences ~> "Change Key" ~> click on "Reload XML" button
6) Put a tick in the checkbox which is called "Tab instead of Command+["
7) Enjoy
PS: Please note that this setting works globally on your machine, not just in Xcode. However, I am sure that google might help you to find a solution how to customize it even further, applying the changes only to Xcode.
You can indent a line of code or a selected block of code as follows:
Command-[ move code left
Command-] move code right
Is there any keyboard shortcut to switch to the project navigator, select a file and then edit the file? I use shortcuts for switching between tabs etc and I find it troublesome to use the mouse to choose a new file in the project navigator and then click the main area again to start editing it..
Thanks!
I hope I understand what you're asking:
Command - Option - ~ (Tilde)
This will navigate through areas you have open in Xcode. If you only have the project view and editor open, this should bounce between the two.
All you need to do is: ^~ (Command + Tilde)
I like the auto-indentation in XCode 4 when coding/typing, but I want to disable this function when I'm pasting the code. So is there a way to have separate settings for the two situations?
Instead of using a normal paste, use "Paste and Preserve Formatting". It's in the Edit menu, and its shortcut is "Alt+Shift+Cmd+V".
You can enable or disable in Xcode 11
Xcode > Preferences > Text Editing > switch to Re-indent on paste
No this option.
Why don't you use Re-Indent function via shortcuts after paste.
When you paste some code to current file, press Command+A to select all, then press ^I.
All the code will be re-indented.