Setting up Vim for Arduino
syntax highlighting, in-editor compile/deploy, & serial port monitoring
If you’re used to Vim, you’ll soon find Arduino’s IDE to be somewhat cumbersome and counter intuitive.
To make Vim work with your Arduino sketches, there are essentially three aspects we need to consider; syntax, compiling and uploading your code to the micro controller, and access to the serial terminal.
The Requisites
This tutorial assumes you’re on OSX and using Homebrew as your package manager. You’ll also need to have Python to painlessly install ino, an Arduino CLI that VIM will be using to compile and upload sketches.
Installing Python, if you don’t have it yet is pretty straight forward through Homebrew via
$ brew install python
Handling Syntax Highlighting
Arduino sketch files are essentially C++ code and can in fact be highlighted by having the below directive in your ~/.vimrc config.
au BufRead,BufNewFile *.ino,*.pde set filetype=c++
Note that Arduino sketches from version 1.0 have an *.ide extension. Versions before 1.0 have *.pde extensions.
However, what you want is for Vim to trigger highlighting for Arduino specific keywords such as digitalWrite for example.
Fortunately, Sudar Muthu has graciously shared a comprehensive Arduino.vim syntax file on his github repo. Let’s go get it!
$ git clone https://github.com/sudar/vim-arduino-syntax.git
Then let’s copy the files into their respective .vim sub folders.
$ cd vim-arduino-syntax
$ cp -r ftdetect/ ~/.vim/ftdetect
$ cp -r syntax/ ~/.vim/syntax
$ cp -r indent/ ~/.vim/indent
At this point, if you load an Arduino sketch into Vim, you’ll see all that glorious C++/Arduino highlighting and you’ll be able to fly through the code with your swift and lightning fast VIM skills.
Compile and upload your sketches from within VIM
Ino is a command line toolkit that you can use with your Arduino if you prefer not to use the Arduino IDE and instead, write sketches using your own text editor and compile/upload them using the command line. We’ll be using Ino in this case to tightly integrate the compile and upload process into Vim.
Since you already have Python on your machine, all you have to do is use Python’s easy_install command.
$ easy_install ino
If you’re using a board other than the Arduino Uno, you’ll need to do some additional configuration to ino using the simple directives found here.
Serial Monitor Support
To enable serial monitor support for ino, you’ll need to install the picocom dumb terminal emulator. That’s easy enough with Homebrew.
$ brew install picocom
Stitching it all together
The last step would be to properly integrate ino with Vim. Fortunately, there’s a plug-in just for that provided to us by Jon Plaut.
To use it though you should be using the Pathogen plug-in manager for Vim.
If you aren’t using it yet, it’s simple enough to install:
$ mkdir -p ~/.vim/autoload ~/.vim/bundle$ curl https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim > ~/.vim/autoload/pathogen.vim
Then go into your ~/.vim/bundle folder and clone the plug-in from the repo.
$ cd ~/.vim/bundle$ git clone https://github.com/jplaut/vim-arduino-ino.git
Wrapping up
Now that you’re using ino, you’ll need to follow a bit of convention. An Arduino sketch project needs to be in its specific folder which you’ll need to prepare beforehand to work with ino.
$ mkdir ~/my_project
$ cd ~/my_project
$ ino init
Your source files should now be placed within the src folder. Arduino libraries go into the lib folder.
Now go open up your sketch in Vim and use the following commands to compile and upload:
<Leader>ac — Compile the current sketch.<Leader>ad — Compile and deploy the current sketch.<Leader>as — Open the serial monitor in screen
Alternatively, you can view the serial monitor from within your own shell,
$ ino serial
or in Vim.
:!ino serial
There you have it, Vim goodness to go with your Arduino code!
Jay is founder and CEO of PROUDCLOUD, a full stack and deep tech development lab in Manila that builds for clients worldwide, through web platforms, the internet of things, machine learning, and the blockchain.