tux

Working on a server the first thing you like to have is an IDE that allows easy code development. Unfortunately this is not possible; you will inevitably turn to a text editor that allows easy code development. In the search for an editor able to highlight the swift syntax, I've landed to the first UNIX beloved/hated environment editor: vim.

Vim is available on any Linux distribution. Below we will see how to download the plugin to highlight swift syntax. For this procedure you must have installed the git program. The plug-in to download is called swift.vim, for its installation the easiest way is to download and install the plugin manager for vim: vim-plug. To download the program, type from shell:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

The ./vim directory will be created in your home. This directory contains the plugin manager and the subsequent plugins that we will ask the program to download.

It is now necessary to create the .vimrc file in your home and copy the code portions needed to run the program. You can use vim to edit the file.

$ vim .vimrc

In the editor type 'i' for insertion and copy and paste the following code:

call plug#begin('~/.vim/plugged')


call plug#end()

It can be seen that references to the plugins to be installed will be inserted between the start line and the end line. That's right. We save and exit vim by pressing esc:wq and then go to swift.vim git repository.

From the "clone or download" button copy the address of the package, and through vim insert the command to download the plugin:

call plug#begin('~/.vim/plugged')

Plug 'https://github.com/keith/swift.vim.git'

call plug#end()

Save and rerun vim, then type the command :PlugInstall. Within a few seconds, the plugin is downloaded and installed. We can now open a swift file to see the effect it does.

With the same procedure, you can install the Syntastic plugin that lets you highlight errors in the code. Add to the .vimrc file under the previous instruction Plug the following line:

Plug 'https://github.com/vim-syntastic/syntastic.git'

Open vim and launch the command :PlugInstall. After installation the plugin is working.

To test it, you can write a .cpp file with some errors, while the file will be saved, the system will highlight the line containing the errors.

Now you need to configure the plugin to work with Swift. Copy the following line into .vimrc file just before call plug#end():

let g:syntastic_swift_checkers = ['swiftpm']

Syntastic works relying on Swift's package manager, in order to work it is necessary to launch vim in the same directory where the file package.swift is located, so if you want to open the main.swift file you need to give it the appropriate path:

$ vim Sources/main.swift

You can test Syntastic by adding random characters; When saving (:w) vim will show the line where the error is present. To make the plugin more useful, you need to add new settings to the .vimrc file.

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

The penultimate line causes the syntax verification when the file is open, while the last one avoids checking during write and quit commands. It is worth noting that in the case of virtual machines or remote servers, opening the file is slowed. Lastly, by adding to the .vimrc file these two last settings you get a symbol to report the line with error or warning.

let g:syntastic_error_symbol = "✖︎"
let g:syntastic_warning_symbol = "⚠"

Finally, if the program is large or requires a large number of libraries, it may happen that by launching vim it starts a long processing, this would give the impression of a block of the program. We can turn around this by launching the first time swift build command so to compile every library before launching vim.