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!

  1. Pingback: Writing and Running F# Scripts with Vim | Julian Kay's Blog

  2. Pingback: Outlook Tasks in PowerShell and Vim | Julian Kay's Blog

  3. You can also just setup an “Alias” for it

    in $PROFILE i have add it as:

    set-alias vi “C:\Program Files (x86)\Vim\vim73\vim.exe”
    set-alias vim “C:\Program Files (x86)\Vim\vim73\vim.exe”

    so, now I just call it as:

    vim myfile.ps1 (Voila!)
    vi myfile.ps1

    By the way, your profile can be anywhere, in my case is at:

  4. Yup! I showed that in my example above :)

    You can also set up a function in your profile to enable you to pipe into Vim like so…

    Function vf ()
        $filepaths = $input | Get-Item | % { $_.fullname }
        vim $filepaths

    this lets you do stuff like

    gci *.ps1 | vf

    Which will open each PowerShell script in a separate buffer in Vim.

  5. When I launch powershell from vim, ps fails to load my profile.ps1 and says that I haven’t enabled scripts on my system. But when I run ps regularly it runs scripts fine so what’s wrong with it from vim?

  6. I figured it out. The default Powershell is 64 bit and Vim launches the 32 bit version. You need to set the execution policy for both separately.

  7. Hi,
    I am very new to Powershell but I have some experience with VIM. I do the setup and VI works fine. But when i restart my computer the vi is no longer accepted. It appears as though my powershell will not hold those setting after a restart.

    I skipped the $PROFILE part because I did not understand how to execute that part. Do i need to create a script with that function inside? Do I write the function directly into console?

    Please help. Thank you for the walkthrough I am very excited to get VI running on my Windows PC. I am running Windows XP Pro SP3 32 bit if that matters.

  8. Pingback: Windows PowerShell + Vim | diogogmt

  9. Thank you very much. You messed up though, (you probaby have several $SCRIPTPATH) but you wrote

    $SCRIPTPATH = “C:\Users\Julian\Scripts”
    ^No S
    $VIMPATH = $SCRIPTSPATH + “\vim73\vim.exe”
    I Kept getting errors cause I copy pasted, and just changed my name.

    Thank you very much though, I am new to ps1, but am used to bash so vim helps.

  10. Thanks, Julian, I’ve always wondered how to get my PowerShell aliases to work in vim. This is quite handy!

  11. Pingback: Will Neovim rock on Windows? - Julian Kay

  12. Pingback: Being productive on Windows 10 - Julian Kay

  13. That is great advice, i love Vim but could only using it on linux machines, now, i will be able to use it on win7 with the help of a few of your tweaks!

Leave a Comment...