Despite being introduced recently (in 2012), Julia is growing in popularity as a general programming language and especially as a powerful tool in data analytics. Julia combines the productivity of interpreted languages such as R and Python and the efficiency of compiled languages such as C, C++ and Fortran and hence overcomes the “two language problem”. Benhcmarks show that Julia performance is quite near that of C with the help of its powerful JIT compiler. The package base is quickly keeping up pace with the large bases of more established languages. The compiled assembly code of Julia code can be easily viewed from within its interactive shell (and its low level efficiency can be witnessed).
There are several ways to edit and run Julia code. The Jupyter notebook “IJulia” is the easiest one to deploy:
julia> Pkg.add("IJulia") julia> using IJulia julia> notebook(detached=true)
These commands will install the IJulia package and start a Julia kernel inside the default browser, and will persist even if Julia shell is exited.
The notebook can even be invoked from the UNIX shell with the following one-liner command:
The notebook is good for prototyping short code snippets and executing them as block “cells”. However for larger and more complex coding tasks, an integrated development environment (IDE) is preferred. “Getting Started with Julia”, a comprehensive beginner’s resource for the language by Ivo Balbaert, mentions the IDE options available for Julia as Julia Studio, Sublime-IJulia and Juno.
GUI based IDE’s usually absorb a large portion of the valuable CPU cycles and RAM that would otherwise be available for resource hungry data analytics tasks. For those coders who devote scarce computer resources to data tasks, lightweight solutions are preferred. Vim is among the most popular lightweight text editors and is highly versatile in a variety of languages due to its large base of plugins/extensions. For a coder who gets used to the practical key bindings, Vim acts as a natural extension of the body.
Although there are some extensions to be installed for Julia, for those who are used to work with Vim editor, there is not yet an integrated extension such as Nvim-R that combines Vim and R in a seamless manner. However several open-source extensions and applications can be combined to create a working environment for Julia through which you can edit Julia code with syntax highlighting and execute selected portions of the code inside the interactive shell.
As also mentioned in “Getting Started with Julia”, julia-vim plugin is a good starting point, but we have to combine it with some other resources to create an efficient environment. The prerequisites for the recipe are:
- Vim editor
- GNU Screen or Tmux terminal multiplexers
- julia-vim plugin for Vim
- vim-slime plugin for Vim
- and of course Julia source code or binaries
Most of the prerequsites are available in repositories of mainstream Linux distributions or can be downloaded and installed from the source code repositories following the links. Apart from the lightweight nature of Vim and the power and speed it derives from key bindings, another advantage of such a recipe is that, it can even be run on command-line (shell only) run levels and headless systems accessed through SSH.
We need the terminal multiplexer so that, both the code editor and interactive shell can be viewed at the same time , julia-vim plugin for Julia syntax highlighting in Vim and vim-slime plugin to send the Julia code from the Vim editor to interactive shell to be interpreted/compiled (not only for Julia, but any language with a REPL shell).
The default terminal multiplexer for vim-slime is GNU Screen and the below scripts will utilize it. However the option can be changed to Tmux easily by:
let g:slime_target = "tmux"
command from the Vim editor or appending to .vimrc file.
After the installations, we first create the steps that GNU screen will follow when invoked from a shell script:
layout new split -s screen 0 exec vim focus next screen 1 exec julia focus next layout save default
We will save these steps into a file (named “screenjulia” in my example) that will be invoked by the -c argument of screen command in order to override the default configuration and create a new layout for the session. The file creates a new layout, splits the screen session horizontally, names the upper region as “0” and executes vim inside (the julia filename will be appended by the script), names the lower region as “1” and executes Julia inside.
Due to difficulties to pass arguments to the custom configuration file, the optional filename argument will be inserted by the script, if provided:
#!/bin/bash # vim-julia-slime binder sed -i "s/^exec vim.*$/exec vim $1/" /path/to/screenjulia screen -S 1 -c /path/to/screenjulia
The sed command inserts the Julia filename if provided by the optional $1 argument to the end of “exec vim” line in the configuration file “screenjulia” (replace “/path/to/screenjulia” with your actual path). The default session name for screen is “1”.
In order to enable the Julia syntax highlighting feature, the filename must have a .jl extension. To enable these features, it is useful to add the following lines into the .vimrc file:
filetype plugin indent on syntax on
Also enabling line numbers will simplify the line selection in visual mode, so it should also be added to the .vimrc file:
To use this script as a custom command, it should be made executable and a symlink pointing to the file should be created inside a location that is declared in the $PATH environment variable of your .bashrc or /etc/environment (such as /usr/bin) (replace /path/to/script with your actual path to the script file):
Now, invoking the scriptname (replace it with any name you’d like for your command) command from anywhere will initiate the following layout:
The code lines are selected in visual mode in Vim and sent by C-c, C-c command (insert mode should be exited) – simply holding CTRL and double tapping c. In each session, the first time this sequence is entered, Vim will prompt for screen session and window name. The default values as defined in our script are 1 and 1. Anytime you can change these values with C-c, v or :SlimeConfig.
The visual mode works by v for character-wise and V for line-wise selections. Without any selection, C-c, C-c binding will send the current block. vip command also selects the current block or paragraph. To be more precise in the line range of code, you should enter VXgg where X is the number of last line to be selected (set nu option in .vimrc file is important in this sense)
With a small code sample for demonstration purposes, selecting first 3 lines with V3gg from Vim and sending to Julia window by C-c, C-c result in:
To switch between panes you should use C-a [tab] and the complete layout can be exited through screen by C-a \ binding.
With an Nvim-R like plugin, vim + Julia can be integrated better with autocompletion and an object browser.