Hotkey for running command - sublimetext3

I want to create a keyboard shortcut in Sublime Text 3.
The command I want to run is something like this:
[make.sublime-build]
{
"name": "boot-dev-svr",
"cmd": ["java","-jar","D:\\prg\\boot\\boot-1.1.1.jar","development"],
"working_dir": "D:/smx",
"path": "C:/Windows/System32",
}
The key I want to map this action to is F5:
[Default (Windows).sublime-keymap - User]
[
{ "keys": ["f5"], "command": "BLAH BLAH BLAH" },
]
Seems like it should be simple, but "cmd" and "command" seem to be fundamentally different. I have not managed to get it to work.
How do I put these things together to do what I want in Sublime Text 3?

Have you tried putting the actual command in []
That worked for me.
Another solution would be to update Sublime Text 3 to the latest release, sometimes that fixes the bug.
Hope I helped!
EDIT
Try removing the comma from the end of make.subime-build, like this
{
"name": "boot-dev-svr",
"cmd": ["java","-jar","D:\\prg\\boot\\boot-1.1.1.jar","development"],
"working_dir": "D:/smx",
"path": "C:/Windows/System32",
}

This is what I wound up doing, which seems to work, more or less.
Build:
{
"cmd": ["python","prj.py","--runmake","serve"],
"working_dir": "D:/smx",
"path": "C:/Python27",
"variants":
[
{
"name": "boot-dev-svr",
"cmd": ["java","-jar","D:\\prg\\boot\\boot-1.1.1.jar","development"],
"working_dir": "D:/smx",
"path": "C:/Windows/System32",
},
]
}
Keymap:
[
{ "keys": ["f5"], "command": "build", "args": {"variant": "boot-dev-svr"} },
]

Related

Can't run py files by using hot key in sublimetext3

I have update my sublime3 to newest version, then I can't use hotkey to run my py files. Here is my hot key configuration:
[
{
"caption": "Tmpl: Create python",
"command": "sublime_tmpl",
"keys": ["ctrl+alt+p"], "args": {"type": "python"}
},
{
"keys":["f1"],
"caption": "SublimeREPL: Python",
"command": "run_existing_window_command", "args":
{
"id": "repl_python",
"file": "config/Python/Main.sublime-menu"
}
},
{
"keys":["f2"],
"caption": "SublimeREPL: Python - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
},
]
When I press F2 or F1, there is nothing happened.
It appears that Sublime Text 3 build 3200 has broken the run_existing_window_command command. As alluded to in this answer the solution is to instead invoke the command that the SublimeREPL menu item is using.
If you navigate to your Sublime Text 3 packages directory (AppData/Roaming/Sublime Text 3/ by default on Windows), you can see the packages you have installed. From this directory, if you open SublimeREPL/config/Python/Main.sublime-menu, you'll see a big ole json file that looks something like this:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "R",
"id": "SublimeREPL",
"children":
[
{"caption": "Python",
"id": "Python",
"children":[
{"command": "repl_open",
"caption": "Python",
"id": "repl_python",
"mnemonic": "P",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
<---snip--->
]}
]
}]
}
]
Notice that the innermost children key is a list of dictionaries with commands and args. We're going to copy those into the sublime-keymap file and replace the command and args that are already there. For example, your shortcut to open a python REPL would now look like:
"keys": ["f1"],
"command": "repl_open",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
Hitting F1 should now work like it used to.

keybinding build_with with cmd in sublime

