MiguelBarro / setpwsh

vim plugin for powershell integration
Apache License 2.0
2 stars 0 forks source link

setpwsh

A Vim plugin to improve powershell integration.

Motivation

Using powershell as windows 'shell' with the defaults settings (see dos-pwsh) is a poor experience:

This plugin rigs the 'shell', 'shellcmdflag' and other related options to workaround the above issues.

Installation

An obvious precondition is having powershell core installed. Though, on windows it will work with the builtin powershell desktop if core is not available or the user enforces it (see Usage).

In order to install powershell core (pwsh) I advise: • Windows. Use winget:

    > winget install Microsoft.Powershell

• Ubuntu. Use snap:

    $ sudo apt install snapd
    $ sudo snap install powershell --classic

• MacOs. Use brew:

    $ brew install powershell/tap/powershell-lts

This plugin can be installed using any popular plugin manager (vim-plug, Vundle, etc...) but vim plugin integration is extremely easy in later releases (version8.0 introduced package support):

Once installed the :SetPwsh command must be used to modify the 'shell' options. The most common place to do it is the .vimrc file. Add the following lines:

    packadd setpwsh
    SetPwsh

Usage

There is only a single command:

    :SetPwsh [Desktop | FtpFromWsl | SshFromWsl]

This command will modify 'shell' and related options to use the powershell. It admits the following argument flags that are only meaningful in windows:

Bang commands

Once 'shell' and related options are modified by the :SetPwsh command, the :!cmd will respond to powershell as on a terminal. For example:

    :!Get-Item "C:\Program Files"

will work properly.

Is possible to read powershell pipeline output into the current buffer using :read!. For example:

    :read !1..5 | \% { [char]($_+96) }

will fill the current buffer with:

 1  a
 2  b
 3  c
 4  d
 5  e

we can use a filter command to modify the buffer. The plugin allows creating powershell filters where the buffer input is translated into a powershell pipeline input. The $_ automatic variable will match each input line. For example in the above buffer doing:

    :1,5!"-->$_<--"

will turn the buffer into:

 1  -->a<--
 2  -->b<--
 3  -->c<--
 2  -->d<--
 5  -->e<--

if we do not want to filter the buffer but running a powershell pipeline with it as input we can use :write_c. For example:

    :1,5w !"-->$_<--"

will execute the same commands without modifying the buffer.

The same applies to the system() function. For example:

    :echo system('1..5 | % { [char]($_+96) }')

will show:

 a
 b
 c
 d
 e

For a more detailed and comprehensive usage explanation refer to the actual plugin docs.