Skip to content

Running F# Interactive from Windows context menu

July 6, 2012

Today a question popped up on Stack Overflow on how to arrange running F# scripts from Windows context menu, but in case of abnormal termination still having opportunity to access diagnostics. Regular context menu item Run with F# interactive lacks the latter because interactive console window closes abruptly on script failure.

Although I gave an outline of the solution as Stack Overflow answer, it lacks level of details that those who want to use such feature may find useful. So, I decided to give here a more detailed description. I will show the implementation for my own work environment, which is Windows 7 Ultra x64 + VS2012 Ultra RC. Reproducing the approach for other environments may require trivial adjustments.

1. Let’s begin with spying the mechanics of stock context menu item Run with F# interactive… implementation. Let’s fire regedit in Run as Administrator mode and search through the registry for string value Run with F# interactive:

Context menu

Getting one level down to the subkey HKEY_CLASSES_ROOT\VisualStudio.fsx.11.0\shell\openRunFsi\command

Original command

we can see how exactly this context menu item is implemented on my box:

"C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0\Fsi.exe" --quiet --exec "%1"

Now the outline of an unobtrusive solution gets clear: let’s create a custom key similar to HKEY_CLASSES_ROOT\VisualStudio.fsx.11.0\shell\openRunFsi, but with our own context menu command having desired properties. In order to make interactive console window stay we can shoot out a shell cmd.exe with command key /k preventing the associated window from closing. However, it came out a problem: being offered the fsi.exe path regular cmd.exe shell cannot correctly interprete quoted string having both spaces and parentethes. In order to workaround this complication we may use as command our own shim batch script fsx.bat that will correctly process the path and pass the argument (.fsx script name) to fsi.exe. So, the context menu command is to be

c:\windows\system32\cmd.exe /Q /K %%USERPROFILE%%\fsx.bat "%1"

and the shim batch script fsx.bat that we’re going to place into the root of user’s directory would be

"C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0\Fsi.exe" --quiet --exec "%~1"

The only outstanding task to do is adding our custom keys to the registry:
and HKEY_CLASSES_ROOT\VisualStudio.fsx.11.0\shell\openRunCmd\command

Finally, let’s check out how everything sticks together. A simplest script sample.fsx will do:

printfn "Let's crash..."
failwith "...but window stays" |> ignore

Let’s invoke it through context menu new item:


getting as expected




From → F#

  1. Reblogged this on Sergey Tihon's Blog and commented:
    It is looks like really reasonable option!

  2. It doesn’t work if the compiled file (example.fsx) has a path that contains spaces, a typical case is when the Desktop resides in another drive/path (D:\Program Data\User\Desktop\example.fsx).

    • Thank you for pointing out to the problem. The fix is quite trivial though – just substitute “%1” in the shim for “%~1”. I’ve edited the original post accordingly.

  3. Can you modify the registry key to allow for spaces in the path of the fsx.bat file:

    D:\Program Data\BAT\fsx.bat

    Using quotation marks doesn’t work:


    C:\Windows\System32\cmd.exe /Q /K “D:\Program Data\BAT\fsx.bat” “%1”

  4. I can only wonder why the suggested placement of the shim into your user directory referred with USERPROFILE environment variable may be not good enough for you πŸ˜•

    Anyways, you should be OK with setting the key to

    C:\Windows\System32\cmd.exe /Q /K ("D:\Program Data\BAT\fsx.bat" "%1")

Trackbacks & Pingbacks

  1. F# script reminder | Rodhern

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: