PowerShell/docs/edit.md

97 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *edit.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2024-03-27 17:36:59 +01:00
This PowerShell script opens a text editor with the given text file.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/edit.ps1 [[-path] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-03-27 17:36:59 +01:00
-path <String>
Specifies the path to the text file (will be queried if none given)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2024-03-27 17:36:59 +01:00
PS> ./edit.ps1 C:\MyDiary.txt
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2024-03-27 17:36:59 +01:00
Opens a text editor
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2024-03-27 17:36:59 +01:00
This PowerShell script opens a text editor with the given text file.
.PARAMETER path
Specifies the path to the text file (will be queried if none given)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-03-27 17:36:59 +01:00
PS> ./edit.ps1 C:\MyDiary.txt
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +01:00
param([string]$path = "")
2022-11-17 20:02:26 +01:00
2024-03-27 17:36:59 +01:00
function TryEditor { param([string]$editor, [string]$path)
try {
2024-11-08 12:35:11 +01:00
Write-Host "$editor.." -noNewline
2024-03-27 17:36:59 +01:00
& $editor "$path"
if ($lastExitCode -ne "0") {
"⚠️ Can't execute '$editor' - make sure it's installed and available"
exit 1
}
exit 0 # success
} catch {
return
2022-11-17 20:02:26 +01:00
}
2024-03-27 17:36:59 +01:00
}
try {
if ($path -eq "" ) { $path = Read-Host "Enter the path to the text file" }
2024-11-08 12:35:11 +01:00
Write-Host "Searching for " -noNewline
TryEditor "neovim" $path
2024-03-27 17:36:59 +01:00
TryEditor "vim" $path
TryEditor "vi" $path
TryEditor "nano" $path
TryEditor "pico" $path
TryEditor "emacs" $path
TryEditor "notepad.exe" $path
TryEditor "wordpad.exe" $path
Write-Host ""
throw "No text editor found - use 'winget install' to install your favorite text editor."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:53)*