Convert Tabs to Spaces on file save
It's good practice to use spaces instead of tabs when indenting code. Sublime Text 2 allows you to convert tabs to spaces in existing files manually (View -> Indentation -> Convert indentation to spaces).
However, this can be done automatically when saving the file. Paste the code below into a new file called ExpandTabsOnSave.py
and save it under $SUBLIME_HOME$\Packages\ExpandTabsOnSave\
.
import sublime, sublime_plugin, os
class ExpandTabsOnSave(sublime_plugin.EventListener):
# Run ST's 'expand_tabs' command when saving a file
def on_pre_save(self, view):
if view.settings().get('expand_tabs_on_save') == 1:
view.window().run_command('expand_tabs')
You can enable the plugin for a given project by pasting the following into the Project's .sublime-project
file:
"settings": {
"expand_tabs_on_save": true
}
If instead you wish to enable it globally, use your Settings file (Preferences -> Settings - User):
"expand_tabs_on_save": true
Written by Paulo Rodrigues Pinto
Related protips
9 Responses

You should release that as a installable extension via the package manager. Would make it easier to use.

That really useful to me, I've looking for this script long time. tkx so much!
Thanks very much!
Agreed with @ro-ka.

There is a package, but it works only in ST2.
If you're using ST3 you should clone my git repo: https://github.com/alfredbez/ExpandTabsOnSave-SublimeText
Otherwise search for 'ExpandTabsOnSave' in PackageManager and remove '.window' on line 7 in ExpandTabsOnSave.py

UPDATE: Use this Package for ST2 & ST3 https://github.com/bubenkoff/ExpandTabsOnSave-SublimeText

dont work for me in ST 3 :(

works with Sublime Text 3 for me. Thanks! That's perfect!

All fine on ST3 as well for me, just followed the instructions.
This is great.