About | Links | Scripts
Sharing linux/windows scripts and tips

Vim and Powershell

March 31, 2019 — Jesse Harris

Back in the days when Powershell was just a young pup there were a few Vim users contributing to Vim via plugins to make writing powershell a nicer experience. Syntax highlighting, auto filetype detection, snippets and other quality of life things.

Since then, the release and rise of VSCode with it's strong Powershell integrations have drawn most developer types away from Vim and new QOL features have been few and far between.

I haven't given up on using Vim for powershell however for a few reasons:

  • Battery life.

I spend most of my time running off laptops, and VSCode and other electron based apps are designed for development speed first, execution speed second. Running a terminal based app all day vs running an electron app can make a serious difference to your battery life.

  • Updates too often.

Every update to a tool like VSCode broadens it's audience and makes it a great tool for more people. This is great to make an app with thousands of features that each user will use a tiny sliver of those features. I, on the other hand don't need or want 99% of those features.

  • Focusing your skills

I'm pretty much a generalist in skills in IT, but there are a few tools were I keep my focus to be as effective as possible. Using Vim well is one of them

So with all this, I have been searching for a while for a plugin to make some QOL improvements to Vim. A couple of months ago I stumbled upon ale a general plugin that can be adapted to any language. It started out for linting as the name implies, but it had grown to support fixing and completion too.

At the time, there was an issue raised to add PSScriptAnalyzer as a linter, and so I decided to see if I could understand how to add a linter to ale.

I didn't know the VimScript language and trawling through the ale codebase didn't make much sense to me. The project has some developer documentation, however it assumes pre-knowledge of VimScript. That's when I did something out of character. I read a book. Learn VimScript The Hard Way by Steve Losh. It was a fun and enjoyable read, especially following along with the exercises. I highly recommend reading it, even if you only have an interest in using Vim.

Toward about half-way through reading the book, I felt confident enough to have another crack at the PSScriptAnalyzer linter. This time the codebase made much more sense to me and I was able to get a nicely working prototype done in a morning.

Since then, I've written some tests, documentation and created a pull request which was merged today on the ale project.

To use it, you just need to install the ale plugin. It defaults to using PowerShell core, so if you want to use powershell.exe you will need to read the help: :help ale-powershell to see the variable to override this setting. Of course you will also need to have the PSScriptAnalyzer module installed.

Finally, there is one issue you can encounter particularly on windows (btw, I've used it fine on Linux and MacOS), if you launch Vim using powershell.exe but ale is using pwsh.exe, Vim will pass it's inherited environment variables to it's child processes. This can lead to an issue where the variable $Env:PSModulePath contains paths for powershell.exe and not pwsh.exe, therefore if you don't have the PSScriptAnalyzer module installed in both shells, ale won't be able to do any linting for you.

Tags: powershell, vim, ale