Arduino with Vim
Categories: [ DIY/Arduino | IT ]
If you are used to Vim as a text editor, the Arduino IDE is terrible to use. After a little bit of fiddling, here are my tips to program an Arduino using Vim.
Makefile
To compile the Arduino software, I found the Makefile by Alan Burlison and I modified it to suit my needs. You can get the modified version and use it at your own risks.
It has been modified as follows:
- Support for arduino 0022 only (but you can change the version number in the Makefile). Versions prior to 0018 are not supported.
- Binaries are located in
/usr/bin
. - The board is by default
atmega328
. - The programming protocol is
stk500v1
and the speed is 57600 bauds. - The terminal emulator is
xterm
and the serial communication software ispicocom
. - The serial port is
/dev/ttyUSB0
.
All these default values can be changed in the Makefile.master
file.
Moreover, the following changes have been made:
- After killing the serial communication software, sleep for 1 second
- Before starting the programmer, the DTR signal is triggered with
stty
to reset the board (programming would otherwise not be possible) - Warnings remain warnings (GCC's
-Werror
flag has been removed, because I had a weird warning that I don't know how to fix) - The sketch's
.pde
file is hardlinked into thebuild
directory, and is#include
d in sketchname_pde.cpp instead of copied into it. This allows GCC to report the errors on the proper line of the.pde
file as well as report the proper path for this file in its error messages (when compiling sketchname_pde.cpp, GCC is in thebuild
directory; since it would include../sketch.pde
, the error messages relative to this file would point to a file name starting with../
, but since Vim is in the parent directory, the path tosketch.pde
would be wrong and Vim's QuickFix fails to find the file). - Linking is made by
gcc
instead ofg++
because of a bug in linking tolibm
(which is needed if you want to do floating point computations, so it links against it by default). - A
vim:
line has been appended to make Vim recognize the file as a Makefile
To use this Makefile, put Makefile.master
into your sketchbook
directory,
and create in your sketch's directory a Makefile
which contains include
../Makefile.master
. If you want to add extra libraries (here RF12, GLCDlib
and Ports), it looks like this:
LIBS = RF12 GLCDlib Ports LIB_DIRS = $(addprefix ../libraries/,$(LIBS)) include ../Makefile.master
Typing make
inside the directory where the Makefile
resides builds the
sketch. make upload
uploads it, make monitor
starts the serial
communication software on the serial port and make upload_monitor
does both
sequentially. Finally, make clean
cleans up the build
directory created
when buidling the sketch.
Arduino Syntax File for Vim
Get arduino.vim by
Johannes Hoff and put it in your ˜/.vim/syntax/
directory. It will let Vim
recognize .pde
files are as arduino
, turning on C++ syntax highlighting
plus Arduino-specific keywords.
Tabs
Vim allows to open multiple tabs: run vim -p *.*
in your sketch's directory,
and it opens all the files (except the Makefile
) in separate tabs. Use
Ctrl-PgUp
and Ctrl-PgDown
to switch between tabs.
To close all tabs at once, you can use :tabd q
, but this is tedious, so I
made a shortcut: Add command Q tabd q
to your .vimrc
and you can close all
the tabs at once using :Q
. You can do the same with :w
, :wq
and :x
if
you want.
Type :help tab
to learn more about tabs (especially, :tabe xyz
opens file
xyz
in a new tab. I thought you'd be glad to know).
QuickFix
You can callmake
from within Vim by typing :make
but that's tedious also.
I added the following mappings to my .vimrc
:
imap <F9> <ESC>:make<CR> map <F9> :make<CR>
This lets F9
calls :make
(whether you're in command mode or in insert mode). You
may want to add set autowrite
in your .vimrc
so that your files are
automatically written when you call make
.
Now, at least in Debian, after the call to :make
has completed, when you return to your file's
screen Vim redirects you to the file that contains the first error in make
's
output. In my setup, this is a warning in a file belonging to Arduino's core,
which I don't want to modify. I could of course press Ctrl-O
to return to
the file I was editing just before running :make
, but that's too much work.
What I wanted is to be able to let Vim jump to the files containing warnings
and errors, but to force it to ignore system files. The solution is then to
add to your ˜/.vimrc
the following (all in one line, without spaces after the
equal sign):
autocmd Filetype arduino set errorformatˆ=\%-G../libraries\%.\%#,\%-G../../libraries\%.\%#,%-G/space/mweber/tmp/arduino-0022\%.\%#
It requires some tuning, because your Arduino directory is probably not in
/space/mweber/tmp/arduino-0022
, but since you're smart, you have already
understood how to adapt it to your setup.
Please note that to get this setup to work properly, you need to append //
vim:ft=arduino
to all of the .pde
, .cpp
and .h
files belonging to your
sketches, otherwise the Vim will not do the ignoring above. And for this to
work, modelines must be enabled by adding set modeline
to your
˜/.vimrc
.
Finally, since you opened all your files in tabs already, tell Vim not to open
the file containing a warning/error in the current tab, but rather in the tab
that already contains the file. You do this by adding set switchbuf=usetab
in your ˜/.vimrc
.
In short, your ˜/.vimrc
has now these extra lines:
command Q tabd q command W tabd w set modeline set autowrite set switchbuf=usetab imap <F9> <ESC>:make<CR> map <F9> :make<CR> autocmd Filetype arduino set errorformatˆ=\%-G../libraries\%.\%#,\%-G../../libraries\%.\%#,%-G/space/mweber/tmp/arduino-0022\%.\%#