Skip to content

Instantly share code, notes, and snippets.

@akaihola
Last active July 30, 2026 09:09
Show Gist options
  • Select an option

  • Save akaihola/a8ec6d69b80c40ea74e730622f285eba to your computer and use it in GitHub Desktop.

Select an option

Save akaihola/a8ec6d69b80c40ea74e730622f285eba to your computer and use it in GitHub Desktop.
Copy file content as plaintext on Windows
__pycache__/
*.py[cod]

Windows context menu entries to copy file contents as plaintext

You need uvx installed for these to work.

Run the command lines in PowerShell to install.

Each registration is shown first in a readable form and then as the generated one-liner to run. After editing a pretty-printed block, run uv run update_oneliners.py to replace both one-liners in-place. Do not edit the generated blocks directly.

.ics

The PowerShell window closes automatically on success. If uvx or clipboard handling fails, it stays open and displays the error until you press Enter.

Pretty-printed

$key='Registry::HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.ics\shell\CopyAsMarkdown';
New-Item $key -Force | Out-Null;
Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown';
New-Item "$key\command" -Force | Out-Null;
$cmd='powershell.exe -NoProfile -Command "& {
	$ErrorActionPreference = ''Stop'';
	try {
		$env:PYTHONIOENCODING = ''utf-8'';
		$OutputEncoding = [System.Text.UTF8Encoding]::new();
		$content = & uvx icalendar ''%1'';
		if ($LASTEXITCODE -ne 0) {
			throw (''uvx exited with code {0}'' -f $LASTEXITCODE)
		};
		Set-Clipboard -Value ($content -join [Environment]::NewLine)
	} catch {
		Write-Host '''';
		Write-Host ''Copy as Markdown failed:'' -ForegroundColor Red;
		Write-Host $_ -ForegroundColor Red;
		Read-Host ''Press Enter to close''
	}
}"';
Set-ItemProperty "$key\command" -Name '(default)' -Value $cmd

One-liner (generated)

$key='Registry::HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.ics\shell\CopyAsMarkdown'; New-Item $key -Force | Out-Null; Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown'; New-Item "$key\command" -Force | Out-Null; $cmd='powershell.exe -NoProfile -Command "& { $ErrorActionPreference = ''Stop''; try { $env:PYTHONIOENCODING = ''utf-8''; $OutputEncoding = [System.Text.UTF8Encoding]::new(); $content = & uvx icalendar ''%1''; if ($LASTEXITCODE -ne 0) { throw (''uvx exited with code {0}'' -f $LASTEXITCODE) }; Set-Clipboard -Value ($content -join [Environment]::NewLine) } catch { Write-Host ''''; Write-Host ''Copy as Markdown failed:'' -ForegroundColor Red; Write-Host $_ -ForegroundColor Red; Read-Host ''Press Enter to close'' } }"'; Set-ItemProperty "$key\command" -Name '(default)' -Value $cmd

.eml

Pretty-printed

$key='Registry::HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.eml\shell\CopyAsMarkdown';
New-Item $key -Force | Out-Null;
Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown';
New-Item "$key\command" -Force | Out-Null;
$cmd='powershell.exe -WindowStyle Hidden -NoProfile -Command "& uvx
	--from eml-to-md
	--with pyperclip
	python -c
	''import sys, eml_to_md, pyperclip;
	pyperclip.copy(eml_to_md.convert(sys.argv[1]))''
	''%1''"';
Set-ItemProperty "$key\command" -Name '(default)' -Value $cmd

One-liner (generated)

$key='Registry::HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.eml\shell\CopyAsMarkdown'; New-Item $key -Force | Out-Null; Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown'; New-Item "$key\command" -Force | Out-Null; $cmd='powershell.exe -WindowStyle Hidden -NoProfile -Command "& uvx --from eml-to-md --with pyperclip python -c ''import sys, eml_to_md, pyperclip; pyperclip.copy(eml_to_md.convert(sys.argv[1]))'' ''%1''"'; Set-ItemProperty "$key\command" -Name '(default)' -Value $cmd
import unittest
from pathlib import Path
import re
from update_oneliners import PRETTY_BLOCK, collapse_pretty_command, update_document
ORIGINAL_COMMANDS = {
".ics": "$key='Registry::HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.ics\\shell\\CopyAsMarkdown'; New-Item $key -Force | Out-Null; Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown'; New-Item \"$key\\command\" -Force | Out-Null; $cmd='powershell.exe -NoProfile -Command \"& { $ErrorActionPreference = ''Stop''; try { $env:PYTHONIOENCODING = ''utf-8''; $OutputEncoding = [System.Text.UTF8Encoding]::new(); $content = & uvx icalendar ''%1''; if ($LASTEXITCODE -ne 0) { throw (''uvx exited with code {0}'' -f $LASTEXITCODE) }; Set-Clipboard -Value ($content -join [Environment]::NewLine) } catch { Write-Host ''''; Write-Host ''Copy as Markdown failed:'' -ForegroundColor Red; Write-Host $_ -ForegroundColor Red; Read-Host ''Press Enter to close'' } }\"'; Set-ItemProperty \"$key\\command\" -Name '(default)' -Value $cmd",
".eml": "$key='Registry::HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.eml\\shell\\CopyAsMarkdown'; New-Item $key -Force | Out-Null; Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown'; New-Item \"$key\\command\" -Force | Out-Null; $cmd='powershell.exe -WindowStyle Hidden -NoProfile -Command \"& uvx --from eml-to-md --with pyperclip python -c ''import sys, eml_to_md, pyperclip; pyperclip.copy(eml_to_md.convert(sys.argv[1]))'' ''%1''\"'; Set-ItemProperty \"$key\\command\" -Name '(default)' -Value $cmd",
}
MANUALLY_PRETTY_COMMANDS = {
".ics": """$key='Registry::HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.ics\\shell\\CopyAsMarkdown';
New-Item $key -Force | Out-Null;
Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown';
New-Item \"$key\\command\" -Force | Out-Null;
$cmd='powershell.exe -NoProfile -Command \"& {
$ErrorActionPreference = ''Stop'';
try {
$env:PYTHONIOENCODING = ''utf-8'';
$OutputEncoding = [System.Text.UTF8Encoding]::new();
$content = & uvx icalendar ''%1'';
if ($LASTEXITCODE -ne 0) {
throw (''uvx exited with code {0}'' -f $LASTEXITCODE)
};
Set-Clipboard -Value ($content -join [Environment]::NewLine)
} catch {
Write-Host '''';
Write-Host ''Copy as Markdown failed:'' -ForegroundColor Red;
Write-Host $_ -ForegroundColor Red;
Read-Host ''Press Enter to close''
}
}\"';
Set-ItemProperty \"$key\\command\" -Name '(default)' -Value $cmd""",
".eml": """$key='Registry::HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.eml\\shell\\CopyAsMarkdown';
New-Item $key -Force | Out-Null;
Set-ItemProperty $key -Name '(default)' -Value 'Copy as Markdown';
New-Item \"$key\\command\" -Force | Out-Null;
$cmd='powershell.exe -WindowStyle Hidden -NoProfile -Command \"& uvx
--from eml-to-md
--with pyperclip
python -c
''import sys, eml_to_md, pyperclip;
pyperclip.copy(eml_to_md.convert(sys.argv[1]))''
''%1''\"';
Set-ItemProperty \"$key\\command\" -Name '(default)' -Value $cmd""",
}
class CollapsePrettyCommandTests(unittest.TestCase):
def test_manual_pretty_prints_reverse_to_original_commands_exactly(self) -> None:
for extension, pretty_command in MANUALLY_PRETTY_COMMANDS.items():
with self.subTest(extension=extension):
self.assertEqual(
collapse_pretty_command(pretty_command),
ORIGINAL_COMMANDS[extension],
)
class UpdateDocumentTests(unittest.TestCase):
def test_replaces_each_generated_one_liner_from_its_pretty_block(self) -> None:
document = """# Commands
<!-- BEGIN .ics PRETTY -->
```powershell
first;
second
```
<!-- END .ics PRETTY -->
<!-- BEGIN .ics ONE-LINER -->
```powershell
stale
```
<!-- END .ics ONE-LINER -->
<!-- BEGIN .eml PRETTY -->
```powershell
third;
fourth
```
<!-- END .eml PRETTY -->
<!-- BEGIN .eml ONE-LINER -->
```powershell
also stale
```
<!-- END .eml ONE-LINER -->
"""
updated = update_document(document)
self.assertIn("```powershell\nfirst; second\n```", updated)
self.assertIn("```powershell\nthird; fourth\n```", updated)
self.assertNotIn("stale", updated)
def test_rejects_a_pretty_block_without_a_matching_one_liner(self) -> None:
document = """<!-- BEGIN .ics PRETTY -->
```powershell
command
```
<!-- END .ics PRETTY -->
"""
with self.assertRaisesRegex(ValueError, "matching ONE-LINER"):
update_document(document)
class MarkdownDocumentTests(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
document_path = Path(__file__).with_name("copy-as-plaintext-win.md")
cls.document = document_path.read_text(encoding="utf-8")
def test_contains_the_manually_verified_pretty_commands(self) -> None:
pretty_commands = {
match.group("name"): match.group("command")
for match in PRETTY_BLOCK.finditer(self.document)
}
self.assertEqual(pretty_commands.keys(), MANUALLY_PRETTY_COMMANDS.keys())
for extension, pretty_command in pretty_commands.items():
with self.subTest(extension=extension):
self.assertEqual(
collapse_pretty_command(pretty_command),
collapse_pretty_command(MANUALLY_PRETTY_COMMANDS[extension]),
)
def test_contains_the_exact_generated_original_one_liners(self) -> None:
for extension, original_command in ORIGINAL_COMMANDS.items():
with self.subTest(extension=extension):
match = re.search(
rf"<!-- BEGIN {re.escape(extension)} ONE-LINER -->\n"
rf"```powershell\n(.*?)\n```\n"
rf"<!-- END {re.escape(extension)} ONE-LINER -->",
self.document,
re.DOTALL,
)
self.assertIsNotNone(match)
self.assertEqual(match.group(1), original_command)
if __name__ == "__main__":
unittest.main()
#!/usr/bin/env -S uv run --script
"""Regenerate PowerShell one-liners embedded in the Markdown document."""
import argparse
from pathlib import Path
import re
PRETTY_BLOCK = re.compile(
r"<!-- BEGIN (?P<name>\S+) PRETTY -->\n"
r"```powershell\n(?P<command>.*?)\n```\n"
r"<!-- END (?P=name) PRETTY -->",
re.DOTALL,
)
def collapse_pretty_command(command: str) -> str:
return " ".join(line.strip() for line in command.splitlines() if line.strip())
def update_document(document: str) -> str:
pretty_blocks = list(PRETTY_BLOCK.finditer(document))
if not pretty_blocks:
raise ValueError("document contains no PRETTY blocks")
updated = document
for match in pretty_blocks:
name = match.group("name")
one_liner = collapse_pretty_command(match.group("command"))
generated_block = re.compile(
rf"(<!-- BEGIN {re.escape(name)} ONE-LINER -->\n"
rf"```powershell\n).*?(\n```\n"
rf"<!-- END {re.escape(name)} ONE-LINER -->)",
re.DOTALL,
)
updated, replacements = generated_block.subn(
lambda block: f"{block.group(1)}{one_liner}{block.group(2)}",
updated,
)
if replacements != 1:
raise ValueError(
f"PRETTY block {name!r} must have exactly one matching ONE-LINER block"
)
return updated
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"document",
nargs="?",
type=Path,
default=Path(__file__).with_name("copy-as-plaintext-win.md"),
)
args = parser.parse_args()
document = args.document.read_text(encoding="utf-8")
args.document.write_text(update_document(document), encoding="utf-8")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment