How to program the Arduino using vim

The Arduino IDE leaves a lot to be desired. Granted, it’s easy for beginners to build and upload sketches, but as a text-editor it makes you want to smash your keyboard against your monitor. Repeatedly.

Files open in new windows instead of a tab, and there are no advanced features like auto-complete; did I mention it looks like a relic from the Windows 95 era?

This article describes how you can free yourself from the shackles of the Arduino IDE and program your Arduino using vim.

arduino-makefile

arduino-makefile by Sudar Muthu is a great project that allows you compile, upload, and monitor the serial port using a simple makefile.

How simple you ask? 4 lines! Seriously. And one of those is blank!

Let’s work through an example together, from scratch. Let’s create a makefile for the legendary Blink sketch.

1. Install the Arduino IDE

The Arduino IDE is still required as arduino-makefile uses the software it provides. Download the latest version and extract it:

1wget -O arduino.tar.xv http://arduino.cc/download.php\?f\=/arduino-1.6.8-linux64.tar.xz
2mkdir ~/.arduino_ide
3tar xf arduino.tar.xv -C ~/.arduino_ide --strip-components=1

2. Install arduino-makefile

To install arduino-makefile we’ll use git to clone the project:

1git clone https://github.com/sudar/Arduino-Makefile.git ~/.arduino_mk

You should now have the Arduino IDE installed in ~/.arduino_ide and arduino- makefile installed in ~/.arduino_mk if you’ve used the same paths as I have above.

3. Create a makefile

Create a folder for the project and copy the Blink sketch to that folder:

1mkdir -p ~/projects/blink
2cp .arduino_ide/examples/01.Basics/Blink/Blink.ino ~/projects/blink

Create an empty makefile in the same project folder:

1touch ~/projects/blink/Makefile

Open the Makefile in vim and add the following:

1ARDUINO_DIR = /home/jon/.arduino_ide
2ARDMK_DIR = /home/jon/.arduino_mk
3BOARD_TAG = uno
4
5include $(ARDMK_DIR)/Arduino.mk

That’s unbelievably simple. All you do is set some variables, and arduino- makefile takes care of the rest.

ARDUINO_DIR
Path to the Arduino IDE
ARDMK_DIR
Path to arduino-makefile
BOARD_TAG
The name of the Arduino board we’re programming. Use make show_boards to get a list of available boards.

There are many other variables that can set. One that I use often is ARDUINO_LIBS which specifies the Arduino libraries a project requires. Checkout the arduino-makefile documentation for a complete list.

The last line in the Makefile includes arduino-makefile. You need this!

4. Build, upload, and monitor

There are three commands you need to know, which you can type in a terminal:

make
Build the sketch
make upload
Upload the sketch
make monitor
Monitor the serial port

Try it out. It’s freakin' awesome!

Integrate with vim

We’re now able to write code using vim and use the command-line to build, upload, and monitor our sketch.

Because we’re using a makefile, and vim has built-in support for makefiles, integrating with vim requires no effort. None. Nada.

:make
Build the sketch
:make upload
Upload the sketch
:make monitor
Monitor the serial port

When you run :make and there are errors, vim will go to the line where the first error is. The commands :cnext and :cprevious will cycle through the errors.