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!