我知道有很多关于这个的帖子,但我不能让它工作。 我使用标签来编码。有没有办法,把空格转换成制表符?即打开和保存文件?有人知道吗?

/ /编辑: 我的愿望是自动做到这一点!->打开,保存或在飞行 有人知道怎么做吗?

我试了一下:

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

下面是我的用户设置:

{
    "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
}

当前回答

对我来说,这句话解决了问题:

"translate_tabs_to_spaces": false

其他回答

使用下面的命令来解决它:

autopep8 -i <filename>.py

这里有一个解决方案,将自动转换为标签每当你打开一个文件。

/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")

请注意。它导致文件在打开后处于未保存状态,即使没有发生实际的空格到制表符的转换…也许有人可以帮忙解决这个问题……

要在保存时自动将空格转换为制表符,请将以下Python脚本添加到“$SUBLIME_HOME$\Packages\”中新创建的名为“UnexpandTabsOnSave”的子文件夹中:

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

谢谢你最初的资源。

下面是如何在保存时自动完成的: https://coderwall.com/p/zvyg7a/convert-tabs-to-spaces-on-file-save

不幸的是,当您从包管理器安装它时,包不工作。

对我来说,这句话解决了问题:

"translate_tabs_to_spaces": false