From a4bdd9c9a52a01e98ba388ccc1a1984e0dd83f70 Mon Sep 17 00:00:00 2001 From: lksz Date: Tue, 29 Sep 2020 16:50:19 -0400 Subject: [PATCH] Added Edit-TextFile --- Edit-TextFile.ps1 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Edit-TextFile.ps1 diff --git a/Edit-TextFile.ps1 b/Edit-TextFile.ps1 new file mode 100644 index 0000000..fc0542b --- /dev/null +++ b/Edit-TextFile.ps1 @@ -0,0 +1,25 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [switch]$sudo, + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [string[]]$Path +) + +$local:editor = '/bin/nvim' +if( (Test-Path env:EDITOR) -and (Test-Path $env:EDITOR) ) { + $editor = $env:EDITOR; +} + +if( $editor -match 'vim?$' ) { + $editor += ' -p' +} + +if( $sudo ) { + $editor = "/usr/bin/env sudo $editor" +} + +$local:arguments = $Path -join "' '" +if( $Path ) { $arguments = "'$arguments'" } + +if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { + Invoke-Expression "$editor $arguments" +}