Sublime Text 3 – How to set different tab size based on file type?

Recently I’ve been switching back-and-forth developing in multiple languages, in particular, Python (for backend and data science stuff) and JavaScript (for front-end web demos and ReactJS stuff). As I use Sublime Text 3 to do the code editing, I very often find myself in a situation where I had to manually change the tab size to 4 spaces when coding in Python, and 2 spaces when coding in JavaScript. The manual process eventually got quite painful and tedious so I decided to find out if Sublime Text 3 is clever enough to automatically set the desirable tab sizes based on the file type?

Fortunately the answer is yes – this post documents how (once you know how it only takes less than a minute to setup – Read on!)

What we want to Achieve

  • If we have a Python file, Sublime Text auto set tab size to 4.
  • If we have a JavaScript file, Sublime Text auto set tab size to 2.

(and some other desirable tab sizes for other languages – concept is the same).

The Solution

Follow the step-by-step guide – should take about a minute.

Step 1

First, make sure the Python file is opened in editor and active.

On Mac: Sublime Text -> Preferences -> Settings - Syntax Specific.

(On Windows and Linux I presume it would be something similar)

Step 2

A blank Python.sublime-settings file will open. Simply copy and paste this JASON text to it:

Save and close the file.

Step 3

Repeat Step 1, but this time do it for the JavaScript file.

A blank JavaScript.sublime-settings file will open. Simply copy and paste this JASON text to it:

Save and close the file.

Finished!

Now, whenever a Python file is opened, tab size will be set to 4 automatically. And whenever a JavaScript file is opened, tab size will be set to 2 automatically. (Try this out for other languages as you need).

Reference: This Stackoverflow forum

Leave a reply