mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-05-15 21:14:53 +02:00
2.5 KiB
2.5 KiB
The edit.ps1 Script
This PowerShell script opens the installed text editor with the given text file. Supported are: Emacs, Helix, pico, nano, neovim, Notepad, vi, vim, Visual Studio Code and Wordpad.
Parameters
/Repos/PowerShell/scripts/edit.ps1 [[-path] <String>] [<CommonParameters>]
-path <String>
Specifies the path to the text file (default is to query the user to specify it)
Required? false
Position? 1
Default value
Accept pipeline input? false
Aliases
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
Example
PS> ./edit.ps1 C:\MyDiary.txt
Notes
Author: Markus Fleschutz | License: CC0
Related Links
https://github.com/fleschutz/PowerShell
Script Content
<#
.SYNOPSIS
Opens a text editor
.DESCRIPTION
This PowerShell script opens the installed text editor with the given text file.
Supported are: Emacs, Helix, pico, nano, neovim, Notepad, vi, vim, Visual Studio Code and Wordpad.
.PARAMETER path
Specifies the path to the text file (default is to query the user to specify it)
.EXAMPLE
PS> ./edit.ps1 C:\MyDiary.txt
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$path = "")
function TryEditor { param([string]$editor, [string]$path)
try {
Write-Host "$editor..." -noNewline
& $editor "$path"
if ($lastExitCode -ne 0) { "⚠️ Can't execute '$editor' - make sure it's installed and available"; exit 1 }
exit 0 # success
} catch { return }
}
if ($path -eq "" ) { $path = Read-Host "Enter the path to the text file" }
if (-not(Test-Path "$path" -pathType leaf)) { throw "Can't access file '$path'" }
Write-Host "⏳ Editing '$path' by executing " -noNewline
TryEditor "nvim" $path
TryEditor "vim" $path
TryEditor "vi" $path
TryEditor "nano" $path
TryEditor "pico" $path
TryEditor "hx" $path
TryEditor "emacs" $path
TryEditor "Code" $path
TryEditor "notepad.exe" $path
TryEditor "wordpad.exe" $path
Write-Host ""
"⚠️ Sorry, no text editor found. Please install your favorite one (e.g. by executing 'winget install helix.helix')."
exit 1
(page generated by convert-ps2md.ps1 as of 05/12/2025 22:02:54)