sublimetext3 - 关闭选项卡 > 转到上一个编辑过的选项卡

标签 sublimetext3 sublimetext

在 sublimetext3 中关闭选项卡时,它总是将我带回左侧选项卡,而在 sublimetext2 上,我会被带到之前打开的选项卡(不一定是左侧选项卡)。

这种行为在 sublimetext2 中非常方便,因为它创建了一种只需连续关闭选项卡即可轻松返回的历史记录。

sublimetext3中有这样的设置吗?

重现步骤

  1. 我打开了 3 个选项卡,第 3 个选项卡处于事件状态: enter image description here
  2. 我现在去编辑第二个: enter image description here
  3. 我已经完成并决定关闭第二个选项卡: enter image description here

失败:我没有回到之前编辑的那个:第三个

最佳答案

没有设置可以做到这一点。然而,由于它可以通过插件来完成,并且我喜欢保持我的插件编写技能达到最佳水平,所以我已经为您编写了它。

它被称为 FocusMostRecentTabClos​​er ,代码在下面和 GitHub Gist 中。 .

要使用它,请将其保存为 Packages 目录结构中的 FocusMostRecentTabClos​​er.py 并将键分配给 focus_most_recent_tab_closer 命令。例如

{"keys": ["ctrl+k", "ctrl+w"], "command": "focus_most_recent_tab_closer"},

我还没有对其进行广泛的测试,它需要 Sublime Text 3 build 3024 或更高版本(但现在已经很旧了)。

如果有任何错误,请回复评论,我会看看我能做什么。

# MIT License

import sublime
import sublime_plugin
import time

LAST_FOCUS_TIME_KEY = "focus_most_recent_tab_closer_last_focused_time"

class FocusMostRecentTabCloserCommand(sublime_plugin.TextCommand):
    """ Closes the focused view and focuses the next most recent. """

    def run(self, edit):

        most_recent = []
        target_view = None
        window = self.view.window()

        if not window.views():
            return

        for view in window.views():
            if view.settings().get(LAST_FOCUS_TIME_KEY):
                most_recent.append(view)

        most_recent.sort(key=lambda x: x.settings().get(LAST_FOCUS_TIME_KEY))
        most_recent.reverse()

        # Target the most recent but one view - the most recent view
        # is the one that is currently focused and about to be closed.

        if len(most_recent) > 1:
            target_view = most_recent[1]

        # Switch focus to the target view, this must be done before
        # close() is called or close() will shift focus to the left
        # automatically and that buffer will be activated and muck
        # up the most recently focused times.

        if target_view:
            window.focus_view(target_view)

        self.view.close()

        # If closing a view which requires a save prompt, the close()
        # call above will automatically focus the view which requires
        # the save prompt. The code below makes sure that the correct
        # view gets focused after the save prompt closes.

        if target_view and window.active_view().id() != target_view.id():
            window.focus_view(target_view)

class FocusMostRecentTabCloserListener(sublime_plugin.EventListener):
    def on_activated(self, view):
        """ Stores the time the view is focused in the view's settings. """
        view.settings().set(LAST_FOCUS_TIME_KEY, time.time())

关于sublimetext3 - 关闭选项卡 > 转到上一个编辑过的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481136/

相关文章:

python - api : how to get selected text from object sublime. 选择

python - 有没有办法将 KeyboardInterrupt 发送到 Sublime Text 中正在运行的 Python 脚本?

settings - 此设置在Sublime Text 3中有什么作用?

plugins - Sublime Text SFTP 插件中文件夹和子文件夹的忽略正则表达式

sublimetext3 - Sublime Text 3 片段,以日期和时间作为自定义变量

正则表达式用数字替换十进制和整数字符串

regex - 在Sublime Text 2中查找和批量编辑HTML元素的内容

plugins - 在 Sublime Text 中显示事件插件并禁用它们

editor - Sublime Text 3 自动换行(设置为默认)不再工作

python - 在 Sublime 中设置插入符位置