I want to replace all the "four spaces" that is written by another text editor with tabs. How can I do it?
8 Answers
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.
-
3While all answers are correct this one is the easiest one! Thanks – Mohsen Mar 05 '12 at 23:39
-
1Gotta admit; it is :) Never used the status bar context menus, guess I always thought they were static indicators. – Ekin Koc Mar 05 '12 at 23:42
-
2They are not as stand out as they perhaps should be, I have a couple of bugs I must file for sublime no show stoppers. Excellent editor. – ncremins Mar 05 '12 at 23:50
-
You can also make use of settings in a [sublime-project file](http://www.sublimetext.com/docs/2/projects.html) if you want the editor to remember this setting for all files in a project – Michael Aug 23 '12 at 13:18
-
Saved me a load of time, wrote my own script to do this because using "Search and replace" didn't work properly most of the time working in .py files between platforms :/ – Torxed Oct 01 '12 at 15:18
-
I also can't find it at first, but the placement of the command makes sense! – Nov 20 '12 at 03:17
-
@Michael what is the setting called? There's a "translate_tabs_to_spaces" option, but not a "translate_spaces_to_tabs" option. Very odd. – phreakhead Feb 06 '14 at 08:00
-
1@phreakhead I am not sure if I fully understand your need (consider opening a new question?), but if you have translate_tabs_to_spaces set to false, then when you are typing (new text) and you hit tab, it will be a tab character. if you have a file that already has some indentations as 4spaces (instead of tabs) you can use the option at the bottom of the menu in the screen shot to convert indentation to tabs. – Michael Feb 06 '14 at 14:37
-
@Michael yeah exactly. I want it to do it automatically, instead of having to click the menu at the bottom. – phreakhead Feb 25 '14 at 19:18
-
Here's my two settings (put in 'Sublime Text' > 'Preferences' > 'Settings - User') and make sure settings are wrapped in curly braces: "tab_size": 2, "translate_tabs_to_spaces": true – Kelly Apr 05 '14 at 03:02
Select all, then:
Windows / Linux:
Ctrl+Shift+p
then type "indent"
Mac:
Shift+Command+p
then type "indent"

- 17,165
- 13
- 68
- 104

- 72,308
- 93
- 206
- 262
-
-
Amazing ... this gives you the option to "Reindent Lines" from files like Bootstrap that comes with 2 lines indentation – Shina Apr 17 '15 at 04:52
-
I think this doesn't do exactly what he's asking. This also reindent the lines so it actually changes the indentation, and he's only asking about converting spaces into tabs. Anyway it's a good answer but in some cases it can mess up your indentation (think of blade indented templates for example). – Sergi Ramón Sep 09 '15 at 09:20
-
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

- 1,849
- 3
- 22
- 29
-
5This is the correct answer, because even after selecting "Indent using spaces", that option will reset on restart. – mvd Oct 11 '13 at 18:09
-
2OP asked for the inverse. Which strangely, there is not an option for. Tab haters. – phreakhead Feb 06 '14 at 07:59
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.

- 158,873
- 26
- 254
- 302
-
3
-
3@MaxNanasy So it is. I guess my dislike of tab characters is so deep it's subconscious. – Dave Newton Apr 01 '13 at 19:29
-
3This answer is completely irrelevant to the question being asked. Please consider changing it to the correct answer or otherwise deleting your answer. – Randy the Dev Apr 29 '14 at 04:26
-
2@AndrewDunn "Completely"? Really? How could the inverse of something be "completely" unrelated? By *definition* it's related, and the fix is obvious. If only we had the ability to edit answers. – Dave Newton Apr 29 '14 at 10:25
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

- 9,636
- 4
- 29
- 35

- 498
- 6
- 10
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

- 12,716
- 3
- 49
- 46
If you want to recursively apply this change to all files in a directoy, you can use the Find > 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.

- 2,090
- 23
- 41