<p>这看起来是因为工具的世界观(在本例中是Sublime和Jupyter)在标签页和空间方面的不匹配,这是一个长期存在的争议。在</p>
<p>特别是在Sublime中,您关闭了<code>translate_tabs_to_spaces</code>,因为您想使用物理制表符字符。因此,在Sublime中按Tab</kbd>将插入物理制表符,这些字符通过<code>tab_size</code>设置可视化。在</p>
<p>另一方面,根据这个问题,<a href="https://github.com/ipython/ipython/issues/10423" rel="nofollow noreferrer">Jupyter does not support indentation via tabs</a>当您按下<kbd>选项卡</kbd>键时。在Jupyter中,尽管您的意图很好,但当您按Tab键时,它将插入空格。但是,如果物理标签被粘贴进来,它确实支持这些标签,这就是为什么它会出现在上面,以及为什么你必须手动粘贴物理标签来获得你需要的输入。在</p>
<p>在这两个工具中,Sublime允许您选择使用物理制表符还是空格字符,但Jupyter不使用,而且总是需要空格。在这种情况下,最方便的解决方案是也不要在Sublime中使用制表符,然后一切都会匹配起来。在</p>
<p>大概这不是你的选择,所以下一个最好的解决方案就是让Sublime以它期望的方式向Jupyter提供数据,这可以通过一个简单的插件来实现。在</p>
<p>在Sublime中,选择<code>Tools > Developer > New Plugin...</code>并将存根替换为以下代码,然后将其保存在Sublime将提示您的默认位置(您的<code>User</code>软件包),并使用一个可识别的名称,如<code>copy_with_spaces.py</code>:</p>
<pre class="lang-python prettyprint-override"><code>import sublime
import sublime_plugin
class CopyWithSpacesCommand(sublime_plugin.TextCommand):
"""
Copy the selected text to the clipboard, replacing all tab characters with
spaces based on the tab size in the current view.
"""
def run(self, edit):
self.view.run_command("copy")
tab_size = self.view.settings().get("tab_size", 4)
text = sublime.get_clipboard().expandtabs(tab_size)
sublime.set_clipboard(text)
</code></pre>
<p>这实现了一个新的命令<code>copy_with_spaces</code>,它将执行一个复制,但在传递过程中会修改数据,以便将任何物理制表符替换为适当数量的空白字符。在</p>
<p>有了它,您就可以添加一个自定义键绑定,以便在将代码从Sublime复制到Jupyter时使用,以简化工作。例如:</p>
^{pr2}$