Adding Custom Menus in Sublime Text - sublimetext2

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.

Related

Modifying settings.json in vscode to add shell escape flag to pdflatex in latex workshop

I'm new to VSCode and I am setting up LaTeX. I am trying to compile a .tex document that uses minted and thus pdflatex needs the --shell-escape flag. I am trying to modify the settings.json to do this.
I have tried adding the following (found on the internet)
{
"latex-workshop.latex.tools": {
"name": "pdflatex",
"command": "pdflatex",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
}
}
However it comes up with an error:
Incorrect type. Expected "array".
This wont even let me try to build with latex workshop. Help would be much appreciated.
I had the same problem and after looking like crazy on the internet I found the solution.
The snippet that you have there is wrongly formatted that's why is complaining.
And is only for the pdf latex recipe.
Here is the snippet that needs to be added to settings.json.
{
...,
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"--shell-escape",
"-pdf",
"%DOC%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
}
],
...
}
I found the solution here in a question related to the pygmentize package

How do I display my HTML/CSS files on a browser via Visual Studio Code on Mac?

I HAVE looked around to find the solution and some fellows already expressed the command:
{
"version": "0.1.0",
"command": "Chrome",
"osx": {
"command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
"args": [
"${file}"
]
}
after you press Cmd+Shift+p and entering Configure Task Runner
This thing is, I don't see that option, thus preventing me from completing this step. Perhaps I am ignorant as to how to work in this still.
Try specifiying a "taskName": attribute in your build task. i.e.
{
"version": "0.1.0",
"taskName": "MyName",
"command": "Chrome",
"osx": {
"command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
"args": [
"${file}"
]
}
Download this: https://marketplace.visualstudio.com/items?itemName=peakchen90.open-html-in-browser. It is written in an asian language but it is very simple to handle, when your html file is opened, save it, right click and click the asian words. It will open in your default browser.

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"]
]
}

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.

In Sublime Text 2 - reopen build output

In Sublimt Text 2, when I use the build system (make) to run tests, the output is displayed in the build output pane.
However, if I press escape to close the output pane (e.g. to make a fix), I can't find a way to redisplay the output pane to see what else was borked. Have tried to create a custom keybinding to execute show_panel "output", but can't get it working.
Meep?
The menu shortcut is under Tools -> Build Results -> Show Build Results.
I wish this was under the View menu like all the rest of view options...
As you can see in Packages/Default/Main.sublime-menu the command for "Show build results" is this:
{
"command": "show_panel",
"args": {
"panel": "output.exec"
},
"caption": "Show Build Results",
"mnemonic": "S"
},
so a custom key binding could be this:
{
"keys": ["ctrl+alt+super+r"],
"command": "show_panel",
"args": {
"panel": "output.exec"
}
}
And the key binding to hide the panel:
{
"keys": ["ctrl+shift+2"],
"command": "hide_panel",
"args": {
"panel": "output.exec"
}
},
Building on akirk's answer you can make it toggle the build results panel by copying some of the syntax used for the escape shortcuts.
Adding the following lines to the user key bindings will do part of the trick. As reported by some of the previous answers the hide_panel command will hide any panel, and pressing it a second time will reveal build_results.
{
"keys": ["alt+b"], "command": "show_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["alt+b"], "command": "hide_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": true }
]
},