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 S
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.
On the bottom right-hand corner of your Sublime Text window, you'll see an indentation indicator that looks a lot like this:
Clicking it will open a menu with options to adjust your indentation preferences, and more importantly, Convert Indentation to Tabs/Spaces
.
The same menu is listed under View -> Indentation
.
In my case, this line solved the problem:
"translate_tabs_to_spaces": false
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.
You can do replace tabs with spaces in all project files by:
Ctrl+Shif+F
^\A(.*)$
Your dir
Replace by \1
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:
"ensure_newline_at_eof_on_save": true,
"trim_trailing_white_space_on_save": true,
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()
File -> Save All
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...