Sublime Forum

Open Command Window Here

#1

Does anyone know if anything been added to the API that allows a plugin to grab the current path triggered from a sidebar folder right click context menu entry?

I’d like to be able to right click a folder and “Open Command Window Here” type thing.

0 Likes

#2

Windows only :smile:

Make a new folder in your plugin directory, create a new file called opencommand.py and put this code in it:

[code]import sublime, sublime_plugin
import subprocess

class OpenPromptCommand(sublime_plugin.TextCommand):
def run(self, edit):
dire = os.path.dirname(self.view.file_name())
retcode = subprocess.Popen(“cmd”, “/K”, “cd”, dire])

def is_enabled(self):
    return self.view.file_name() and len(self.view.file_name()) > 0[/code]

Now create another file called “Content.sublime-menu” and put the following in it:

{ "caption": "-", "id": "file" }, { "command": "open_prompt", "caption": "Open Command Window Here…" } ]
Now when you right click and click “Open Command Window Here…” you’ll get a new command prompt at the location of the file you were editing.

0 Likes

#3

Oh, sorry, I see you wanted this from the sidebar… Just change “Context” to “Sidebar” in the filename.

0 Likes

#4

Saved me some time with this. Thanks!

0 Likes

#5

Hmm, I can’t seem to get the Popen command to work on my setup (XP64, dev2122). The item shows up in the context menu enabled, but doesn’t seem to act in any way.

I’ve dickered with the Popen command, but I admit little facility with Python libraries. Any ideas what the call might be fragile on?

0 Likes

#6

Yup. Try adding the import os.

import sublime, sublime_plugin, subprocess, os

The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.

0 Likes

#7

[quote=“atomi”]The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.[/quote]

I see…

Well the sidebar can pass arguments through I believe, so I’ll have a play :smile:

0 Likes

#8

Ah ha, yes, there it is. Thanks, works like a charm now!

0 Likes

#9

[quote=“jbrooksuk”]

[quote=“atomi”]The problem with loading this from the sidebar is that the working directory is set using self.view.file_name().
What I wanted was a way to use the folder I right clicked on as the current working directory for the command prompt.[/quote]

I see…

Well the sidebar can pass arguments through I believe, so I’ll have a play :smile:[/quote]

Yeah that was what was hanging me up.

I see

{ "caption": "Delete Folder", "command": "delete_folder", "args": {"dirs": ]} },

in the Default\Side Bar.sublime-menu but when I add “args” to my Side Bar entry I get this in the console:

Traceback (most recent call last):
  File ".\sublime_plugin.py", line 276, in run_
TypeError: run() got an unexpected keyword argument 'dirs'
0 Likes

#10

Sublime Terminal does what you are looking for, and works cross-platform too: wbond.net/sublime_packages/terminal

You can right click on any folder, and also use ctrl+shift+t to open a terminal/command prompt at the folder of the current file, or ctrl+shift+alt+t to open at the project folder the current file is in.

0 Likes

#11

this works for me, thanks everyone.

open_prompt.py

import sublime, sublime_plugin, subprocess, os

class OpenPromptCommand(sublime_plugin.TextCommand):
    def run(self, edit, paths=]):
        retcode = subprocess.Popen("cmd", "/K", "cd", paths[0]])

Side Bar.sublime-menu


    { "caption": "-", "id": "end" },
    { "caption": "Open Command Window Here", "command": "open_prompt", "args": {"paths": ]} }
]
0 Likes