The other day I made my first library (a 7-segment display controller) for my new Arduino Uno, following two nice tutorials. They both mention that it’s a good idea to make a keywords.txt file for new libraries, which gives hints to the Arduino IDE’s syntax highlighter. However, neither gives a thorough explanation of format of that file. I thought I would document my findings.
The built-in keywords are defined an a simple text file. On my computer, this lives at /Applications/Arduino.app/Contents/Resources/Java/lib/keywords.txt. Here’s how it starts:
# LITERAL1 specifies constants HIGH LITERAL1 Constants LOW LITERAL1 Constants
The interesting thing here is that there are three fields which get parsed. Only the first two are useful.
- The keyword to highlight
- The type of keyword it is. This really just determines the color, but most people seem to use the following convention:
- KEYWORD1 Classes, datatypes, and C++ keywords
- KEYWORD2 Methods and functions
- KEYWORD3 setup and loop functions, as well as the Serial keywords
- LITERAL1 Constants
- LITERAL2 Built-in variables (unused by default)
- Documentation page. This is used by the ‘Help<Find in Reference’ menu item. For example, the reference for HIGH in the example above would be file:///Applications/Arduino.app/Contents/Resources/Java/reference/Constants.html.
By default, Arduino 1.0 colors all the KEYWORD types orange, and all the LITERAL types blue. These defaults are set in the /Applications/Arduino.app/Contents/Resources/Java/lib/theme/theme.txt. Here’s the relevant snippet (the comments seem to be inaccurate or outdated):
# TEXT - KEYWORDS # e.g abstract, final, private editor.keyword1.style = #cc6600,plain # e.g. beginShape, point, line editor.keyword2.style = #cc6600,plain # e.g. byte, char, short, color editor.keyword3.style = #cc6600,bold # TEXT - LITERALS # constants: e.g. null, true, this, RGB, TWO_PI editor.literal1.style = #006699,plain # p5 built in variables: e.g. mouseX, width, pixels editor.literal2.style = #006699,plain
Just change any of the hexadecimal colors. I like the following:
editor.keyword1.style = #cc6600,plain editor.keyword2.style = #993300,plain editor.keyword3.style = #993300,bold editor.literal1.style = #006699,plain editor.literal2.style = #0099CC,plain
If loop and setup aren’t showing up bold, you may be using Monaco, which doesn’t have a bold style. I recommend using another fixed-width font which does have a bold style, such as DejaVu Sans Mono. This can be set in the Arduino preferences file, ~/Library/Arduino/preferences.txt:
editor.font=DejaVu Sans Mono,plain,10 editor.antialias=true
Make sure the Arduino IDE is not running, as it overwrites the preferences file upon exit.