Get path of folder on right click in sidebar by Sublime API - sublimetext2

How could I get path of folder on right click in sidebar by Sublime API? I can get path of file by sublime.view.file_name() but nothing for folders.

In a nutshell, create a Side Bar.sublime-menu file with command definitions:
[
{ "caption": "Side Bar Test Dirs", "command": "side_bar_test_dirs", "args": {"dirs": []} },
{ "caption": "Side Bar Test Paths", "command": "side_bar_test_paths", "args": {"paths": []} },
{ "caption": "Side Bar test Files", "command": "side_bar_test_files", "args": {"files": []} },
]
And then the commands:
import sublime
import sublime_plugin
class SideBarTestDirsCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
print(dirs)
class SideBarTestPathsCommand(sublime_plugin.WindowCommand):
def run(self, paths):
print(paths)
class SideBarTestFilesCommand(sublime_plugin.WindowCommand):
def run(self, files):
print(files)
There is more detailed documentation in the Sublime Text Unofficial Documentaion.

Related

In Sublime Text 3, how to have shortcuts for "Build and Run" and "Build only" separately like it was in Sublime Text 2?

In Sublime Text 3,when we press Ctrl+Shift+B, we are given the option to either do "Build and Run" or "only Build", whereas Ctrl+B executes the previously chosen operation among the two. But I want it to be like, it should directly build and run when I press Ctrl+Shift+B and only build when I press Ctrl+B like it was in Sublime Text 2. Can someone help me out?
Addings this to your sublime-keymap should result in the expected behavior:
{ "keys": ["ctrl+b"], "command": "build", "args": { "variant": "" } },
{ "keys": ["ctrl+shift+b"], "command": "build", "args": { "variant": "Run" } },
However you might want to remap keep the list of options to alt+b:
{ "keys": ["alt+b"], "command": "build", "args": { "select": true } },
Not answering the question but good to know, F7 functions the same as Ctrl+b.

Multiple commands with args under 1 hotkey in Sublime Text 3

I'm trying to configure ST3 keybindings to reindent whole text, save file and refresh the browser on ctrl+s. I'm using Chain of Command and Browser Refresh plugins, but the problem is I don't know how to pass commands with arguments, so that the reindent command would affect whole text instead of one line only. "single_line" : false seems to be ignored.
"keys": ["ctrl+3"],
"command": "chain",
"args": {
"commands": [
["reindent",{"context": "window", "args": {"single_line": false}}],
["browser_refresh"]
]
}
I've made it.
"keys": ["ctrl+s"],
"command": "chain",
"args": {
"commands": [
["reindent",{"single_line": false}],
["browser_refresh"]
]
}

Sublime Text 2 REPL run iPython file with a user Plug-in

I am trying to run an iPython file in an interactive shell in Sublime Text 2. This is my first plugin I've written.
Here's what I have so far:
import sublime, sublime_plugin
class IpydevCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('repl_open',{"type": "subprocess",
"encoding": "utf8",
"cmd": ["ipython", "-i", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "ipython",
})
self.window.run_command('move_to_group', { "group": 1 })
The issue here is that I don't get an interactive environment after. The view is all messed up. Any ideas how to fix this?
Thanks
Instead of running a notebook, you can directly call ipython using following configuration at the keymap bindings:-
[
{ "keys": ["f9"], "command": "repl_open",
"caption": "Python - IPython",
"id": "repl_python_ipython",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": {
"osx": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"linux": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"windows": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
},
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"
}
}
}
]
So, By clicking F9, it will open ipython on new shell

Is it possible to add root level menu of SublimeText?

I know we can configure SublimeText menu via sublime-menu files.
Is it possible to add a root level menu of SublimeText instead of subtree of the root menu?
The reason I ask is SublimeText is all about customize and configuration; and since I access to Sublime Text > Preferences so frequently, that I want the Preferences at not the subtree but the root level, where menu bar shows.
I use SublimeText3.
In ST2/3, configurations are JSON. Create a Main.sublime-menu file in your user directory. This contains a list of object entries. As an example,
[
{
"caption": "Custom",
"mnemonic": "m",
"id": "custom"
}
]
This will create a "Custom" menu (at the top level). Of course, there aren't any sub menu entries so it's a little less useful. Below is a portion of the Menu file from one of my plugins (just to show how to specify a command). I listed under the "Custom" menu as an example.
[
{
"caption": "Custom",
"mnemonic": "m",
"id": "custom",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "AdvancedNewFile",
"children":
[
{
"command": "open_file",
"args": {"file": "${packages}/AdvancedNewFile/README.md"},
"caption": "README"
}
]
}
]
}
]
}
]
I did this in ST3 on W7. I'm unsure how it will behave in other OS's, but I assume it would work.

Adding Custom Menus in Sublime Text

How to add a custom Menu item in SublimeText 2 .
Any Ideas ??
I see there is a Main.sublime-menu file but dont know how to edit it.
Thanks!
The *.sublime-menu file is simply JSON. You can create a Main.sublime-menu in your user directory and it will be merged with other menu entries. It may be beneficial to look through the Main.sublime-menu files third party plugins have. These are generally much shorter, so may be easier to understand some of the things you need to define in each entry.
edit
You can use the following as a plugin to open notepad with an arbitrary file.
import sublime
import sublime_plugin
import subprocess
import threading
class OpenNotepadCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
th = NotepadThread(filename)
th.start()
class NotepadThread(threading.Thread):
def __init__(self, filename=None):
self.filename = filename
threading.Thread.__init__(self)
def run(self):
if self.filename is not None:
subprocess.call("notepad.exe %s" % self.filename)
else:
subprocess.call("notepad.exe")
When you are creating a menu item use something like the following for the command and arguments.
{
"command": "open_notepad",
"args": { "filename": "<the absolute path here>"}
}
Easier option if what you want is just run a command. Create a file Context.sublime-menu inside your Packages/User directory, and add the following:
[
{ "caption": "<Your caption here>", "command": "exec", "args": {"cmd": ["<your cmd name>", "<arg1>", "<arg2>", <...>]} }
]
Exemple: Adding a menu item to the context menu that just run dir:
[
{ "caption": "List files in current dir", "command": "exec", "args": {"cmd": ["dir"]} }
]
I know this way too late to join the party and add my 2 cents. Anyway, Main.sublime-menu is a file that allows you to add menu items to the top menu i.e [File, Edit, Selection, Find, View, Goto, etc.]
I recently added a new section "Dev" just to figure it out. I also wanted a way to trigger browser previews for a specific browser. Check it out.
[
{
"caption": "Dev",
"mnemonic": "Z",
"id": "dev",
"children": [
{
"caption" : "Previews",
"children": [
{ "caption": "Markdown Live Preview", "command": "new_markdown_live_preview", "id": "markdown_live_preview" },
{ "caption": "Preview in Default Browser", "command": "view_in_browser", "id": "markdown_live_preview" },
{ "caption": "Preview in Firefox", "command": "view_in_browser", "args": { "browser": "firefox" }, "id": "markdown_live_preview" },
{ "caption": "Preview in Chrome","command": "view_in_browser", "args": { "browser": "chrome" }, "id": "markdown_live_preview" },
{ "caption": "Preview in Safari", "command": "view_in_browser", "args": { "browser": "safari" }, "id": "markdown_live_preview" },
]
},
]
}
]
Anyway, this still works in ST3. Just in case anyone stumbles around here.