I want to call a specific build command stored in an external plugin of Sublime. The .sublime.build looks like this.
{
"selector": "text.html.markdown.knitr",
"working_dir": "${project_path:${folder}}",
"env": { "LANG": "en_US.UTF-8" },
"cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file$
"shell": true,
"variants":
[
{
"name": "Run",
"working_dir": "$file_path",
"shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
}
]
}
It uses cmd to simply call an external command. I would like to create a keybinding that automatically selects the "Run" variant of the .sublime.build. I tried with the following code:
{ "keys": ["ctrl+shift+b"], "command": "build", "args": {"build_system": "/Packages/knitr/knitr-Markdown.sublime-build", "variant": "Run" }},
Unfortunately, the shell returns
shell_cmd or cmd is required
[cmd: None]
[dir: /Users/serg/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/serg/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/MacGPG2/bin:/opt/X11/bin]
[Finished]
Any help is appreciated
First of all, your current build file is invalid, the JSON itself is not valid. I'm not sure if this is a copy & paste error. If not, use a JSON Validator to fix the syntax.
Next, you need to provide the command in the correct syntax. cmd is expecting the command as an array. Since your cmd is incomplete, I'll provide a different example.
Wrong syntax
"cmd": ["compiler --arg source.c"]
Correct syntax
"cmd": ["compiler, "--arg", "source.c"]
For reference, here's one of the build files from the R-IDE package:
{
"selector": "text.html.markdown.rmarkdown",
"working_dir": "$file_path",
"cmd": [
"Rscript", "-e",
"rmarkdown::render('$file_name', encoding = 'UTF-8')"
],
"osx":{
"path": "/Library/TeX/texbin:/usr/local/bin:$PATH"
}
}

How do I keybind SublimeREPL to run Haskell interpeter?

I've been searching for the right command name to keybind to run Haskell Interpeter in Sublime 3. I'm trying to add to the Sublime key bindings - user and the code should look something like this:
{ "keys": ["alt+keypad2"], "command": "run_existing_window_command", "args":
{
"id": "repl_haskell_run",
"file": "config/Python/Main.sublime-menu"
}
},
The following line is wrong, I made it up and I need to find the right command to put there:
"id": "repl_haskell_run",
"file": "config/Python/Main.sublime-menu"
I think the following does what you are looking for. I found the command by looking for a .sublime-menu file in the Haskell folder of the SublimeREPL repo: config/Haskell/Default.sublime-commands. (One such file had to exist for you to be able to do ctrl + shift + p -> SublimeREPL: Haskell.)
[
{ "keys": ["alt+keypad2"],
"command": "run_existing_window_command",
"args": { "id": "repl_haskell", "file": "config/Haskell/Main.sublime-menu" } },
]

How to set key-bindings in Sublime that change values of settings?

I'm looking to bind the "alt+f11" binding to toggle "draw_centered", which centers the text as in Distraction-free mode. I'm not sure how to get it to work though.
Heres my keybinds so far:
//if draw_centered == true, set to false
{ "keys": ["alt+f11"], "command": "set_setting", "args":
{
"setting": "draw_centered",
"value": "true",
},
"context":
[
{ "key": "setting.draw_centered", "operator": "equal", "operand": false}
]
},
//if draw_centered == false, set to true
{ "keys": ["alt+f11"], "command": "set_setting", "args":
{
"setting": "draw_centered",
"value": "false",
},
"context":
[
{ "key": "setting.draw_centered", "operator": "equal", "operand": true}
]
}
I couldn't find a command that automatically toggled "draw_centered", so I had to resort to building some sort of advanced command. I'm having a bit of trouble understanding the documentation on keybindings, but I tried to follow the "Contexts" example. Could anyone point to what I'm doing wrong?
Thanks sergioFC for the tip about toggle_setting! I got it to work with this code:
{ "keys": ["alt+f11"], "command": "toggle_setting", "args":
{
"setting": "draw_centered",
}
}
EDIT: I discovered a bug with this. After using the key-combination "alt-f11" now, the distraction-free mode isn't behaving like it should. It now follows the draw_centered state that I am in when I switch from normal to distraction-free mode.
For example: if I have a file opened and click 'alt-f11' so I am left-aligned (i.e. draw_centered = false) , the window will remain left-aligned when I enter distraction-free mode. Any ideas as to why this is and how to fix it?
A little late, but it took me a while to find out how to do something similar and this was the most closest question to what I was trying to achieve.
toggle_setting only works in the current view(file you are working on), it also won't work in other options like show_encoding, because these aren't too related to the view specifically rather than being more part of the panel spectrum.
After digging a couple of hours I found an old plugin called Cycle Settings, this one was for Sublime 2, but after a little tweaking it worked as expected.
(I remember that there was an option to create packages directly in sublime, but don't remember where..)
Go to Preferences/Browse Packages... and create a new file there
"Cycle Settings/cycle_setting.py" and the following code there:
"""
Cycle Setting plugin for Sublime Text 3.
Copyright 2011 Jesse McCarthy <http://jessemccarthy.net/>
Adds a command that can be called from a key binding to toggle or
cycle through values for a setting.
The Software may be used under the MIT (aka X11) license or Simplified
BSD (aka FreeBSD) license. See LICENSE
"""
import sublime, sublime_plugin
class CycleSettingCommand(sublime_plugin.TextCommand):
def run(self, edit, setting, options):
"""Cycle $setting to next of $options"""
index = len(options)
if not index :
return
settings = sublime.load_settings('Preferences.sublime-settings')
value = settings.get(setting)
if value in options :
index = options.index(value)
index += 1
if index >= len(options) :
index = 0
value = options[index]
settings.set(setting, value)
sublime.save_settings('Preferences.sublime-settings')
self.view.set_status(
'cycle_setting',
"Setting '{setting}' cycled to value '{value}'".format(**locals())
)
Now, lets use our new command for bindings
Go to Preferences/Key Bindings
[
{
"keys": ["f5"],
"command": "cycle_setting",
"args": {
"setting": "draw_centered",
"options": [true, false]
}
},
{
"keys": ["shift+f5"],
"command": "cycle_setting",
"args": {
"setting": "default_line_ending",
"options": ["system", "windows", "unix"]
}
}
]
What our command is doing is cycling through the options array and saving the current one in the cycle to User/Preferences.sublime-settings.
I hope this helps someone else, I actually spend a while trying to find how to achieve this with many commands for external plugins and at this rate I was soon going to run out of key combinations, regards!

Implement Vim-like imap mapping in sublime-text 3

I would like to have ; inserted at the end of line, when I press ;; while in insert mode. What would be mapping for that in sublime-text 3?
Somtehing like:
inoremap ;; <C-o>A;
in VIM.
So far, I managed to get to EOL I am currently on, but have no idea on how to chain other command to insert ;. I was not able to find anything in documentation on running multiple commands in sequence.
{
"keys": [";", ";"],
"command": "move_to", "args": { "to": "eol" }
}
As I wasn't able to find any native-to-editor solution to this,
luckily there is a plugin called Chain of Command
https://packagecontrol.io/packages/Chain%20of%20Command
Then you can put this into user keymap settings file:
{
"keys": [";", ";"],
"command": "chain",
"args": {
"commands": [
[ "move_to", { "to": "eol" }],
[ "insert", { "characters": ";" } ]
]
},
// with a help of this line, command won't get executed
// if in command_mode (vintage mode enabled), so it applies to insert mode
// only.
"context": [ { "key": "setting.command_mode", "operand": false } ]
}

Resources