key map sublimeREPL commands in Sublime Text 2 - sublimetext2

I'm trying to map keyboard shortcuts for SublimeREPL plugin commands. Looking at SublimeREPL it looks like a menu item command is defined as:
Default.sublime-commands
{
"caption": "SublimeREPL: SBT for opened folder",
"command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}
or in
Main.sublime-menu
{"command": "repl_open",
"caption": "SBT for opened folder",
"id": "repl_sbt",
"mnemonic": "b",
"args": {
"type": "subprocess",
"encoding": "utf8",
"external_id": "scala",
"cmd": {"linux": ["sbt"],
"osx": ["sbt"],
"windows": ["sbt"]},
"soft_quit": "\nexit\n",
"cwd": "$folder",
"cmd_postfix": "\n",
"extend_env": {"osx": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"linux": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"windows": {"EMACS": "1"}},
"suppress_echo": false,
"syntax": "Packages/Scala/Scala.tmLanguage"
}
}
I've tried making a keybinding in my SublimeREPL.sublime-settings to:
[{ "keys": ["super+shift+k"], "command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}]
But when I try to use it the Sublime console just says:
no command for selector: noop:
Same if I map it to:
[{ "keys": ["super+shift+k"], "command": "repl_open",
"args": {
"type": "subprocess",
"encoding": "utf8",
"external_id": "scala",
"cmd": {"linux": ["sbt"],
"osx": ["sbt"],
"windows": ["sbt"]},
"soft_quit": "\nexit\n",
"cwd": "$folder",
"cmd_postfix": "\n",
"extend_env": {"osx": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"linux": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"windows": {"EMACS": "1"}},
"suppress_echo": false,
"syntax": "Packages/Scala/Scala.tmLanguage"
}
}]

Your first keybinding is correct and should work as expected. Place is in Preferences -> Key Bindings - User file.
[{ "keys": ["super+shift+k"], "command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}]
based on your description, I suspect that some other command is hijacking super+shift+k.
>>> sublime.log_commands(True)
will let you see what's get called when.

Related

Reference a variabled in tasks.json that was defined in launch.json

I'm trying to configure my project in Visual Studio Code.
I am using CMake+make to build my project. CMake needs to know the project configuration (Debug/Release) and architecture (x64/x86) before it can generate the make files.
I could define 4 tasks and 4 launch configurations to achieve this (each hardcoding both options), but that is a lot of duplication.
Instead, I would like to use environment variables to basically "pass down" the parameters from the launch configuration to a single task.
However, VScode doesn't seem to forward what I define in the launch configuration to the task.
Here is what I have:
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug (x64)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/debug/x64/executable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "proj_configuration",
"value": "Debug"
},
{
"name": "proj_platform",
"value": "x64"
}
],
// "variables": {
// "proj_configuration": "Debug",
// "proj_platform": "x64"
// },
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Make Project",
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Project",
"type": "shell",
"command": "cmake",
"args": [
"-B${workspaceFolder}/build",
"-H${workspaceFolder}",
"-DCMAKE_BUILD_TYPE=${proj_configuration}",
"-DCMAKE_GENERATOR_PLATFORM=${proj_platform}"
],
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Make Project",
"type": "shell",
"command": "make",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": ["Build Project"],
}
]
}
When I try to run my project, I get
Executing task: cmake -B/home/user/project/build -H/home/user/project/ -DCMAKE_BUILD_TYPE=${proj_configuration} -DCMAKE_GENERATOR_PLATFORM=${proj_platform}
As you can see, the 2 variables are not replaced.
I've also tried using the property "variables", but no luck using that either.
So, is it possible to do what I described, or do I have to define multiple tasks instead?
with the extension Command Variable you can save a number of strings in a store and use these in launch.json and task.json. See example in the link

Configure Visual Studio Code task to execute multiple shell commands in sequence

