Highlight Strings in Cucumber Features with Vim

I am currently working on a project that is using Cucumber for its system/integration testing, and I am using MacVim as my primary editor. MacVim comes with Tim Pope’s excellent vim-cucumber plugin pre-installed, so you get syntax highlighting of .feature files right out of the box.

After having used TextMate to edit Cucumber features in the past I was a bit disappointed that double-quoted strings were not being highlighted in Vim. I looked around in the plugin code a bit, and the best I can tell it is only configured to highlight multi-line strings (which are triple-quoted).

Vim allows you to extend or override syntax highlighting by adding a file in the ~/.vim/after/syntax directory that corresponds to the file type you want to modify. After spending some time with the Vim Syntax documentation and the source code of vim-cucumber’s syntax file I wrote the following:

syn region cucumberGivenString start=/"/ skip=/\\"/ end=/"/ contained containedin=cucumberGivenRegion,cucumberGivenAndRegion,cucumberGivenButRegion
syn region cucumberWhenString start=/"/ skip=/\\"/ end=/"/ contained containedin=cucumberWhenRegion,cucumberWhenAndRegion,cucumberWhenButRegion
syn region cucumberThenString start=/"/ skip=/\\"/ end=/"/ contained containedin=cucumberThenRegion,cucumberThenAndRegion,cucumberThenButRegion
 
hi def link cucumberGivenString cucumberString
hi def link cucumberWhenString cucumberString
hi def link cucumberThenString cucumberString

When placed in a ~/.vim/after/syntax/cucumber.vim file this results in double-quoted strings in Cucumber steps being highlighted by Vim.

Syntax highlighting in Vim