Skip to content

Instantly share code, notes, and snippets.

@algonzalez
Last active March 13, 2025 04:30
Show Gist options
  • Save algonzalez/6606ede868ee5364f97af9b97ed511f7 to your computer and use it in GitHub Desktop.
Save algonzalez/6606ede868ee5364f97af9b97ed511f7 to your computer and use it in GitHub Desktop.
# ----------------------------------------
# Command: Start-Scratchpad (Written in PowerShell 7)
# Used in my daily script to open a new scratchpad file for the day.
# File naming follows the following format: ~/.scratch/yyyy-MM-dd-scratchpad.md
# ----------------------------------------
# Includes code for Get-Editor and Get-TerminalEditor dependencies
# ----------------------------------------
# Returns the editor specified in the VISUAL or EDITOR environment variables.
# If none is specified, a platform default is returned.
# Default is set to VS Code on Windows, my editor of choice.
function Get-Editor {
# defaulting to VS Code, my editor of choice
$env:VISUAL.Length -gt 0 ?
$env:VISUAL :
$env:EDITOR.Length -gt 0 ?
$env:EDITOR :
$IsWindows ?
'code' :
'vim'
}
# Returns the editor specified in the EDITOR environment variable
# If none is specified, a platform default is returned.
# Default is set 'micro' on Windows, my editor of choice.
function Get-TerminalEditor {
$env:EDITOR.Length -gt 0 ?
$env:EDITOR :
$IsWindows ?
'micro' :
'nano'
}
function Start-Scratchpad {
param(
[Parameter(Position = 0, Mandatory=$false,
HelpMessage='When $true, returns $env:EDITOR even if $env:VISUAL is defined.')]
[switch]$useTerminalEditor
)
$dir = Join-Path ($IsWindows ? $env:USERPROFILE : $env:Home) ".scratch"
$file = Join-Path "$dir" "$(Get-Date -Format 'yyyy-MM-dd')-scratchpad.md"
# create scratch folder, if it doesn't exist
if (-not (Test-Path "$dir" -PathType Container)) {
New-Item -ItemType Directory -Force -Path $dir
}
$line = '------------------------------------------------------------'
# create scratchpad file, if it doesn't exist
if (-not (Test-Path "$file")) {
New-Item -ItemType File -Force -Path $file
"$line`n$line`n" | Out-File -FilePath "$file"
}
$editor = if ($useTerminalEditor) {Get-TerminalEditor} else {Get-Editor}
# open scratch pad in editor
if ($editor -ieq 'code') {
. $editor --new-window "$dir" --goto "$file"
} else {
. $editor "$file"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment