Sublime Forum

[Question] How run Windows shell command into Sublime Text plugin?

#1

1. Briefly

I don’t understand, how I can run Windows shell command into Sublime Text plugin.

2. Expected behavior

I open file → Ctrl+Shift+PSimplyDelete → current file delete for me. I need to do this use del Windows command.

3. Did not help

My Default.sublime-commands file:

[{
    "caption": "SimplyDelete",
    "command": "simply_delete"
}]

My simplydelete.py file:

#!/usr/bin/env python
# coding: utf-8
import os
import sublime
import sublime_plugin


class SimplyDelete(sublime_plugin.ApplicationCommand):

    def simply_delete_current_file(self):
        command = "del /Q"
        file_path = self.view.file_name()
        os.system(command + file_path)

But it don’t work for me.

4. Do not offer

  1. Yes, I know about build systems, please, do not offer this.
  2. Yes, I know other methods to delete file. I want delete file exclusively use .py plugin and Windows del command.

Thanks.

0 Likes

#2

You can use subprocess to execute shell commands.  Use commas to separate commands, flags, and arguments in your command array, and subprocess will automatically handle whitespace & quoting.

import sublime, sublime_plugin
import subprocess

class simply_delete(sublime_plugin.TextCommand):
	def run(self, edit):
		filePath = self.view.file_name()
		if(filePath):
			command = ["del", "/q", filePath]
			subprocess.Popen(command, shell=True)
			self.view.close()

 

However, I’d recommend against using subprocess in this case since del will permanently delete your files.  A safer alternative is to use the send2trash library, so that you have the option to restore files from the recycle bin.

import sublime, sublime_plugin
from Default.send2trash import send2trash

class simply_delete(sublime_plugin.TextCommand):
	def run(self, edit):
		filePath = self.view.file_name()
		if(filePath):
			send2trash(filePath)
			self.view.close()
2 Likes

#3

There is a version of send2trash that ships with Sublime in the default package, which is used by DeleteFileCommand in Packages\Default\side_bar.py. If you use that one you don’t have to package any dependency with your package.

The caveats there being the packed version may or may not be different than what you might expect and/or I don’t know what version of Sublime it appeared in.

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
        for f in files:
            v = self.window.find_open_file(f)
            if v is not None and not v.close():
                return

            send2trash.send2trash(f)

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

#4

Nice find, updated my answer and removed the extra stuff.

0 Likes

#5

Sending to the trash is probably the best solution in this case, but if you want to delete a folder/file in python, you can use os.remove.

2 Likes