Sublime Text 3, convert spaces to tabs

前端 未结 11 2252
[愿得一人]
[愿得一人] 2020-12-07 08:07

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

相关标签:
11条回答
  • 2020-12-07 08:39

    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.

    0 讨论(0)
  • 2020-12-07 08:41

    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.

    The same menu is listed under View -> Indentation.

    0 讨论(0)
  • 2020-12-07 08:46

    In my case, this line solved the problem:

    "translate_tabs_to_spaces": false
    
    0 讨论(0)
  • 2020-12-07 08:46

    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.

    0 讨论(0)
  • 2020-12-07 08:47

    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

    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
    0 讨论(0)
  • 2020-12-07 08:54

    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...

    0 讨论(0)
提交回复
热议问题