Vim and Salesforce development

Since I switched over to using vim as my primary mode of Salesforce development, I’ve been asked several times how I’ve configured vim.  Well, it’s about time I show the man behind the curtain.  My primary vim config file is quite large now, but I’ve condensed it down to the parts that I think are most pertinent to Salesforce development.

Highlighting

Now vim is great for editing but where I think it shines the most is in highlighting.  I’ve taken the standard java vim file and have modified it for use with the Apex language.  To use it, download the apex.vim file and place it in your syntax directory for vim.

Local

To make it work for just your user you can place the file in ~/.vim/syntax/ but you will need to also symlink (or copy) the html.vim file into that directory.

ln -s /usr/share/vim/vim73/syntax/html.vim ~/.vim/syntax/html.vim

Global

To make it work for all users on the system you can just place the file into your global syntax directory.  For fedora and probably most unix systems it is /usr/share/vim/vim73/syntax/

.vimrc

Now that we’ve got highlighting installed, lets get vim to use it.  First we need to create our backup and swap directories.  We do this to keep extra files out of our Salesforce directory so that we don’t try to deploy them (or commit them to our repo).

mkdir ~/.bak ~/.swp

Now we can add / replace the following options into our .vimrc file

You might need to edit the values for tabstop, shiftwidth and softtabstop for your coding standards but 4 is what we use.

This entry was posted in Development, Salesforce and tagged , , , , , . Bookmark the permalink.
  • Jack

    I think you can avoid the hassle of the apex.vim file and the symlink by just using syntax=java, and fixing the one change from java.vim in the apex.vim file, from your .vimrc that sets up syntax for the filename extensions:

    au BufEnter *.cls set syntax=java tabstop=4 shiftwidth=4 softtabstop=4 nowrap binary noeol
    au BufEnter *.trigger set syntax=java tabstop=4 shiftwidth=4 softtabstop=4 nowrap binary noeol
    au BufEnter *.cls syn region javaString start=+’+ end=+’+ end=+$+ contains=javaSpecialChar,javaSpecialError,@Spell
    au BufEnter *.trigger syn region javaString start=+’+ end=+’+ end=+$+ contains=javaSpecialChar,javaSpecialError,@Spell

    You should set the “binary” and “noeol” on a filetype basis, too, or else other files (like the apex.vim I just copied from github) will lose their newline at end of file. It’s just broken by design metadata that doesn’t have a newline at end of file. Pretty much every other text file should always have one.

  • Thanks for the feedback. I have the intentions (probably very delayed intentions) of doing a little more formatting within the apex.vim file to handle some of the other Apex keywords. It’s just not at the top of my priority list.

  • Rocky Assad

    You’re my hero

  • Anonymous

    Hi Patrick,

    I really loved your blog 🙂 thanks a lot. Have a question regarding vim editor with force.com
    How are you able to handle the developer console logs in Vim editor, as this feature is already available in the current developer console. Just curious to know.

  • I have no way currently to handle developer console logs. There are some new additions to the ToolingAPI that are coming out soon that may help to pull down data in a format a little easier to deal with than the plain debug log text format.