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

Feb 22, 2011 4:56 AM in response to Den B.

Thanks. You are assuming correctly. I have XCode 3.2.4. To be sure, I restarted my machine, opened Xcode, made sure Tab insert spaces is unchecked, created a new project, and retyped tab followed by left key in main.cpp. The final position of the cursor is 0, not 3 spaces to the right of its initial position.

Should I re-install XCode, at this point?

Feb 27, 2011 4:25 AM in response to frangipane

Now I'm confused.

It looks to me that you've asked the editor to indent a line of source code, and it's done what you've asked.

If you don't like the way Xcode indents source lines, then you can either play with the settings on the Indentation preference pane, or manually indent each block of lines using cmd-open square bracket and cmd-close square bracket

From what I can see, Xcode doesn't offer the same level of control over indentation as (say) Netbeans. If a particular source layout is important to you, then you may have to find another IDE, although 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!

Bob

Message was edited by: Bob Lang1

Mar 4, 2011 5:32 AM in response to Den B.

Thanks, but Re-indenting has never been the core issue. I'm not going to do this file by file, as you suggest. It's a pain. I just do a Edit > Find in project and replace \t by 4 spaces (in the whole project).

Indenting right in the first place was the issue. I'm doubtful re-installing will do anything, and besides I'm up-to-date on software updates (so your version might be a little ahead of standard updates), but thanks.

I have a feeling (nothing more scientific than that) it might be a OS setting that is conflicting with XCode.

Apparently it's too much to ask that unchecking

Tab inserts space not tab, not space

actually do, always, what it's supposed to do.

Mar 4, 2011 10:10 AM in response to frangipane

I think you are confusing a simple preference settings "tab key inserts spaces/tabs" and "tab spaces" with the much more complex syntax-aware indenting. You are never, and I mean never, going to get syntax-aware indenting to work the way you want. It works the way it does. If you are OK with that, use it. If not, turn it off.

Unfortunately, the developers at Apple who built Xcode are just like developers everywhere. They have their own way they think code should look. The standard "look" that is pervasive these days is essentially this:
no spaces where they would be useful
no alignment of related items
newlines can be used only where they are confusing and are forbidden anywhere else
keep all lines to a minimum of 256 characters

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.