How to Add Jenkinsfile Syntax Highlighting to Vim

If you’ve ever tried to edit a Jenkinsfile with Vim, you’ve probably noticed that Groovy syntax highlighting is not enabled for this file type by default.  The reason for this is that although Vim is smart enough to enable syntax highlighting of Groovy files by default, it does not know that a Jenkinsfile is a Groovy source file.

Don’t worry though, there is a simple Vim configuration that you can apply which will effectively tell Vim that a Jenkinsfile is to be considered as a .groovy file.

To associate a Jenkinsfile with the Groovy programming language in Vim, you can simply run the following one-liner within your shell of choice (Bash in my case):

echo 'au BufNewFile,BufRead Jenkinsfile setf groovy' >> ~/.vimrc

What does this do, exactly? Well, with Vim you can modify your configuration file to associate a syntax for a file name that matches a given pattern. For example, if you wanted to tell vim that a file ending in .foo should always be recognized as a shell file (and highlighted accordingly, you would add the following to your ~/.vimrc file:

au BufNewFile,BufRead *.foo setf sh

As you can see, this can be modified to suit your needs whenever a file is not automatically recognized as a certain type within Vim.