Skip to content

MiguelBarro/setpwsh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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:

  • Commands perfectly valid in terminal cannot be used due to the defective way windows CRT parses command lines. Powershell binary uses also this CRT parsing strategy. For example from cmd:

        > pwsh -C Get-Item "C:\Program Files"
    

    pwsh.exe will receive as arguments (according with CRT rules):

        Arg 0 is <Get-Item>
        Arg 1 is <C:\Program Files>

    and fails due to the lack of quotation marks. This issue does not plague pwsh for linux or mac.

  • Powershell heavily relies on pipelines to join different cmdlets. It will be very convenient to profit from vim's integrated filtering capabilities to feed those pipelines.

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):

  • A [vimball] is distributed by www.vim.org. Installation is as easy as sourcing the vimball file:

      :source setpwsh.vba

    so is uninstall:

      :RmVimball setpwsh.vba
  • The github repo can be cloned direcly into the $VIMRUNTIME/pack directory as explained in matchit-install. Though using this approach many useless files in this repo will be installed too.

  • Use getscript plugin to automatically download an update it. Update the local $VIMRUNTIME/GetLatest/GetLatestVimScripts.dat adding a line associated with this plugin.

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:

  • Desktop Use powershell desktop instead of powershell core.
  • FtpFromWsl Sets up netrw global options to rig wsl ftp.
  • SshFromWsl Sets up netrw global options to rig wsl ssh & scp.

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.