Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get smart tabs ("indent with tabs, align with spaces") behavior in Xcode?

I used to use tabs for indentation and spaces for alignment. Like so (arrows show tabs and dots show spaces).

enter image description here

In QtCreator you can set such coding style in standard preferences. I can't find the way to achieve the same goal in Xcode. Could you please tell me if it is possible? May be there is a plugin for that?

UPDATE: Here is how it can be achieved in QtCreator: enter image description here

P.S. Please, don't try to persuade me that using spaces only/tabs only is better, otherwise this question will turn into another holywar :)

Once more about what I'm trying to achieve: Indent with tabs, align with spaces. Vim Tips Wiki.

UPDATE 2: I've just submitted an Apple Radar bug-report (enhancement) rdar://22285639. Here it is on the open radar.

UPDATE 3: I received an answer from Apple. My bug is a duplicate of rdar://8165023.

like image 803
FreeNickname Avatar asked Apr 25 '13 06:04

FreeNickname


People also ask

Why do programmers use spaces instead of tabs?

Their research found that spaces were far better for a number of different reasons. Not only is this technique more visually appealing, it allows programmers to make more money. The analysis performed by the team at Stack Overflow found that programmers who use spaces instead of tabs are making more money.

Are tabs or spaces the preferred method for indentation?

Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

How do I see tabs in XCode?

XCode 4 now supports tabs. You can enable by selecting "View / Show Tab Bar" menu. You can set custom keyboard shortcut to rotate between tabs in "Preferences / Key Bindings / Select Next Tab" option.


2 Answers

In Xcode > Preferences (,):

> Text Editing > Indentation

If I understand your question correctly, this should do it for you.

like image 54
Undo Avatar answered Sep 23 '22 04:09

Undo


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.

like image 23
OlDor Avatar answered Sep 23 '22 04:09

OlDor