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

Setting up Vim to work with PowerShell

As an avid console user, I like being able to edit text without opening an interface that requires a mouse. For me this text editor is Vim – the extremely well regarded editor that ships with a very large number of operating systems.

My command line of choice is PowerShell, and I set up any extra commands to live in a Scripts directory inside my Windows user directory. If you’d like to do this you need to download Vim to the Scripts directory, then edit your PowerShell profile to include an alias so you can access Vim from any directory you might be in, as well as a couple of commands to make editing common files even simpler.

# There's usually much more than this in my profile!
$SCRIPTPATH = "C:\Users\Julian\Scripts"
$VIMPATH    = $SCRIPTPATH + "\vim73\vim.exe"

Set-Alias vi   $VIMPATH
Set-Alias vim  $VIMPATH

# for editing your PowerShell profile
Function Edit-Profile
    vim $profile

# for editing your Vim settings
Function Edit-Vimrc
    vim $home\_vimrc

Then you need to set Vim up in the way you like it, there are lots of sites with suggestions for how to set your vimrc file, but for now I’m just going to suggest you add a link back to PowerShell by adding the following lines:

set shell=powershell
set shellcmdflag=-command

This means that when you run the :shell command in Vim, you will actually use PowerShell itself to run commands, including all the aliases you set in your profile. I find this especially handy for writing and running F# scripts, as well as task management with Outlook – both of which I’ll write about in posts this week, but you can test this out now by running :sh or :!get-childitem | more and seeing what happens!

Storing my Pens and Pencils

I really like pens, pencils, and pretty much all the cool stationery, but I also like to keep things organized – so I have one bag which is dedicated to my artwork and stationery collection which has built up over the last 10 years or so.

The bag itself is a Toshiba laptop bag, with lots of little organization pockets on both the inside and on the front, a section for papers and other things, and of course the main laptop compartment.

Inside the bag I keep a number of pencil cases, some tear-out paper books, a folder of drawings, a couple of Moleskines and a Toshiba R400 Tablet PC.

The Tablet PC lets me draw directly on the screen, which I mainly use for producing cartoon-like graphics (I should totally post some!) and any other photo retouching work. I’m actually really fond of the ink support in Windows 7 and I love using the Wacom ‘penabled’ digitizer to produce pressure sensitive lines.

The black pencil case on the far left contains mostly pencils. There is a set of Watercolor pencils, a set of various grades of lead pencil, and some brushes for when I fancy mixing the colours. Pretty much everything in this pencil case is from WHSmiths.

The two denim Edwin pencil cases contain a wide selection of pens, too many to go through in this post, but I’ll write a follow up on some of my favourites in the future. The larger of the two also contains some spares I’ve purchased – for example I have a couple of Sailor Ink Bar pens which are unfortunately no longer being produced. These pens are sourced from a number of places including Cult Pens and JetPens and random shops I’ve found in the UK.

The two red Nomadic pencil cases are home to my favourite art pens, the supplies I usually need when doing any arts and crafts, and all the spare leads for the various pencils I use for drawing. I’ll definitely write a post about these two soon.

But that’s not even everything… you can also check out the contents of my favourite pen collection, and I’ll be sure to write some more posts in the future which will detail some of the most intersting pens, including why I like the Kuru Toga and Jetstream ranges so much.

I know I have a bit of a pen addiction, but I’m not the only one. Brad Dowdy from The Pen Addict has set up an IRC channel on irc.freenode.net #penaddict. Feel free to drop by to our ‘help group’ with your IRC client of choice, or via your web browser.