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

Chis has been interested in computers and programming since owning his first NES as a child. At Collective Idea, his expertise is in web and mobile development.

Comments

  1. August 04, 2018 at 5:07 AM

    Great guide about arduino Programming inn vim. It’s really informative content for who experts in arduino programming as well as advanced professional . Thanks for sharing useful information

  2. Pavel
    March 06, 2019 at 14:19 PM

    Thanks for sharing this, Chris! I’ve been thinking about switching to Vim for Arduino programming and this is a good guide to start with. One question: how do you switch between different ports? I assume you should edit the Makefile for that. Recently I started playing with LoRa boards and noticed that usually I have two boards connected (transmitter and receiver) and I’m constantly switching between them. Do you see any easy way to do it in Vim/Makefile setup?

  3. kj6wi@yahoo.com
    kj5wt Frank Townsend
    April 04, 2019 at 6:38 AM

    Thank you, thank you. As the lead vi programmer/fixer upper for IBM’s AIX System thru 3.2 , this is exactly what is needed now that retirement is here. My daughter Beth & myself have decided to learn linux & programming for Aduinuo’s in general. Editor’s will be Cream, a much improvied vi based editor for me & Cream, E3, Emacs. Eclipse, Gerny, or Gedit for her. She is more gaming & robotics inclined while mine is microbitx HF general coverage transceiver & antenna design oriented. Much of editor choise is based on prior exposure &/or can it do what I want. For column work Scite is a good choice.