Writing and Running F# Scripts with Vim

When I’m writing software for the .NET Framework I tend to have a copy of F# Interactive open. This lets me run commands directly like so…

This is fine for simple stuff, but if you’re writing something a little more complex it’s better to write a script. In the past I’ve done this by having another copy of Visual Studio open and running the script that way. This works really well, and includes colour coding, intellisense and all the other good stuff you expect.

However there are times that having yet another copy of Visual Studio is a little heavy for just keeping track of a script. Enter Vim, the de facto command line editor for Unix and other operating systems.

  1. Set up Vim to work with PowerShell
  2. Grab yourself a copy of the F# Syntax file
  3. Save it into your Vim plugins directory
  4. Add the following lines to your vimrc file…
au BufRead,BufNewFile *.fs set filetype=fs
au BufRead,BufNewFile *.fsx set filetype=fs

Now when you create a .fsx file, you can run it directly from F# Interactive by using Vim’s shell execution feature.

:!fsi %

This will run your script in F# Interactive and present you with the results. The :! Vim command is for running the external program, and the % represents the filename of the currently open document.

Note that you’ll need to have set up F# Interactive by either adding it to the path or setting an alias in PowerShell. If you haven’t done this already, you can do it by adding the following lines to your PowerShell profile:

$FSIPATH     = "C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe"

Set-Alias fsi  $FSIPATH