Skip to content

Instantly share code, notes, and snippets.

@hktonylee
Created December 25, 2025 07:08
Show Gist options
  • Select an option

  • Save hktonylee/4297134d43f766c3e9d5589d21e81195 to your computer and use it in GitHub Desktop.

Select an option

Save hktonylee/4297134d43f766c3e9d5589d21e81195 to your computer and use it in GitHub Desktop.
Zsh scripts to copy or paste image in Ubuntu / WSL
#!/usr/bin/env zsh
() {
local file_type="$(file -b --mime-type "$1")"
if [[ "$file_type" == "image/png" ]]; then
cat "$1" | powershell.exe -c '
Add-Type -AssemblyName System.Drawing;
Add-Type -AssemblyName System.Windows.Forms;
$ms = New-Object System.IO.MemoryStream
$stdin = [Console]::OpenStandardInput()
$stdin.CopyTo($ms)
$img = [System.Drawing.Image]::FromStream($ms)
[System.Windows.Forms.Clipboard]::SetImage($img)
$img.Dispose()
$ms.Dispose()
'
else
cat "$1" | clip.exe
fi
} =(cat) # Flush all input to the temp file, that supports lseek
#!/usr/bin/env zsh
powershell.exe -c '
Add-Type -AssemblyName System.Windows.Forms;
if ([System.Windows.Forms.Clipboard]::ContainsImage()) {
[Console]::Write("0")
$image = [System.Windows.Forms.Clipboard]::GetImage()
$ms = New-Object System.IO.MemoryStream
$image.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
[Console]::OpenStandardOutput().Write($ms.ToArray(), 0, $ms.Length)
$ms.Dispose()
} else {
[Console]::Write("1")
$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Get-Clipboard
}
' | (
local ctl
read -k1 -u0 ctl
if [[ $ctl == "1" ]]; then
sed 's/\r$//' | sed -z '$ s/\n$//'
else
cat
fi
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment