Changing Between Spaces and Tabs in Sublime Text

Avatar of Chris Coyier
Chris Coyier on

Sublime Text is pretty dang good at making it easy to switch between using tabs and spaces to indent your code. More importantly, it makes it easy to adjust the indentation of code that doesn’t match your preference. I thought I’d put this together for reference, as there is a particular sequence of steps for some of the transitions that needs to be followed.

The first step is making sure your User Preferences are how you like them.

I prefer spaces, so my settings are like this:

{
  // other stuff

  "tab_size": 2,
  "translate_tabs_to_spaces": true,

   // more other stuff
}

Adjust to your liking there.

You can override these settings on any given file, from the bottom right.

The bottom right corner is where you can see the current settings for the open document.

This is also headquarters for fixing up a document that isn’t how you like it.

Changing from Tabs to Spaces (Same Level of Indentation)

Here’s a document that is in tabs right now. I can tell because I’ve selected the text and there are dashes in the white space not dots. Dashes are tabs, dots are spaces. Also, in the bottom right, I can see it says “Tabs Size: 2”.

You can see that those Tabs are 2 spaces wide. Assuming I want to switch to spaces and I’m happy with 2 spaces per tab, I just select “Convert Indentation To Spaces” from that menu.

And I’m good to go for that case.

Changing from Tabs to Spaces (Different Level of Indentation)

Let’s say a straight conversion from tabs to spaces isn’t going to do it for me. Say the tab level is set to 6 spaces per tab, and I want to convert to spaces, but only 2 spaces per indentation. The trick here is to adjust the spacing while in tabs first, then convert.

Changing From Spaces to Spaces (Different Level of Indentation)

What if you have a file that is in spaces how you like it, but it’s using 4 spaces instead of 2? Sublime Text can still help here. You use the ability of tabs to be of variable length. So first convert to tabs, adjust the width, then switch back to spaces.

Changing From Spaces to Tabs

I think we’re getting the hang of this now. You can do any of this in the reverse direction if you prefer tabs.


I think you can extrapolate other switching scenarios from there.

I don’t think there is any scenario that you can’t adjust to your liking this way. If you’re caught manually adjusting indentation (that is consistent within itself), stop, you can figure out a way to fix it with settings.

If you want to discuss tabs vs. spaces below, go for it, but I can tell you I really don’t care. I mostly care that any particular project is consistent about it and enforces it.

.editorconfig

Also worth mentioning: There is a thing called EditorConfig. You put a file in the root of your project called .editorconfig. If someone opens that project in an editor that supports it, those settings will be honored. There is one for SublimeText.

The file might look something like:

# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false