Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 3: confirm to delete file

Tags:

sublimetext3

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)

like image 943
robbash Avatar asked Jun 16 '15 22:06

robbash


People also ask

How do I delete a file in Sublime Text 3?

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:

How do I show the OK/Cancel dialog in sublime?

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.

How do I revert Sublime Text to a previous installation?

Sublime Text can be reverted to a freshly installed state by removing your data directory. Depending on your operating system, this directory is located in: If Sublime Text 3 was installed previously, the data directory may include 3 at the end of the name:

Where can I find the side_bar code in Sublime Text?

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.


2 Answers

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": []} },
like image 107
sergioFC Avatar answered Oct 19 '22 11:10

sergioFC


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.

like image 27
iron77 Avatar answered Oct 19 '22 12:10

iron77