Today I'm bringing together two of my favourite things. Powershell, and vim.

Don't get me wrong, the new ISE is fancy. It's the tool if you intend to learn how to use powershell, or if you want to debug a hairy script. But you can't code in another programming language and use powershell's awesome feature set from within the ISE. This is something vim is great at; shell integration.

As a bit of a Linux fan I was more than happy to turf the age-old dos shell from vim on windows anyway. Replacing it with shiny powershell v2 goodness is a breeze. Just add the following to your _vimrc.

[sourcecode language="bash"]
set shell=powershell.exe
set shellcmdflag=-Command
set shellpipe=|
set shellredir=>
[/sourcecode]

Now we're all set to perform some vim/powershell tricks...

[sourcecode language="powershell"]
# go fetch the lines in a text file that match expr and insert the line into the current buffer
:r ! gc ~someApp.log | where { $_ -match 'expr' }

# change the readonly flag of the current file
:silent ! set-itemProperty %:p IsReadOnly $false

# select properties on clr objects and dump as csv into the buffer
:r ! ps | select Name, Path | convertTo-csv

# read a csv file and output the only 'name' column into the buffer
:r ! import-csv test.csv | select name
[/sourcecode]

And if you haven't already done so, rig up syntax highlighting, indentation and filetype detection for ps1 files themselves, thanks to Peter Provost:

To mimic the ISE auto completion we can turn on some newer vim features. Vim has built-in keyword completion for a variety of languages. It also has a clever fallback; it can parse syntax highlighting files for language keywords and other important words. To do this, without disturbing other built-in language completion, the following addition to the vimrc works well:

[sourcecode language="bash"]

filetype plugin on
if has("autocmd") && exists("+omnifunc")
autocmd Filetype *
if &omnifunc == "" |
setlocal omnifunc=syntaxcomplete#Complete |
endif
endif
[/sourcecode]

While this is nice, the powershell syntax highlighting file is written too cleverly for omni completion to be truly useful. A quick and dirty hack is to include all the standard cmdlets names in the highlighting file. Perhaps I'll get my head around the vim scripting required to produce ps1complete some other rainy day. For now, we can just  bring in the entire set of cmdlet names into syntax/ps1.vim:

[sourcecode language="powershell"]

:r ! get-command | where { $_.CommandType -eq 'Cmdlet' } | Select Name

[/sourcecode]

(I'll leave it as an exercise for the reader to optimize the powershell required to only bring in the distinct words from the list of cmdlet names as a space delimited string)

[sourcecode language="bash"]
syn keyword ps1Cmdlet Add-Content Add-History...... #etc
[/sourcecode]

If your learning powershell, its still best to use the ISE, this tweak is only useful if you know what you're looking for. For those comfortable with powershell and vim, this can allow you to stay in your favourite editor longer, before requiring the ISE.