Reputation: 46247
I know the "File > Open folder..." dialog box in Sublime.
The problem is that:
How to open the current file's folder in the left "Folder view" of the current Sublime window, without any popup? (I would like to bind a keyboard shortcut for this). Note: I still use Sublime 2.
Upvotes: 0
Views: 333
Reputation: 22791
The menu item Project > Add Folder to project...
will prompt you for the name of a folder and then add it to the current window rather than create a new one. Contrary to the name, this will always work even if you're not explicitly using sublime-project
files directly.
For doing this without any sort of prompt, a plugin would be needed to adjust the list of folders that are open in the window currently.
In Sublime Text 3 and above, there is API support for directly modifying the list of folders that are open in the window, while Sublime Text 2 only has an API for querying the list of folders.
All versions of Sublime have a command line helper that can be used to interact with the running copy of Sublime (generally referred to as subl
), and one of the things it can do is augment the list of folders in the window by adding an additional one. In Sublime Text 2, the subl
helper is just the main Sublime Text executable itself.
The following is a plugin that can be used in Sublime Text 2 and above that will perform the appropriate action to get the path of the current file to open in the side bar. If you're unsure of how to use plugins, see this video on how to install them.
import sublime
import sublime_plugin
import os
# This needs to point to the "sublime_text" executable for your platform; if
# you have the location for this in your PATH, this can just be the name of the
# executable; otherwise it needs to be a fully qualified path to the
# executable.
_subl_path = "/home/tmartin/local/sublime_text_2_2221/sublime_text"
def run_subl(path):
"""
Run the configured Sublime Text executable, asking it to add the path that
is provided to the side bar of the current window.
This is only needed for Sublime Text 2; newer versions of Sublime Text have
an enhanced API that can adjust the project contents directly.
"""
import subprocess
# Hide the console window on Windows; otherwise it will flash a window
# while the task runs.
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen([_subl_path, "-a", path], startupinfo=startupinfo)
class AddFileFolderToSideBarCommand(sublime_plugin.WindowCommand):
"""
This command will add the path of the currently focused file in the window
to the side bar; the command will disable itself if the current file does
not have a name on disk, or if it's path is already open in the side bar.
"""
def run(self):
# Get the path to the current file and add it to the window
self.add_path(self.get_current_path())
def is_enabled(self):
"""
The command should only be enabled if the current file has a filename
on disk, and the path to that file isn't already in the list of folders
that are open in this window.
"""
path = self.get_current_path()
return path is not None and path not in self.window.folders()
def get_current_path(self):
"""
Gather the path of the file that is currently focused in the window;
will return None if the current file doesn't have a name on disk yet.
"""
if self.window.active_view().file_name() is not None:
return os.path.dirname(self.window.active_view().file_name())
return None
def add_path(self, path):
"""
Add the provided path to the side bar of this window; if this is a
version of Sublime Text 3 or beyond, this will directly adjust the
contents of the project data to include the path. On Sublime Text 2 it
is required to execute the Sublime executable to ask it to adjust the
window's folder list.
"""
if int(sublime.version()) >= 3000:
# Get the project data out of the window, and then the list of
# folders out of the project data; either could be missing if this
# is the first project data/folders in this window.
project_data = self.window.project_data() or {}
folders = project_data.get("folders", [])
# Add in a folder entry for the current file path and update the
# project information in the window; this will also update the
# project file on disk, if there is one.
folders.append({"path": path})
project_data["folders"] = folders
self.window.set_project_data(project_data)
else:
# Run the Sublime executable and ask it to add this file.
run_subl(path)
This will define a command named add_file_folder_to_side_bar
which will add the path of the current file to the side bar; the command disables itself if the current file doesn't have a name on disk, or if that path is already open in the side bar.
If you're using Sublime Text 2, note that you need to adjust the variable at the top to point to where your copy of Sublime is installed (including the name of the program itself, as seen in the example code), since the plugin will need to be able to invoke it to adjust the side bar.
In order to trigger the command, you can use a key binding such as:
{ "keys": ["ctrl+alt+a"], "command": "add_file_folder_to_side_bar"},
You can also create a file named Context.sublime-menu
in your User
package (the same place where you put the plugin) with the following contents to have a context menu item for this as well:
[
{ "caption": "-", "id": "file" },
{ "command": "add_file_folder_to_side_bar", "caption": "Add Folder to Side Bar",}
]
Upvotes: 3
Reputation: 46247
Solved for ST2 with @OdatNurd's idea:
class OpenthisfolderCommand(sublime_plugin.TextCommand):
def run(self, edit):
current_dir = os.path.dirname(self.view.file_name())
subprocess.Popen('"%s" -a "%s"' % ("c:\path\to\sublime_text.exe", current_dir))
Add the key binding with for example:
{ "keys": ["ctrl+shift+o"], "command": "openthisfolder"}
Upvotes: 0