How to replace four spaces with a tab in Sublime Text 2?

EditorSublimetext

Editor Problem Overview


enter image description here

I want to replace all the "four spaces" that is written by another text editor with tabs. How can I do it?

Editor Solutions


Solution 1 - Editor

Bottom right hand corner on the status bar, click Spaces: N (or Tab Width: N, where N is an integer), ensure it says Tab Width: 4 for converting from four spaces, and then select Convert Indentation to Tabs from the contextual menu that will appear from the initial click.

Similarly, if you want to do the opposite, click the Spaces or Tab Width text on the status bar and select from the same menu.

enter image description hereenter image description here

Solution 2 - Editor

Select all, then:

Windows / Linux:

Ctrl+Shift+p

then type "indent"


Mac:

Shift+Command+p

then type "indent"

Solution 3 - Editor

To configure Sublime to always use tabs try the adding the following to preferences->settings-user:

{
    "tab_size": 4,
    "translate_tabs_to_spaces": false
}

More information here: http://www.sublimetext.com/docs/2/indentation.html

Solution 4 - Editor

On main menu;

View -> Indentation -> Convert Indentation to Tabs / Spaces

Solution 5 - Editor

Do a regex "Search" for \t (backslash-t, a tab), and replace with four spaces.

Either the main menu, or lower-right status-bar spacing menu does the same thing, with less work.

Solution 6 - Editor

create a keybinding for quickest way

{ "keys": ["super+alt+t"], "command": "unexpand_tabs", "args": { "set_translate_tabs": true } }

add this to Preferences > Key Bindings (user) when you press super+alt+t it will convert spaces to tabs

Solution 7 - Editor

You could add easy key binding:

Preference > Key binding - user :

[    { "keys": ["super+l"], "command": "reindent"},
]

Now select the line or file and hit: command + l

Solution 8 - Editor

If you want to recursively apply this change to all files in a directoy, you can use the Find > Find in Files... modal:

Find in Files modal

Edit I didn't highlight it in the image, but you have to click the .* button on the left to have Sublime interpret the Find field as a regex /Edit

Edit 2 I neglected to add a start of string anchor to the regex. I'm correcting that below, and will update the image when I get a chance /Edit

The regex in the Find field ^[^\S\t\n\r]{4} will match white space characters in groups of 4 (excluding tabs and newline characters). The replace field \t indicates you would like to replace them with tabs.

If you click the button to the right of the Where field, you'll see options that will help you target your search, replace. Add Folder option will let you select the folder you'd like to recursively search from. The Add Include Filter option will let you restrict the search to files of a certain extension.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohsenView Question on Stackoverflow
Solution 1 - EditorncreminsView Answer on Stackoverflow
Solution 2 - EditorWeb_DesignerView Answer on Stackoverflow
Solution 3 - EditorSimonView Answer on Stackoverflow
Solution 4 - EditorEkin KocView Answer on Stackoverflow
Solution 5 - EditorDave NewtonView Answer on Stackoverflow
Solution 6 - EditorIbrahim BenzerView Answer on Stackoverflow
Solution 7 - EditorDPPView Answer on Stackoverflow
Solution 8 - EditorBobbyAView Answer on Stackoverflow