Is there a possibility, to add multiple shell commands in sequence to a Visual Studio Code task with separate arguments and options? I managed to execute multiple commands using && to chain then together to a single command, as you could in any Linux shell. But i guess, there has to be a better way to do this.
I use Visual Studio Code on Ubuntu 18.04 LTS.
Here is the example of how i currently chained the commands for a build task in a task.json file to build a c++ project using cmake:
{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "cd ${workspaceFolder}/build && cmake .. && make clean && make",
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
Update: -------------------------------------------------------------------
I tried to use the dependsOn: property to start the 4 separatly defined tasks. However, this resulted in all 4 commands being executed at the same time in different shell instances instead of in sequence as needed:
{
"tasks": [
{
"type": "shell",
"label": "build project",
"dependsOn": [
"Go to build folder",
"Cmake",
"make clean",
"make",
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Go to build folder",
"type": "shell",
"command": "cd ${workspaceFolder}/build",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "Cmake",
"type": "shell",
"command": "cmake ..",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "make",
"type": "shell",
"command": "make",
"presentation": {
"group": "cmake-complile"
}
}
],
"version": "2.0.0"
}
Thanks to the many comments i found a solution which works well by setting "dependsOrder" to "sequence":
{
"tasks": [
{
"type": "shell",
"label": "build project",
"dependsOrder": "sequence",
"dependsOn": [
"Cmake",
"make clean",
"make",
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Cmake",
"type": "shell",
"command": "cmake ..",
"options": {
"cwd": "${workspaceFolder}/build",
},
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"options": {
"cwd": "${workspaceFolder}/build",
},
},
{
"label": "make",
"type": "shell",
"command": "make",
"options": {
"cwd": "${workspaceFolder}/build",
},
}
],
"version": "2.0.0"
}

VS code, c++ extension, task.json is there a way to group args in a array and reference in a task

Im using VS code with c++ extesion, on my task.json i have a debug task and runwithoutdebug task, this tasks have common arguments and i instead of have to add those arguments in both, i was trying to reference a array, and write those common argument in that array.
Is that possible? is there any alternative
Example: --->task.json
"version": "2.0.0",
"tasks": [
{ ----------------------> TASK 1
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"main.cpp",
"header.cpp",
"-o",
"main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{ ----------------------> TASK 2
"label": "compileDeb",
"type": "shell",
"command": "g++",
"args": [
"-g",
"main.cpp",
"header.cpp",
"-o",
"main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Has you can see i have on task "compilewithoutDeb" and "compileDeb" this common arguments
"main.cpp",
"header.cpp",
"-o",
"main.exe"
Is there a away that i could do like:
"paramArg":[
"main.cpp",
"header.cpp",
"-o",
"main.exe"
]
"tasks": [
{
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"${paramArg}" <-----------------
],
"group": {
"kind": "build",
"isDefault": true
}
}
You can create an input. It would be a promptString with the arguments as the default. For your other task you'd add the "-g" parameter before the input.
"tasks": [
{
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"${input:paramArg}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "compileDeb",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${input:paramArg}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
],
"inputs": [
{
"id": "paramArg",
"description": "Just hit the Enter key",
"type": "promptString",
"default": "main.cpp header.cpp -o main.exe"
},
]
Yes, there's an (ugly) way to do this. While tasks.json can only define tasks (that is what is is for) it can reference different kind of variables.
The two options that would be reasonable to use: env (if you want to set it outside of vscode which doesn't seem to be the case here) or config.
To apply it to your sample:
settings.json (either in your user's settings or in the workspace [folder .vscode])
{
"personalSettings.commonArgs": [
"-o",
"main.exe",
"header.cpp",
"main.cpp"
]
}
and then your tasks.json would be
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{ // ----------------------> TASK 1
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++ ${config:personalSettings.commonArgs}",
"group": {
"kind": "build",
"isDefault": true
}
},
{ // ----------------------> TASK 2
"label": "compileDeb",
"type": "shell",
"command": "g++ ${config:personalSettings.commonArgs}",
"args": [
"-g"
],
"group": "build"
}
]
}
(additional changes: only one build task can be the default, used inline comments)
Note: to get the most out of your build tasks I'd suggest to add a problem matcher to your task definition, something like
// use this or similar when cpptools or a similar libre/free extension
// is installed providing an appropriate matcher
// "problemMatcher": "$gcc"
// remark: ms-vscode.cpptools is "gratis", but comes with telemetry + usage-restrictions
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
Note: I'd suggest to not hard-wire the file names, instead use either a "general task" which uses ${file} or change the tool you run to make where you have all the arguments defined clean, can build outside of vscode, too and because of dependency tracking only compile what is actually necessary.

vs code to run c code

I have an existing code base that builds with a makefile and I'd like to use Visual Studio Code to run it.
There are my launch.json and tasks.json.
{
"version": "0.2.0",
"configurations": [{
"name": "C Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"launchOptionType": "Local",
"targetArchitecture": "x64",
"cwd": "${workspaceRoot}",
"program": "E:/code/c",
"miDebuggerPath": "E:/Program Files/TDM-GCC-64/bin/gdb64.exe",
"args": [""],
"stopAtEntry": false,
"externalConsole": true,
"preLaunchTask": "gcc"  
}]
}
{
"version": "0.1.0",
"command": "gcc",
"args": ["${file}", "-o", "${fileBasenameNoExtension}.exe", "-g3", "-Og", "-Wall", "-static-libgcc", "-std=c11", "-fexec-charset=GBK", "-finput-charset=UTF-8"],
"showOutput": "always",
"problemMatcher": {
"owner": "c",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
then when I run Hello world,then will have some problems.
Try changing your paths to use \ instead of / on Windows. Since you have to escape \ in json strings, the actual value would be:
"program": "E:\\code\\c\\${fileBasenameNoExtension}.exe"
provided the built executable is actually at E:\code\c

VS Code tasks.json -- Tasks work individually, but not combined

This is driving me nuts (go nuts!). Build / run file is proper and fmt command is proper. But if I try to combine into one tasks file, it stops working.
These two work fine on their own and behave the way I want:
tasks.json
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"taskName": "build",
"args": [
"build",
"-o",
"${workspaceRoot}.exe",
"&&",
"${workspaceRoot}.exe"
],
"isBuildCommand": true
}
tasks.json
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"taskName": "fmt",
"args": [
"fmt",
"${file}"
],
"isBuildCommand": true
}
But combined into one file, it will not work:
tasks.json
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"tasks": [
{
"taskName": "build",
"args": [
"build",
"-o",
"${workspaceRoot}.exe",
"&&",
"${workspaceRoot}.exe"
],
"isBuildCommand": true
},
{
"taskName": "fmt",
"args": [
"fmt",
"${file}"
]
}
]
}
Error given on build:
can't load package: package build: cannot find package "build" in any of:
D:\dev\Go\src\build (from $GOROOT)
D:\dev\Gopher\src\build (from $GOPATH)
can't load package: package -o: cannot find package "-o" in any of:
D:\dev\Go\src\-o (from $GOROOT)
D:\dev\Gopher\src\-o (from $GOPATH)
can't load package: package d:/dev/Gopher/src/myproject.exe: cannot find package "d:/dev/Gopher/src/myproject.exe" in any of:
D:\dev\Go\src\d:\dev\Gopher\src\myproject.exe (from $GOROOT)
D:\dev\Gopher\src\d:\dev\Gopher\src\myproject.exe (from $GOPATH)
I can't seem to understand why it works one way, but not the other. The second method (for combined tasks) is outlined here: Define multiple tasks in VSCode
Answer: The problem lies with adding "build" or "fmt" as an args when it's already listed as a taskname. I did not know that's how taskname worked. Final working product which allows users to develop without worrying about stupid windows firewalls:
tasks.json (final & working thanks to #not-a-golfer)
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"echoCommand": true ,
"tasks": [
{
"taskName": "build",
"args": [
"-o",
"${workspaceRoot}.exe",
"&&",
"${workspaceRoot}.exe"
],
"isBuildCommand": true
},
{
"taskName": "fmt",
"args": [
"${file}"
]
}
]
}
The following seems to be working, but it appears that you can't chain the running with &&:
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"echoCommand": true ,
"tasks": [
{
"taskName": "build",
"args": [
"-x",
"-o",
"${workspaceRoot}.exe"
],
"isBuildCommand": true
},
{
"taskName": "fmt",
"args": [
"${file}"
]
}
]
}
You should add the attribute suppressTaskName.
OPs solution to remove superfluous build parameter obviously works, however, VSCode's documentation covers this very example:
We set suppressTaskName to true as by default the task name is also passed to the command which would result in "echo hello Hello World".
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "hello",
"args": ["Hello World"]
},
{
"taskName": "bye",
"args": ["Good Bye"]
}
]
}
My favorite build task is:
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"echoCommand": true ,
"options": {
"cwd": "${fileDirname}"
},
"tasks": [
{
"taskName": "build",
"args": [
"build",
"-x"
],
"isBuildCommand": true,
"suppressTaskName": true
}
]
}