Arduino Programming in Vim

How to build Arduino projects without ever leaving your favorite editor

Working Laptop by Unsplash is licensed by Creative Commons CC0

I’m ramping up on an Arduino project at Collective Idea. The Arduino IDE is a great place for beginners, and I’ve used it in the past. This time around, though, I wanted to stick with my favorite development tool, Vim. It was a bumpy start trying to write Arduino code in Vim, but I have a nice workflow now. Here’s what I learned getting there.

What Doesn’t Work

My first Google search for “vim arduino” pointed me to two Vim plugins for Arduino development: tclem/vim-arduino and jplaut/vim-arduino-ino. I didn’t have luck getting either of these plugins to compile code and upload it to my Arduino board.

I’ll keep an eye out for updates on these projects, but for now, I’m avoiding them.

What Works Well

After scouring through a few blog posts, I was able to find a few helpful tidbits.

To get syntax highlighting and code snippets, I found two plugins from Sudar: sudar/vim-arduino-syntax and sudar/vim-arduino-snippets. Both of these worked out of the box for me (I already had UltiSnips installed).

Building code and uploading it to a board is handled with Arduino-Makefile. I cloned this repository to a location in my home directory, and sourcee it from a Makefile in my project:

$ git clone https://github.com/sudar/Arduino-Makefile ~/.arduino_mk
# Makefile
ARDMK_DIR = $(HOME)/.arduino_mk
include $(ARDMK_DIR)

From the command line, I can compile and upload a .ino file directly to the board:

$ make upload

This same command can be called in Vim with :make upload.

And finally, to quickly navigate code within my project, I’m using Ctags. To generate the Ctags, I’ve “borrowed” and modified a snippet of code from David Crosby, updating my Makefile to look like this:

ARDUINO_DIR = /Applications/Arduino.app/Contents/Java
ARDMK_DIR = $(HOME)/.arduino_mk
CTAGS_PATH = $(ARDUINO_DIR)/tools-builder/ctags/5.8-arduino11/ctags

include $(ARDMK_DIR)/Arduino.mk

.PHONY: ctags
ctags:
	$(CTAGS_PATH) -f tags.cpp $(shell find . -name "*.cpp" -o -name "*.h")
	$(CTAGS_PATH) -f tags.ino --langmap=c++:.ino $(shell find . -name "*.ino")
	cat tags.cpp tags.ino > tags
	sort tags -o tags
	rm -f tags.*
	

In the Ctags task I use the ctags binary provided by the Arduino IDE installation. There’s no need to install it system-wide if I don’t need it elsewhere.

Now when I run :make ctags in Vim, then place my cursor over a variable or function name and press <C-]>, my cursor jumps to the definition of that variable or function.

With this configuration, I can do everything I need to do for Arduino development, without ever leaving Vim.

Photo of Chris Rittersdorf

It was gaming on the NES as a child that first interested Chris in computers and programming. He cut his programming teeth writing Visual Basic in school and then graduated from Kettering University. Now he plays around with writing retro games and chip tunes on mobile devices. At Collective Idea his expertise is in web and mobile development and teaching us all new things through pair programming.

Chris and his wife Sarah live in Grand Rapids with their two cats. When not coding, Chris is probably either remodeling his house or wrenching on a Volvo 240. In his words, “they’re fun to fix, and fancy to drive.”

Comments:


Post a Comment

(optional)
(optional — will be included as a link.)