Scilab: Run Scilab in VS Code
Tags: scilab, course, tutorial, vscode
Published: 2022-01-08 21:33:35

 - [VS Code ❤ Scilab](#vs-code-❤-scilab)
 - [Download Code Extensions](#download-code-extensions)
    - [Scilab Extension](#scilab-extension)
    - [Code Runner](#code-runner)
 - [Customize Code Runner for Scilab](#customize-code-runner-for-scilab)
    - [Open `settings.json`](#open-settingsjson)
    - [Add `code-runner.executorMap` command](#add-code-runnerexecutormap-command)
    - [Specify run mode instruction](#specify-run-mode-instruction)
    - [Add Scilab instruction to execute](#add-scilab-instruction-to-execute)

## VS Code ❤ Scilab

Firstly, we need to have installed in our computer both [Scilab](https://www.scilab.org/download/) and [VS Code](https://code.visualstudio.com/download).

## Download Code Extensions

We need only two extensions for run Scilab inside VS Code:

- [Scilab](https://marketplace.visualstudio.com/items?itemName=mammothb.scilab) by [mammothb](https://marketplace.visualstudio.com/publishers/mammothb)
- [Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry)

### Scilab Extension

This extension is useful to make a highlight of the reserved word of Scilab syntaxis, this allow us to have our own customization in VS Code.  
This is an example of the two sights of the Scilab in Code vs Scilab IDE:

<!-- ![Scilab Codes](../img/scinotes-code.png) -->
![Scilab Codes](https://raw.githubusercontent.com/fcomovaz/blog-drafts/main/img/scinotes-code.png)

### Code Runner

This extension allow us run code snippets or code files for multiple languages and create custom commands.

## Customize Code Runner for Scilab

In this step we need to customize the `settings.json` file, particulary, for the `code-runner.executorMap`. In this case, I created a Github Repo where I explain my [VS Code configurations](https://github.com/fcomovaz/vscode-config) you can access to it and follow the instructions there, anyway, I'll explain how to configurate the file in this post:

- Open `settings.json`.
- Add `code-runner.executorMap` command.
- Specify run mode instruction.
- Add Scilab instruction to execute.

### Open `settings.json`

This is the file where you can overwrite the default configurations of the editor.

You can access it with `Ctrl + ,` and clicking like the image below:


![settings.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e38pxq8v45w8gw17z4rh.png)

### Add `code-runner.executorMap` command

If you are used to move and customize VS Code extensions, this step won't be a problem for you, either way I will show you how to do it.

In the `settings.json` file you can find a blank file (or not) and how its own name specify, the instructions needs to be in JSON format.

```javascript
{
     "code-runner.executorMap": {},
}
```

With this line we can begin to give instructions depending on any programming language we need to overwrite.

### Specify run mode instruction

Foremostly, we need to understand what is a run mode in Scilab.

Scilab have 3 different ways to run its programs:

1. `Wscilex`: runs the standar Scilab.
2. `Wscilex-cli`: in this mode, Scilab is started as command line with advanced features still available (graphics, export, xcos, scinotes, help browser, ...).
3. `Scilex`. this mode has two features:
   - This mode prevents loading the Java Virtual Machine. It starts faster and uses less memory.
   - Scilab is started as command line interpreter without any advanced features requiring the JVM: graphics, export, xcos, scinotes, help browser, other java interfaces, the Java module,... are not available.

We only need to be focus on `Wscilex-cli` and `Scilex` because we are goin to search for the instalation folder of Scilab (in my case, Scilab is installed in `D:\Programs\scilab-6.1.0\` so replace for your path).

### Add Scilab instruction to execute

If you want to run Scilab and visualize plots, graphs, images, etc, use the following script:

```javascript
{
     "code-runner.executorMap": {
        // run Scilab with graphic Functions
        "scilab": "cd $dir && D:\\Programs\\scilab-6.1.0\\bin\\WScilex-cli.exe -nb -f $fileName",
    },
}
```

But if you only want numerical results:

```javascript
{
     "code-runner.executorMap": {
        // run Scilab from VSCode
        "scilab": "cd $dir && D:\\Programs\\scilab-6.1.0\\bin\\Scilex.exe -nb -quit -f $fileName",
    },
}
```

In both cases we have flags that specify different things:

- `-nb`: "no banner": Cancels the display of the Scilab's loading message.
- `-quit`: This option forces scilab to always exit after the instruction(s) passed with the -e option, or the script referred to by the -f option, have been executed, even in case of a runtime error. This option should always be used in batch mode.
- `-f file`: This option execute the file which is specified.

For `$dir` and `$fileName`, these are options nativately used in code-runner configuration.

And that is everything you need to begin with Scilab in VS Code!!

![scilab hello](https://raw.githubusercontent.com/fcomovaz/blog-drafts/main/img/scilab-invitation.PNG)