What is the command to open a file in specified ViewColumn in VS Codium? - visual-studio-code

How can I open a specific file in a specified split view with an built-in command in VS codium? Is that possible without writing an extension?
I have a project, and I periodically I want to open a pair of files in specific way in split view. Let's mention them as problem1.py and problem1.txt. I want to programmatically open problem1.py in the left side, and the problem1.txt in the right side.
I found an a documentation for the command vscode.open:
vscode.open - Opens the provided resource in the editor. Can be a text or binary file, or an http(s) URL. If you need more control over the options for opening a text file, use vscode.window.showTextDocument instead.
uri - Uri of a text document
columnOrOptions - (optional) Either the column in which to open or editor options, see vscode.TextDocumentShowOptions
label - (optional)
(returns) - no result
In keybindings.json I created following statements:
{
"key": "numpad4",
"command": "vscode.open",
"args": "/home/user/myproject/README.md"
},
{
"key": "numpad6",
"command": "vscode.open",
"args": ["/home/user/myproject/README.md", "1"]
},
Now when I press numpad4, it works perfectly, the readme file opens. But when I press numpad6, I get a notification:
Unable to open '': An unknown error occurred. Please consult the log for more details.
Am I passing parameters in a wrong way? Why it does not detect a filename? And I do not see whare to view a log.
Additional info:
VS codium version: 1.66.2.
I saw a cli option -r, --reuse-window, but it has not control of in which view I want to open a file.
I saw a similar question, but there the author wants to do it from extension, while I would prefer to not write an extension for this problem. Also, as documentation says, I think I do not need vscode.window.showTextDocument, as vscode.open should be enough for my task.
Here is an enum list for available ViewColumn values: https://code.visualstudio.com/api/references/vscode-api#ViewColumn

you can use the extension HTML Related Links use command htmlRelatedLinks.openFile
{
"key": "numpad6",
"command": "htmlRelatedLinks.openFile",
"args": {
"file": "${workspaceFolder}/README.md",
"method": "vscode.open",
"viewColumn": 1
}
}

Combining the #rioV8's solution of using HTML Related Links and #Mark's solution for combining two commands into one, the resulting keybindings.json is the following:
{
"key": "numpad5",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{ "command": "htmlRelatedLinks.openFile",
"args": {
"file": "/home/user/myproject/problem1.py",
"method": "vscode.open",
"viewColumn": 1,
}
},
{ "command": "htmlRelatedLinks.openFile",
"args": {
"file": "/home/user/myproject/problem1.txt",
"method": "vscode.open",
"viewColumn": 2,
}
},
]
}
},

Related

VS Code: Create file inside the current directory of the editor

Is it possible to create a new file inside the same folder as the file that is active in the editor by the time the keyboard shortcut ctrl+n for explorer.newFile is hit?
I can tell if it's the correct folder by looking at the breadcrumbs and mostly have the file explorer toggled off. The command, however, seems to create a file inside the last folder opened or focused by the file explorer.
You can do this by overloading that explorer.newFile command's default keybinding to run a task instead. Here is the keybinding:
{
"key": "ctrl+n",
"command": "workbench.action.tasks.runTask",
"args": "newFile",
"when": "editorFocus" // must have an editor focused
},
And the task (in tasks.json):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
"command": "touch '${fileDirname}/${input:fileName}' && code '${fileDirname}/${input:fileName}'",
"type": "shell",
"problemMatcher": [],
},
],
"inputs": [
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
That will create a file in the same directory as the active editor's directory - it will ask you for the filename - and then open it.
I used the bash command touch to create the file (if it doesn't already exist), if you are using a different shell you will need the equivalent of touch.

Where to define a shared problemMatcher to check the terminal?

In vscode I experience sometimes I can click on build errors in the integrated terminal and sometimes it is not possible to do so. This has annoyed me for quite some time because I was not able to find a pattern, until today when I was editing tasks.json.
It looks to be related to defining a problemMatcher in .vscode/tasks.json. Removing the problemMatcher section from the file and build errors in terminal were no longer clickable but putting it back did not re-enable them.
My vscode-project is located in a subfolder of the build tree and its build root for the entire project is two levels up ${workspaceFolder}/../.. which I believe maybe could confuse some build tools.
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"problemMatcher": {
"fileLocation": "relative",
"severity": "error",
"pattern":{
"regexp": "^system/mytool/(.*):(\\d+):(\\d+):\\s+(warning|error):(.*)$",
"file": 1,
"location": 2,
"column": 3,
"severity": 4,
"message": 5
},
},
"tasks": [
{
"type": "shell",
"label": "android deploy",
"command": "cd ${workspaceFolder}/../..; source build/envsetup.sh ; lunch hikey960-userdebug ; m mytool",
"args": [
],
"options": {
},
"group": "build"
},
]
}
I have seen examples putting "problemMatcher" = "$gcc" inside the task, should I define my problem matcher globally somewhere else and refer to it my name instead?
How to use it to parse the output when I build by typing make-commands in the integrated terminal?
You cannot define problem matchers globally. Instead you have to define them in each task.
This is somewhat ugly because you may have to copy the same problem matcher into a lot of tasks. There is an open issue for this: Global task properties

Automating build task with unique ids

I've written some embedded code (using VS-Code as my IDE) that is ready for deployment to many different devices.
The code contains a file config.h that defines a unique device_id that needs to change for each device.
I have a file unique_ids.csv that contains all of the unique ids that I need to use.
VS-Code can automatically build my project and creates a file called project_name.bin.
How can I set up a script that automatically takes the uniqe ids from my CSV file and builds a specific device_id.bin file for each one?
I suspect this may require utilizing Visual Studio Tasks in some way. Here is my current tasks.json:
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"name": "make",
"isShellCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}/mbed-os"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"args": ["-j"],
"linux": {
"command": "make"
},
"osx": {
"command": "make"
},
"windows": {
"command": "make.exe"
}
}
Thinking of an alternative approach, I might not be answering directly.
If you are using the csv for release and not for debug, why would you build the release inside VS?
I would rather build my debug inside VS and build my different releases using command line with a batch script.
Something like this batch pseudo code
set id_list=unique_ids.csv
set id=
for %%a in (%id_list%) do (
set "id=%%~na"
replace in config.h device_id by device_%id%
make your_project
)

