XCode > Preferences > Indentation > Tabs

Tab key inserts tab, not spaces is unchecked, and Tab width = 4, and Indent width = 4.

However, typing a tab does not seem to result in 4 spaces. If I open a *.cpp or *.h file with an internet browser, what appeared nicely aligned in XCode, is now displayed with ragged indentation. So I have to go back to editing the file and replace each tab with 4 spaces... Not convenient.

Is there an easier fix?

MacBook5,1, Mac OS X (10.6.1)

Posted on Jan 16, 2011 4:01 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 26, 2011 3:41 AM

Hi

A useful trick in Xcode is to re-indent the whole file; this is particularly useful when the file comes from someone else, or has been developed on a different editor/IDE, or when you've changed the preference settings.

I discovered through trial and error that a fireproof way to do this is as follows:

Make sure the file is in the Xcode editor, and click on any line to put the cursor there.
Cmd-A to highlight the whole file.
Cmd-[ enough times to remove any previous indentation.
From the menu: Edit > Format > Re-indent

This will re-indent the file using the current preferences and frequently sorts out a lot of these tab/space issues.

Bob
24 replies
Question marked as Top-ranking reply

Feb 26, 2011 3:41 AM in response to etresoft

Hi

A useful trick in Xcode is to re-indent the whole file; this is particularly useful when the file comes from someone else, or has been developed on a different editor/IDE, or when you've changed the preference settings.

I discovered through trial and error that a fireproof way to do this is as follows:

Make sure the file is in the Xcode editor, and click on any line to put the cursor there.
Cmd-A to highlight the whole file.
Cmd-[ enough times to remove any previous indentation.
From the menu: Edit > Format > Re-indent

This will re-indent the file using the current preferences and frequently sorts out a lot of these tab/space issues.

Bob

Feb 27, 2011 1:25 PM in response to frangipane

frangipane, I'm not sure what you're doing, but it worked perfectly for me. You didn't confirm that you downloaded & installed the latest version of Xcode (in one of your posts you said that you were using v.3.2.4, so that might be your issue.)

I actually appreciate Bob Lang's input on using the Re-Indent command. It works really nice. Here again what you need to do, step by step:

1. First open up Xcode and click the Xcode item in the menu. Then go to Preferences -> Indentation and uncheck "Tab key inserts tab, not spaces". Click OK to confirm. (This part works more like Microsoft menu, and not Apple's, so clicking OK or Apply might be somewhat confusing here.)

2. Open your source file in Xcode (.m, .h, .c, .cpp. etc. file)

3. Click somewhere in the source and select all with ⌘+A

4. Now you need to "mess-up" your current indenting for Xcode to fix it on the next step. (You might want to submit it as a bug at a link posted by Bob above, because Xcode won't do much if it finds the current indenting to be correct.) OK, to do this, simply hit ⌘+[ one or a couple times to make the code all bunched up to the left.

5. In the menu go to Edit -> Format -> Re-Indent.

At this point the Xcode will re-indent the selection (i.e. the whole file) and you can have your 4 (or as many you set up) spaces instead of tabs. Make sure to save and continue on with the next source file.

Aug 26, 2011 2:37 PM in response to frangipane

After complaints from my co-worker about tabs showing up in the code, I, too, was trying to track this one down. XCode was inserting tabs no matter what my setting in Preferences was. I think I may have found a 'fix', but I don't want it to be the final answer.


I reverted to XCode Default key bindings.


I haven't gone through all my custom key bindings to see if there is one that causes it (yet), but it definitely affects the tab/spaces issue.


I need my key bindings. I am switching between Mac and PC all day and using the same keyboard. But just in case it helps someone, here it is.

Feb 21, 2011 2:30 PM in response to frangipane

frangipane, I just tried the "Tag key inserts tab, not spaces" option in the preferences for Xcode and it worked exactly as it's supposed to. You must keep in mind that it will not change tabs that are already in your source file. To change those you'll need to reformat the code. The easiest way to do it in Xcode is to select a code block and then hit ⌘ + [[ and then ⌘ + ]] for each tab space.

Feb 22, 2011 1:38 AM in response to frangipane

frangipane wrote:
- Uncheck Tab key inserts tab, not spaces
- Press tab
- Press left key.

Is the final position of the cursor back to its initial position, or to its right (by 3 spaces)?

3 spaces to right.

frangipane wrote:
In my case it's back to the initial position. I expect 3 spaces to the right!

This is very trivial. But I'm assuming that you're OK'ing the Preferences window after you make the changes...

Feb 27, 2011 3:28 AM in response to Bob Lang1

Thanks. But it does nothing. For example, typing

\t{
\t\tusing namespace std;
\t}

is equivalent to

\t{
\tusing namespace std;
\t}

Notice the 4 spaces at the beginning of the 2nd line. Selecting these 3 lines and doing Edit > Format > Re-indent does nothing.

Anyway, the problem persists even if I start a new project (not just a file), as reported in previous messages.

Feb 27, 2011 6:02 AM in response to Bob Lang1

"I can safely say that I've never found an IDE which performs indentation exactly as I would like, so it's always a compromise!"

That's not a compromise, but an inconsistency between the stated functionality (Tab key insert spaces, not tab) and the actual one. In CodeBlocks, for example, a tab is exactly 4 spaces, whichever place it appears in the code. To be clear,
typing

\t{
\t\tusing namespace std;
\t}

comes out like this

[space][space][space][space]{
[space][space][space][space][space][space][space][space]using namespace std;
[space][space][space][space]}

It's not a matter of giving good points to Paul and bad points to Jack. Just calling a spade a spade.

Jan 16, 2011 5:54 PM in response to etresoft

"Did you create this file from scratch? Or did you inherit it from someone else who used tabs?"

Sometimes yes, sometimes not. Will take that into account.

"Also, how are you displaying the code in a web browser? "

I just use open file with firefox.app, say. But I also upload the code to repos with the same outcome.


"Are you using a monospaced font?"

In XCode I changed the font to Monaco 12. Is that consequential? The default font is too small for my eyes.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

XCode > Preferences > Indentation > Tabs

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.