Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:31
Show Gist options
  • Save codeartery/e8adfb9ee49a9faaeb2e3c6f32d716e4 to your computer and use it in GitHub Desktop.
Save codeartery/e8adfb9ee49a9faaeb2e3c6f32d716e4 to your computer and use it in GitHub Desktop.
Very simple 'console' text editor for quick file edits.
function Edit-Item {
<#
.SYNOPSIS
Sendkeys the contents of $Path into a here string in the current console for quick simple edits. Should not be used on large files.
.EXAMPLE
edit file.txt
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[ValidateScript({
if (-Not ($_ | Test-Path)) {
throw "File not found! '$_'"
}
return $true
})]
[string]$Path
)
begin {
$wsShell = New-Object -ComObject WScript.Shell
Write-Host @"
=======================================================================
Editing: '$((gi $Path).FullName)'
Keys : <Enter> to Save, <Shift-Enter> for Newline, <Ctrl-C> to Cancel
=======================================================================
"@ -ForegroundColor Gray
}
process {
$content = [System.Text.RegularExpressions.Regex]::Replace(((gc $Path).Replace('~','{~}') -join '~'), '[+^%(){}]', '{$0}')
$wsShell.SendKeys("@'~$content~'@|sc '$Path'")
}
}
Set-Alias -Name 'edit' -Value Edit-Item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment