622

When I select multiple lines of code and want to indent them as usual with TAB key, it just deletes them all. I come from Eclipse where I always did it that way. How's that done in Xcode? I hope not line by line ;)

DeveloBär
  • 673
  • 8
  • 20
Thanks
  • 40,109
  • 71
  • 208
  • 322

21 Answers21

936

The keyboard shortcuts are +] for indent and +[ for un-indent.

  • In Xcode's preferences window, click the Key Bindings toolbar button. The Key Bindings section is where you customize keyboard shortcuts.
Abizern
  • 146,289
  • 39
  • 203
  • 257
JWD
  • 12,188
  • 3
  • 34
  • 32
65

Select your code to reindent, then Go to

Editor -> Structure -> Re-Indent

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
48

+ [ and + ] are the equivalents to shift+tab in Xcode.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Nik
  • 1,364
  • 8
  • 7
  • 8
    It does apply to multiple lines if you have multiple lines selected at the time. – Nik Jun 13 '12 at 16:20
39

Here are the shortcuts, to format the code in XCode

  1. Format entire code (entire class/controller)

    Select the entire code and press control+I on mac to format your code.

  2. Format particular block of code

    Select the code and press:

    • +] for right move (indent)
    • +[ for left move (un-indent)

Note: as per @JavierGiovannini sugesstion you can do using Editor Menu option

  1. Select your code and navigate to Editor > Structure > Re-Indent
Community
  • 1
  • 1
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
21

First, select all code using command+a

Second, hold key ctr and then press i

the whole selected code will nicely indent.

16

Multiline Indentation Shortcut key:

ctrl + i

NOTE: Please select codes to Re-indent and press 'control' and 'i' on your mac.

Aashish
  • 2,532
  • 2
  • 23
  • 28
12

Another way to quickly reformat indenting is a quick cut and paste. +x and +v. I often find it faster than +[ or +] as you can do it with one hand (versus two) and it will reformat to the correct indent level in one shot.

Joel
  • 15,654
  • 5
  • 37
  • 60
10

If you use synergy (to share one keyboard for two PCs) and PC(MAC) in which you are using xcode is slave, and master PC is Windows PC

keyboard shortcuts are alt+] for indent and alt+[ for un-indent.

Update:

But from synergy version 1.5 working +[ for indent and +] for un-indent

MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
7

In Xcode 9, you can finally use Tab and Shift+Tab to indent multiple lines of code. Yay!

phatmann
  • 18,161
  • 7
  • 61
  • 51
3

Select "Tab key: Indents always" in Preferences->Text Editing->Indentation Then you can indent a single line or a selection of lines by pressing TAB or SHIFT+TAB Sadly this removes altogether the possibility to insert tabs where you want, and conflict badly with the tab key being used to switch between "autocompletion fields".

I guess we need more tab keys in the keyboard, one is not enough...

hariseldon78
  • 1,027
  • 14
  • 22
2

To all frenchs Xcoders around, the default keybindings are as follow :

Indent selection:

+ + + )

Unindent selection:

+ + + (

Community
  • 1
  • 1
Neimsz
  • 1,554
  • 18
  • 22
2

For those of you with Spanish keyboard on mac this are the shortcuts:

+ + [ for un-indent

+ + ] for indent

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
2

For code indentation first select the lines of code then press:

command + alt + [

command + alt + ]

Ali Raza
  • 2,796
  • 1
  • 20
  • 24
1

here all the important shortcuts from another question answered on stack overflow here

Mostafa Sultan
  • 2,268
  • 2
  • 20
  • 36
1

If you want to remap the standard Xcode's +[ 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

enter image description here

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.

OlDor
  • 1,460
  • 12
  • 18
1

Danish Keyboard

Unindent: + + 8

Indent: + + 9

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
1

Basically [ for left multiple indent and ] right multiple indent.

For TR keyboard you can left multiple indent with 8 and right multiple indent with 9.Because both 8 and 9 are ALT characters of the these nums.Or if you have a keyboard which has [ or ] characters are belong to ALT() property so you can use KEYBOARD CHARACTER

eemrah
  • 1,603
  • 3
  • 19
  • 37
0

Since I didn't see an update to this question for the current version of Xcode, I thought I'd add that in Xcode 9.3, Tab works for indenting selected line(s) of text as well as moving from one autocomplete field to another.

0

Select the lines and press Ctrl+i from keyboard.

You will have the lines indented.

-1

Tab for Indent SHIFT + Tab Re-indent

Asim Khan
  • 508
  • 1
  • 7
  • 21
-1

In Xcode 4.2 auto-indenting is pretty good. You can now indent a selection of code by pressing the Tab key. I find that Xcode generally formats code really well automatically, and you rarely have to move things around yourself. I find it faster to select a piece of code, right-click and choose Structure -> Re-indent if some code looks messy.

machineboy
  • 321
  • 3
  • 14