Arduino From Vim
Arduino is awesome, but using their IDE is uncomfortable for people that would prefer vim or emacs. There is an option in the IDE to allow you to use an external editor, but you still need to use the GUI to get access to the compile & upload tools. Here’s my setup, for a vim-only setup (would be similar for emacs).
Goals My goals for this project are simple:
- Proper syntax highlighting
- Compile & upload code from within editor
Syntax Highlighting I use Johannes Hoff’s syntax file for this. Just put arduino.vim in your $HOME/.vim/syntax directory.
Compile & Upload Code (From The Command Line) In order to compile and upload the code from within vim, I first needed to figure out how to bypass the IDE and access the required toolchain from the command line. I followed Martin Atelier’s guide to figure it out. I won’t repeat his content here (you’ll want to get the latest version from his blog), but one thing not mentioned there was that I had to include Wiring.h at the top of my header files (not the main PDE). When make runs, it prepends your PDE with #include <WProgram.h>, which gives you some nice Arduino stuff (like the constants HIGH, LOW, INPUT, and OUTPUT). This #include also includes stdlib.h, which gives you lots of C++ stuff (notably, types like uint8_t). To use any of this in a bundled module, you can just drop #include <Wiring.h> at the top of the header. After that, make and make upload were able to compile and push the code to my Arduino.
Compile & Upload Code (From Vim) Once the toolchain works from the command line, it’s just a matter of deciding what vim shortcut you want to trigger it with. I have am to make, au to upload, and aa to do both. Drop something like this in your $HOME/.vimrc:
map <silent> <LocalLeader>am :!make<CR>
map <silent> <LocalLeader>ac :!make clean<CR>
map <silent> <LocalLeader>au :!make upload<CR>
map <silent> <LocalLeader>aa :!make && make upload<CR>