Getting Started with VSCode
Start using Julia interactively with VSCode, edit your code efficiently, and find about some cool things you can do.
Installing Julia and VSCode
In case you haven’t installed Julia yet, you can download the latest stable release here, which, last time I tested this tutorial, was version 1.6.1. Bear in mind that if you install Julia from certain package distribution systems (like APT) you might get the latests LTS version which is currently 1.0.
In this post we are going to be working with the VSCode IDE for Julia, which you can get here.
Make sure to also get the Julia Extension for VSCode.
Starting The Julia REPL
One of the salient features of Julia is that it can be used interactively.
In order to start executing Julia code from within VSCode, one way to do so is by starting the REPL.
To start the REPL, type Ctrl + Shift + P, which will open the command pallette, and type
Julia: Start REPL
Note that, as soon as you have typed some of that text, VSCode will autocomplete the expression for you. So don’t worry about remembering the exact syntax for this commands.
The command above will show an interactive prompt below the text editor, in which we can execute some Julia code. For example, we will run a ‘Hello World’ example
Run Code From your Editor in the Julia REPL
An interesting way of using VSCode is to select fragments of code from your text editor to execute in the REPL.
There are a couple of keyboard shortcuts to do this:
Run a Line (Ctrl+Enter)
Pretty much like copy-pasting selected text in the editor or the current line. Beware that all line-number information will be lost if the code is run this way.
Run a Block (Alt+Enter)
The recommended way of executing code from the editor. In the absence of selected text, the command will identify the code block in which the cursor is positioned. Relative paths and line number information is retained in this way.
Run a Cell (Shift+Enter)
This command will run a code cell demarked with the characters ##, retaining location information as with code block execution. In case there are no demarked cells, it will execute the entire document.
Run a File (Julia: Execute File in REPL )
Runs the entire file in the REPL. Open the command pallette (Ctrl + Shift + P) and start typing “Julia: Execute File in REPL” until you can autocomplete it.
Alternatively, you can clicking ‘run’ in the upper-right corder of the screen, and selecting the ‘Execute File in REPL’ option:
Autocomplete should kick-in automatically as you type. If it doesn’t, you can force it by pressing “Ctrl + Space”.
How to Input Unicode Math Symbols with Latex Autocomplete
One of the nice syntactic features of Julia is that it supports unicode characters. For example, the following is actual Julia code:
θ = π/4
However, a good question is how do you type this type of characters in VSCode?
If doing this in the text editor, you can do so by typing the corresponding Latex code and hitting Enter on the autocomplete suggestion:
In the REPL, you should press the TAB key after the Latex code, instead.
Check out this fantastic list of latex-enhanced mathematical programming with Julia: BeautifulAlgorithms.jl.
Native Jupyter Notebooks Integration in VSCode for Julia
A nice feature of Visual Studio code is that you can now run Jupyter notebooks natively. This can be easily done without installing additional packages, and with virtually no configuration required. All is taken care by two VSCode extensions: Julia and Jupyter.
At the time of writing this, the following steps are required:
- Install the Jupyter extension within VSCode.
- Enable the “Notebook controller” in the Julia extension settings.
- To create a new Jupyter notebook, hit “Ctrl+Shift+P” to open the VSCode command pallette, and type “Jupyer: Create New Blank Notebook”.
- After you save your file, you should be able to select the Julia Kernel, when prompted.
This feature was announced at the JuliaCon2021 talk by David Anthoff. At the time of writing this, this feature is considered “experimental”.
It should be noticed that, when attempting to run some code from VSCode in the REPL, for example to include a module file, the current directory might not be the directory where our file is located.
For this, it is useful to press the extra button in the upper-right corner, and click on “Change to this directory”, as shown below.
Cool things you can do with Julia in VSCode
VSCode is an incredible IDE, with so many things you can do. Make sure to check out the VSCode Documentation, and the Julia-VSCode Documentation.
A few interesting things you can do are:
- Github integration.
- Compiling Sysimages to Speed-Up Load Time.
- Profiling your code and viewing the profiler results in VSCode.
We have covered some of the basics features of Julia-VSCode. With this material, you should be able to start using Julia interactively, edit your code efficiently, and dig deeper into the more advanced features as you need them. Happy coding!