How can I indent multiple lines in Xcode?
Asked Answered
O

21

625

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

Onaonager answered 6/5, 2009 at 14:43 Comment(3)
I wish the Tab key did this in Xcode.Bigham
Anyone know if you can remap the tab key to this behaviour?Zeller
It's funny because I'm pretty sure I was able to use Tab for right indent before. It just one day stopped working...Persas
C
937

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.
Coir answered 6/5, 2009 at 14:47 Comment(11)
Note that these are commonly used Mac OS X key bindings thus you can indent using CMD-] and CMD-[ in, for example, Mail.app.Parian
Some apps that also run on other OSs use TAB and SHIFT+TAB instead, like eclipse and any derivativesPeppard
Additionally, Ctrl + I reindents your entire code in the active editor based on your text-editing preferences in Xcode.Topflight
Thanks for that tip - I didn't know that. Frustratingly, on German keyboards, there is no [ or ] key - so to indent, you actually have to hold CMD+ALT+6 to indent. God, I miss Visual Studio sometimes...Bowknot
French keyboards should be the worse I think: ctrl+alt+shift+')'. Four stupid keys to indent a few lines. That's just as bad as it can be.Fictionalize
Haha, with a Swiss (FR-CH or DE-CH) keyboard it is Cmd+Shift+Alt+6, and 5 to unindent... could not be worse.Rivero
I am going to abandon XCode as soon as Appcode gets its act together with Swift. For people who have not used IntelliJ IDEs, I feel very sorry!Croupier
Swedish: CMD+ALT+8 and CMD+ALT+9Stupefacient
Coming from windows into os x I wish I would had known this days ago.Doran
Intuitive. Not.Fess
@FredrikJohansson I was just thinking that this is (almost) a universal code editor shortcuts standard. 800+ points just for answering this question!? o_O Fair enough.Alenealenson
A
66

Select your code to reindent, then Go to

Editor -> Structure -> Re-Indent

Altricial answered 12/11, 2011 at 22:32 Comment(3)
-1: The question was how to indent a block of lines, not on how to have it indented automatically by XCode. IMHO, this is a big difference.Inglis
not the correct answer, but it's a useful reminder anyway!Mceachern
Don't forget to '⌘ + A' before doing itAgronomy
T
48

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

Toffee answered 6/5, 2009 at 14:46 Comment(1)
It does apply to multiple lines if you have multiple lines selected at the time.Toffee
R
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
Rachellrachelle answered 16/8, 2013 at 6:1 Comment(2)
1. It should be Ctrl + IViolante
It is ctrl + i !!! The letters l (lowercase L) and I (uppercase i) look equal! Please use lowercase. I'm unable to edit this post due to SO's inexplicable 6-chars restriction.Aegis
I
21

First, select all code using command+a

Second, hold key ctr and then press i

the whole selected code will nicely indent.

Interpretive answered 1/3, 2018 at 16:14 Comment(0)
M
18

Multiline Indentation Shortcut key:

ctrl + i

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

Mingy answered 27/2, 2020 at 17:23 Comment(0)
S
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.

Surculose answered 17/11, 2013 at 5:49 Comment(2)
IMO, you should compare it to +I which "has it indented automatically by XCode", not just "indent a block of lines".Watchdog
This also works for us that does not work on an american keyboard :)Cero
M
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

Membrane answered 20/6, 2013 at 12:28 Comment(0)
P
7

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

Puente answered 18/9, 2017 at 20:54 Comment(0)
M
4

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

Melainemelamed answered 14/10, 2015 at 0:45 Comment(0)
S
3

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

command + alt + [

command + alt + ]

Scylla answered 17/7, 2020 at 5:36 Comment(0)
S
3

Select the lines and press Ctrl+i from keyboard.

You will have the lines indented.

Subequatorial answered 11/8, 2022 at 5:39 Comment(0)
L
2

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

Indent selection:

+ + + )

Unindent selection:

+ + + (

Lewan answered 22/10, 2015 at 14:1 Comment(0)
P
2

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

+ + [ for un-indent

+ + ] for indent

Primm answered 19/2, 2016 at 18:13 Comment(0)
S
1

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

Stunner answered 20/6, 2015 at 10:15 Comment(0)
F
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.

Factoring answered 10/12, 2015 at 20:23 Comment(0)
E
1

Danish Keyboard

Unindent: + + 8

Indent: + + 9

Erlond answered 10/6, 2017 at 15:14 Comment(0)
A
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

Aishaaisle answered 27/2, 2018 at 10:8 Comment(0)
O
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.

Ode answered 10/4, 2018 at 0:23 Comment(0)
L
-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.

Lancastrian answered 27/11, 2011 at 12:13 Comment(2)
i think you have not used any other code editor except xcode in your life. xCode autoCodeFormatting is the worst.Tengdin
Doesn't work in XCode 8.3. Don't know if it ever did.Argumentation
C
-1

Tab for Indent SHIFT + Tab Re-indent

Caligula answered 16/10, 2018 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.