As developers, we often find ourselves working in remote environments like Kubernetes pods or EC2 instances. A common challenge is moving data, especially large files, from these remote systems to our local clipboard. While tools like scp or SSH tunnels are great, they aren't always available or convenient, particularly in restricted or temporary environments. Opening a multi-megabyte log file in vim just to copy its contents can be slow and memory-intensive.
What if you could pipe any file, no matter how large, directly to your local clipboard from a remote terminal with a single command?
Enter termco-py, a lightweight Python CLI tool designed to solve this exact problem.
termco-py?termco-py is a simple yet powerful utility that sends file contents or piped data to your local machine's clipboard using terminal escape sequences (specifically, OSC 52). This means you can copy data from a remote session without needing an active SSH tunnel for clipboard sharing or loading the entire file into an editor. It's fast, efficient, and works in any terminal that supports OSC 52, such as iTerm2, Alacritty, or WezTerm.
Installation is as simple as running pip:
pip install termcopy
termco-py is designed to be intuitive and fit naturally into your existing command-line workflows.
To copy the entire contents of a file, just pass the filename to termcopy:
termcopy /var/log/really_large_log_file.log
The content is now in your local clipboard, ready to be pasted anywhere you need it.
The real power of termco-py shines when combined with other tools. You can pipe the output of any command directly into it.
Want to copy the last 100 lines of a log?
tail -n 100 my-app.log | termcopy
Need to grab the YAML definition of a running Kubernetes pod?
kubectl get pod my-pod -o yaml | termcopy
Or maybe you just want to quickly share a snippet of text:
echo "Hello from the remote server!" | termcopy
The magic behind termco-py is the OSC 52 escape sequence. Here's a quick rundown of the process:
stdin) if it's part of a pipe.\x1b]52;c;BASE64_STRING\x07.This approach avoids the need for complex networking or loading large amounts of data into memory, making it a highly efficient solution for a common problem.
This project is a Python alternative to my friend Trevor Bernard's Rust implementation, which demonstrated the power and elegance of using OSC 52 escape sequences for clipboard operations in terminal environments. You can find his project at trevorbernard/termcopy.
Next time you're deep in a remote session and need to get data back to your local machine, give termco-py a try. It's a simple tool that can save you a surprising amount of time and hassle.
Check out the project on GitHub at scaryPonens/termco-py to see the source code, and feel free to contribute!
Comments