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

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

我试了一下:

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
}

在你的Sublime文本窗口的右下角,你会看到一个缩进指示器,看起来很像这样:

点击它将打开一个菜单,其中有选项来调整缩进偏好,更重要的是,将缩进转换为制表符/空格。

视图->缩进下列出了相同的菜单。


您可能已经知道,您可以在首选项中自定义缩进设置。崇高设定,例如:

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

这将设置您的编辑器使用4个空格宽的选项卡,并将覆盖导致Sublime匹配您正在编辑的任何文件的缩进的默认行为。使用这些设置,重新缩进文件将导致所有空格替换为制表符。

至于在打开文件时自动重新缩进,这并不那么容易(但可能不是一个好主意,因为空格更改会对文件差异造成严重破坏)。更好的做法可能是:可以映射一个用于重新缩进的快捷方式,并在打开需要修复的新文件时触发它。


在Sublime窗口的底部,你会看到一些代表你的标签/空格设置的东西。

然后你会看到一个带有一堆选项的下拉列表。你关心的选项有:

将缩进转换为空格 将缩进转换为制表符

将所需的设置应用于整个文档。


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

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


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

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

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


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

"translate_tabs_to_spaces": false

要在保存时自动将空格转换为制表符,请将以下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' )

谢谢你最初的资源。


您可以使用命令面板解决此问题。

第一步:Ctrl + Shift + P(激活命令面板)

第二步:输入“缩进”,选择“缩进:转换为制表符”


你可以用空格替换所有项目文件中的制表符:

Doing a Replace all Ctrl+Shif+F Set regex search ^\A(.*)$ Set directory to 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, 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() Now, save all changed files by going to the menu File -> Save All


如果你有Mac,就使用帮助选项(通常是Mac菜单栏的最后一个选项) 然后键入:"制表符缩进"并选择制表符缩进宽度

但一般来说,你可以遵循这条路径: View ->缩进


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

autopep8 -i <filename>.py