Search code examples
sublimetext3

Sublime Text 3: confirm to delete file


Is there a way to confirm deleting a file from the tree (left hand side) or remove the option from the context menu?

It is too easy to miss i.e. rename and click delete file instead. Then the file is gone.

I googled and found it should be moved to the trash folder but either that doesn't apply to Win7 or to using network drives. As a result the files are actually deleted or moved somewhere I have failed to track them down so far.

Using Sublime Text (build 3083)


Solution

  • 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": []} },