Visual Studio Code 1.17.1 Open in Browser without opening terminal

Looks like an old question, but no proper answers found.
I've looked at here and here.
Right now, I can open Chrome by doing this:
task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "echo",
"type": "shell",
"command": "echo Hello"
},
{
"taskName": "Open in Chrome",
// "type": "process",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"args": ["${file}"],
}
]
}
keybindings.json:
[{
"key": "ctrl+alt+g",
"command": "workbench.action.tasks.runTask",
"args": "Open in Chrome"
},]
Note that I don't even need type: process to make it run and can only run it using my own key binding. If I use ctrl+shift+B (Windows), it'll allow one task only.
However, every time I run the task, the terminal is also opened with: Terminal will be reused by tasks, press any key to close it. which is repetitive and not really helpful for front-end work.
Is there a way to turn that off?
I've tried adding:
"presentation": {
"reveal": "never" //same with "silent"
}
to the task in task.json but it doesn't work.
Answer to close question:
The easiest way is to use an extension like this one: https://marketplace.visualstudio.com/items?itemName=techer.open-in-browser

missing items from Command palette in ST2

I'm trying to create a custom package and one thing I would like to do is bring up the Build Systems(located in the Tools menu item) in the command palette(show_overlay). So I have tried to create a Default.sublime-commands file in my package and type in...
[
{ "caption": "Build System", "command": "build_system" }
]
...to enable that menu item for the command palette(I also tried set_build_system) and then I created a Default.sublime-keymap file in my package so that I can access the Build System list from via shortcut...
[
{
"keys": ["f9"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Build System"}
}
]
I am not having any luck exposing the Build System menu item to the command palette. Can I get some help on this? I also noticed the Tools menu item is not available in the command palette as well. What am I missing?
Ok I figured it out. The command palette can only be populated by existing commands that run in sublime. The way you can view which commands that are being run in sublime is to open up the console(CTR + ~) and type in sublime.log_command(True)
Now whenever you do anything that makes sublime trigger a command, it will log that action in the console. Armed with this knowledge, we we go to Tools > Build System and click on the build system type we want, say, C++, we get:
command: set_build_system {"file": "Packages/C++/C++.sublime-build"}
Sweet! Knowing this we can go to our .sublime-commands file(you can call it Default.sublime-commands) and type in the below code:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
}
]
Tip: pay close attention to the "caption" it's what we will use to tie our .sublime-command file with our .sublime-keymap file. Let's add another one build system:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
},
{
"caption": "Set Build System: Python", "command": "set_build_system",
"args": { "file":"Packages/Python/Python.sublime-build" }
}
]
Now that we have exposed these two commands in our .sublime-commands file. We can create a shortcut for it in our .sublime-keymap file. I called mine Default.sublime-keymap:
[
{
"keys": ["f8"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Set Build System:"}
}
]
Notice the "text" key. Look familiar? This is how you connect your key binding to your command. Save press F8 and boom! You have our own custom command palette menu. Enjoy!
PS: you can put your .sublime-commands/.sublime-keymap files in your User package or add to any existing ones if you have them there if you just want to customize your sublime text 2 without making a custom package.