Vim in Powershell

Because, why not.

And because I am stuck in Windows Land while at work.

And because I found myself playing around with Windows Power Shell. It’s not as good as Bash by a long stretch, but it is a significant improvement on the old Windows cmd almost-terminal. And, because I like to be able to move from task to task, without having to constantly switch windows, I thought I would give it a try.

The first thing to do, of course, is install a decent text editor. This is easy, a Windows binary exists for Vim so I downloaded and installed it. Then comes the fun bit.

Getting it to work in Powershell isn’t exactly difficult, but there are a few hoops that need to be jumped through and no clear overview of what and where these hoops happen to be. And that’s why I am posting this.

First, you need to allow scripts to run. This is not enabled by default because, obviously, if you have a scripting environment, the last thing you are going to want to do is execute scripts. Fortunately, this is easily checked and altered by means of the execution policy.

To see the current state of your execution policy, use:

Get-ExecutionPolicy

This will probably tell you it’s restricted, in which case you need to tell it to run scripts by typing (or pasting):

Set-ExecutionPolicy RemoteSigned

(You can also set the execution policy to AllSigned or Unrestricted, but RemoteSigned is sufficient for now.)

Next, you need a Powershell profile (why is it that Windows terms all feel like they should be wrapped in a blink tag?), which can be created by typing (or pasting):

new-item -path $profile -itemtype file -force

And now you need to edit your profile to add a Vim alias. You’ll need to do this in Notepad:

notepad $profile

And then paste the following:

set-alias vim "C:/Program Files (x86)/Vim/Vim74/./vim.exe"

# To edit the Powershell Profile
# (Not that I'll remember this)
Function Edit-Profile
{
    vim $profile
}

# To edit Vim settings
Function Edit-Vimrc
{
    vim $HOME\_vimrc
}

(Obviously, you will need to amend the Vim path name to match the reality on your system.)

And you’re done.

I’m not sure how much use I will actually get out of Vim at work as my workflow often involves copying and pasting text from one application to another – and for that Notepad++ is probably still a better fit. However, having an additional tool certainly does no harm and I can certainly see cases where having the power of Vim available will be useful. For now, I will use both and see how each fits into my workflow.

And finally, this post was brought you you with the help of Microsoft, Marlon Abastar and Julian Kay.

Flattr this!

10 thoughts on “Vim in Powershell

  1. Cody

    This post made my day and also makes working in Windows-land a whole 2% more bearable. Thanks!

  2. Ricky

    I installed gVIM on a windows 7 #1. VIM is working if I’m log in locally and can edit files within powershell using VIM.
    Rrom windows 7 #2 I connect to workstation #1 via psremoting. Once I’m connected I can see workstation#1 files and so on but the command “vim” don’t work.

    I have a powershell script on that station#1 that I would like to modify within the comand line using VIM.
    How can I get VIM to work on workstation#1 when I’m psremoting to it. note: if log in locally to workstation 1, VIM works though.

  3. Alana

    So, will this allow a script that I made using vim to be run in PowerShell too? I already allowed PowerShell to work with vim, but I’m having difficulty getting PowerShell to recognize the vim script that I made.

  4. Joe

    If you use chocolatey you can install vim with a simple `cinst vim`. Chocolatey is quite nice when you’re installing something that’s well maintained.

  5. Paul Post author

    Chocolatey looks really handy. I shall have to play around with it when I am next in front of a Windows PC. Thanks for the heads up 🙂

  6. Jonathan Weinberg

    You might enjoy looking at ConEmu. Right now, I have it: 1. Bound Quake Style. 2. Opening the following tabs – a. Cygwin b. cmd.exe x86 c. cmd.exe x64 d. PS_x86 (Std Domain User) e. PS_x64 (Std Domain User) f. PS_x86 (Elevated Domain Admin) g. PS_x64 (Elevated Domain Admin). I’ll likely also throw a few up there using PowerShell Remoting if I have the need. Few other things: huge buffer, logging, resizable window, transparency, heck it even has search! I can send you my config to get started if you’d like.

  7. Mattias Johnson

    Hate to be the one who asks nooby questions but how do you revert back everything you’ve changed in this tutorial? I’m having some troubles with Vim not starting (not sure if the error stems from this but still worth a try)

  8. Codeheadtheo

    If you did not use profiles before and want to simply delete it then:
    remove-item $profile -force

    wah lah you are done!

    or if you did you a profile:
    notepad $profile
    Once it is open, remove the added lines.

    Then wah lah you are done!

Leave a Reply

Your email address will not be published. Required fields are marked *