Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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
24 replies

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.

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 21, 2011 11:48 PM in response to Den B.

Thanks. I created a file from scratch with File > New File.

Den B., let me ask you this:
- 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)?

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

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

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.

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