zvyg7a
Last Updated: December 10, 2016
·
16.66K
· regularjack
6464bf0dc2232bd1e44a32618cd68289

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

9 Responses
Add your response

3826
E5d6e8e64e78a5ad4a33db3beff589dd

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

over 1 year ago ·
9585
85cf9d33ff5ecca0bbdd20baee43564c

That really useful to me, I've looking for this script long time. tkx so much!

over 1 year ago ·
10115

Thanks very much!

Agreed with @ro-ka.

over 1 year ago ·
15409
26c38a899d6b4dd0561cb1900a0b9315

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

over 1 year ago ·
21222
26c38a899d6b4dd0561cb1900a0b9315

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

over 1 year ago ·
22033
255c42dd857e422ea44c892b067eeaeb normal

dont work for me in ST 3 :(

over 1 year ago ·
26733
None

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

over 1 year ago ·
26756
0 fo2qejjvqdl66hmxfsdveyeq6wnfqhmxdm4jeyjhjmc9mumk 2pexos6hl9jlf7hadaz5jf5hhgj

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

over 1 year ago ·
28372

This is great.

2 months ago ·