在 Sublime Text 中禁用文件关闭警告对话框

neh*_*hem 4 sublime-text-2 sublime-text-3

我主要使用 ST 作为我的临时记事本。现在,当我按下 Ctrl+W 时,它会生成这个对话框

在此处输入图片说明

然而,99% 的时间我的意图是“关闭而不保存”。有没有办法禁用这个弹出窗口?因为实际上,ST 会自动保存我的文件。

r-s*_*ein 6

是的,您只需编写一个插件即可将视图设置为草稿并关闭它。然后为该命令创建一个键绑定。选择Tools > Developer > New Plugin...并粘贴:

import sublime_plugin


class CloseWithoutSavingCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        view.set_scratch(True)
        view.close()
Run Code Online (Sandbox Code Playgroud)

然后创建一个键绑定来覆盖 ctrl+w

{
    "keys": ["ctrl+w"],
    "command": "close_without_saving",
},
Run Code Online (Sandbox Code Playgroud)

附注。正如@Dreamcat4 在评论中提到的,将此键绑定限制为临时缓冲区也可能相关。在这种情况下,您可以轻松创建一个上下文侦听器,它只启用该文件类型的键绑定:

import operator as opi

import sublime
import sublime_plugin

class CloseWithoutSavingCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        view.set_scratch(True)
        view.close()

class IsRealFileListener(sublime_plugin.EventListener):
    def on_query_context(self, view, key, operator, operand, match_all):
        # only act with the correct context key
        if key != 'user.is_real_file':
            return

        if operator == sublime.OP_EQUAL:
            op = opi.eq
        elif operator == sublime.OP_NOT_EQUAL:
            op = opi.ne
        else:
            # operator not supported
            return

        # assumption: a real file is a buffer, which has a filename as target
        is_real_file = bool(view.file_name())
        return op(is_real_file, operand)
Run Code Online (Sandbox Code Playgroud)

创建一个键绑定来覆盖 ctrl+w,但仅限于上下文

{
    "keys": ["ctrl+w"],
    "command": "close_without_saving",
    "context": [
        { "key": "user.is_real_file", "operator": "equal", "operand": false }
    ],
},
Run Code Online (Sandbox Code Playgroud)