Sublime Text 3: confirm to delete file

When I choose delete by right clicking on a file in the SideBar, I get a confirmation.
Maybe it's SideBarEnhancements. It is worth a try.


While sergioFC's answers work great, I'm bit worried of modifying Default.sublime-package, as it might someday get overwritten when Sublime is updated, so the fix would need to be manually re-applied after each such update. SideBarEnhancements, on the other hand, might have too many features for someone who only wants the confirmation when deleting a file.

Alternatively, you can add a simple confirmation dialog that should be more resistant to ST updates, by creating a file (plugin). On Linux it should be somewhere around ~/.config/sublime-text-3/Packages/User/confirm_delete.py, and if you're on Windows/Mac or this path does not work for you, you can simply choose from the top menu: Tools -> Developer -> New Plugin and later save as confirm_delete.py - thanks to harrrrrrry for this suggestion. Code to put in:

from Default.side_bar import *

class DeleteFileCommand(sublime_plugin.WindowCommand):
    def run(self, files):
        if len(files) == 1:
            message = "Delete File %s?" % files[0]
        else:
            message = "Delete %d Files?" % len(files)

        if sublime.ok_cancel_dialog(message, "Delete") != True:
            return

        # Import send2trash on demand, to avoid initialising ctypes for as long as possible
        import Default.send2trash as send2trash
        for f in files:
            v = self.window.find_open_file(f)
            if v != None and not v.close():
                return

            send2trash.send2trash(f)

    def is_visible(self, files):
        return len(files) > 0

This code is basically a copy of DeleteFileCommand function from Default.sublime-package's side_bar.py combined with confirmation dialogs from DeleteFolderCommand from the same file, as Sublime has such dialog natively for folder removal.


Important: take a look at iron77 answer. It says that if you modify Default.sublime-package (options 1 and 3) this changes might be overriden if sublime text is updated.

Option 1: modify side_bar.py file

You can use sublime API to show an ok/cancel dialog. The code you are looking for is in a file called side_bar.py. This file is located inside the zip file Default.sublime-package. In windows this is usually located in C:\Program Files\Sublime Text 3\Packages\Default.sublime-package and can be explored using programs such as WinRar.

Inside that file locate DeleteFileCommand and add this 3 new lines, so it is changed from this:

class DeleteFileCommand(sublime_plugin.WindowCommand):
    def run(self, files):
        # Import send2trash on demand, to avoid initialising ctypes for as long as possible
        import Default.send2trash as send2trash

To this

class DeleteFileCommand(sublime_plugin.WindowCommand):
    def run(self, files):
        isSure = sublime.ok_cancel_dialog('Are you sure you want to delete the file?')
        if isSure != True:
            return
        # Import send2trash on demand, to avoid initialising ctypes for as long as possible
        import Default.send2trash as send2trash

We are showing a ok/cancel dialog and if the user doesn't press Ok then we return and the file isn't removed.

Notes:

  • You will have to add the same code in class DeleteFolderCommand in order to confirm also when deleting folders.
  • Is a good practice to backup your Default.sublime-package file first just in case something goes wrong. EDIT: use a different folder for the backup or the package could be loaded twice causing problems as the OP has said in his comment.
  • As this is python code indentation is extremly important, don't replace any spaces for tabs nor add any extra space or it will not work (you can see it console).

Result:

Sublime-confirm file deletion

Option 2: use an existing package

As user leesei said in his answer you can use SideBarEnhancements package to achieve your goal. This package adds many other features to the file context menu as you can see in the following image, but it is a very good choice as you only need to install an exsiting package.

SideBarEnhancements package

Option 3: remove option from context menu

Edit Side Bar.sublime-menu inside Default.sublime-package (see option 1) and remove this line (and if you want remove also the line reffering to deleting folders):

{ "caption": "Delete File", "command": "delete_file", "args": {"files": []} },

WTF a software that doesn't have a confirm dialog before delete. I can't believe this. Sad but true. Just stupid software!

Tags:

Sublimetext3