[tabs] Sublime Text 3, convert spaces to tabs

I know there are a lot of posts about this, but I couldn´t get it to work.
I use tabs for coding. Is there a way, to convert always spaces to tabs? I.e. on open and on Save files? Anyone got an idea?

// edit:
My desire is to do this automatically! -> open, save or on the fly
Does anyone know how to do?


I tried this:

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')

And here are my user Settings:

{
    "auto_complete_selector": "source - comment, meta.tag - punctuation.definition.tag.begin",
    "auto_indent": true,
    "detect_indentation": true,
    "draw_white_space": "all",
    "ensure_newline_at_eof_on_save": true,
    "expand_tabs_on_save": true,
    "font_face": "SourceCodePro-Regular",
    "font_size": 10,
    "format_on_save": true,
    "ignored_packages":
    [
        "Vintage"
    ],
    "indent_to_bracket": true,
    "open_files_in_new_window": false,
    "smart_indent": true,
    "tab_size": 4,
    "translate_tabs_to_spaces": false,
    "trim_automatic_white_space": true,
    "trim_trailing_white_space_on_save": true,
    "use_tab_stops": false,
    "word_wrap": false
}

This question is related to tabs sublimetext sublimetext3 spaces auto-indent

The answer is


As you might already know, you can customize your indention settings in Preferences.sublime-settings, for example:

"detect_indentation": true,
"tab_size": 4,
"translate_tabs_to_spaces": false

This will set your editor to use tabs that are 4 spaces wide and will override the default behavior that causes Sublime to match the indention of whatever file you're editing. With these settings, re-indenting the file will cause any spaces to be replaced with tabs.

As far as automatically re-indenting when opening a file, that's not quite as easy (but probably isn't a great idea since whitespace changes wreak havoc on file diffs). What might be a better course of action: you can map a shortcut for re-indention and just trigger that when you open a new file that needs fixing.


At the bottom of the Sublime window, you'll see something representing your tab/space setting.

You'll then get a dropdown with a bunch of options. The options you care about are:

  • Convert Indentation to Spaces
  • Convert Indentation to Tabs

Apply your desired setting to the entire document.

Hope this helps.


In my case, this line solved the problem:

"translate_tabs_to_spaces": false

Here is how you to do it automatically on save: https://coderwall.com/p/zvyg7a/convert-tabs-to-spaces-on-file-save

Unfortunately the package is not working when you install it from the Package Manager.


if you have Mac just use help option (usually the last option on Mac's menu bar) then type: "tab indentation" and choose a tab indentation width

but generally, you can follow this path: view -> indentation


Here is a solution that will automatically convert to tabs whenever you open a file.

Create this file: .../Packages/User/on_file_load.py:

import sublime
import sublime_plugin

class OnFileLoadEventListener(sublime_plugin.EventListener):

    def on_load_async(self, view):
        view.run_command("unexpand_tabs")

NOTE. It causes the file to be in an unsaved state after opening it, even if no actual space-to-tab conversion took place... maybe some can help with a fix for that...


On the bottom right-hand corner of your Sublime Text window, you'll see an indentation indicator that looks a lot like this:

Indentation options menu

Clicking it will open a menu with options to adjust your indentation preferences, and more importantly, Convert Indentation to Tabs/Spaces.

enter image description here

The same menu is listed under View -> Indentation.


You can use the command palette to solve this issue.

Step 1: Ctrl + Shift + P (to activate the command palette)

Step 2: Type "Indentation", Choose "Indentation: Convert to Tabs"


Use the following command to get it solved :

autopep8 -i <filename>.py

You can do replace tabs with spaces in all project files by:

  1. Doing a Replace all Ctrl+Shif+F
  2. Set regex search ^\A(.*)$
  3. Set directory to Your dir
  4. Replace by \1

    enter image description here

  5. This will cause all project files to be opened, with their buffer marked as dirty. With this, you can now optionally enable these next Sublime Text settings, to trim all files trailing white space and ensure a new line at the end of every file.

    You can enabled these settings by going on the menu Preferences -> Settings and adding these contents to your settings file:

    1. "ensure_newline_at_eof_on_save": true,
    2. "trim_trailing_white_space_on_save": true,
  6. Open the Sublime Text console, by going on the menu View -> Show Console (Ctrl+`) and run the command: import threading; threading.Thread( args=(set(),), target=lambda counterset: [ (view.run_command( "expand_tabs", {"set_translate_tabs": True} ), print( "Processing {:>5} view of {:>5}, view id {} {}".format( len( counterset ) + 1, len( window.views() ), view.id(), ( "Finished converting!" if len( counterset ) > len( window.views() ) - 2 else "" ) ) ), counterset.add( len( counterset ) ) ) for view in window.views() ] ).start()
  7. Now, save all changed files by going to the menu File -> Save All

To automatically convert spaces to tabs on save, add the following Python script to a newly created subfolder called "UnexpandTabsOnSave" within "$SUBLIME_HOME$\Packages\":

import sublime, sublime_plugin, os

class ConvertSpacesToTabsOnSave( sublime_plugin.EventListener ):
  # Run Sublime's 'unexpand_tabs' command when saving any file
  def on_pre_save( self, view ):
    view.window().run_command( 'unexpand_tabs' )

Thank you for the initial resource.


Examples related to tabs

Visual Studio Code - Convert spaces to tabs Sublime Text 3, convert spaces to tabs Bootstrap 3: Keep selected tab on page refresh Change tab bar tint color on iOS 7 Stacked Tabs in Bootstrap 3 C++ printing spaces or tabs given a user input integer Open link in new tab or window How to make a new line or tab in <string> XML (eclipse/android)? Find Active Tab using jQuery and Twitter Bootstrap How to make the tab character 4 spaces instead of 8 spaces in nano?

Examples related to sublimetext

Sublime text 3. How to edit multiple lines? Why do Sublime Text 3 Themes not affect the sidebar? How to Install Sublime Text 3 using Homebrew 80-characters / right margin line in Sublime Text 3 Comparing the contents of two files in Sublime Text What is the default font of Sublime Text? Showing the same file in both columns of a Sublime Text window Sublime Text 3, convert spaces to tabs What is the difference between Sublime text and Github's Atom Sublime Text 2 multiple line edit

Examples related to sublimetext3

Sublime text 3. How to edit multiple lines? How to open remote files in sublime text 3 Eslint: How to disable "unexpected console statement" in Node.js? Why do Sublime Text 3 Themes not affect the sidebar? How to Install Sublime Text 3 using Homebrew How to change background and text colors in Sublime Text 3 80-characters / right margin line in Sublime Text 3 What is the default font of Sublime Text? Column/Vertical selection with Keyboard in SublimeText 3 How to compile and run C in sublime text 3?

Examples related to spaces

Visual Studio Code - Convert spaces to tabs Removing spaces from a variable input using PowerShell 4.0 Sublime Text 3, convert spaces to tabs Read input numbers separated by spaces How do I convert a list into a string with spaces in Python? How to copy directories with spaces in the name How can I convert tabs to spaces in every file of a directory? How can I convert spaces to tabs in Vim or Linux? Adding whitespace in Java Converting a sentence string to a string array of words in Java

Examples related to auto-indent

Brackets.io: Is there a way to auto indent / format <html> How to auto-indent code in the Atom editor? Sublime Text 3, convert spaces to tabs How to fix/convert space indentation in Sublime Text? How To Auto-Format / Indent XML/HTML in Notepad++ Difference between Ctrl+Shift+F and Ctrl+I in Eclipse Turning off auto indent when pasting text into vim Tab key == 4 spaces and auto-indent after curly braces in Vim