Skip to content

Instantly share code, notes, and snippets.

@jpflouret
Last active June 8, 2025 10:23
Show Gist options
  • Save jpflouret/19da43372e643352a1bf to your computer and use it in GitHub Desktop.
Save jpflouret/19da43372e643352a1bf to your computer and use it in GitHub Desktop.
paste.exe

Pipe contents of windows clipboard to another command

Usage

To pipe the contents of the clipboard to another command (e.g. grep):

paste | grep pattern

To save the contents of the clipboard to a file:

paste > file.txt

Building

You can compile paste.cs with a single command on any Windows machine without the need to install any extra tools. Just run this command:

%SystemRoot%\Microsoft.NET\Framework\v3.5\csc /o paste.cs

Prebuild version is included here as paste.zip

Installing

Put the resulting paste.exe in your path.

using System;
using System.Windows.Forms;
namespace jpf {
class paste {
[STAThread]
public static int Main(string[] args) {
Console.Write(Clipboard.GetText());
return 0;
}
}
}
@Andrej730
Copy link

Andrej730 commented Jun 8, 2025

Noticed a caveat with this method, that it doesn't support unicode characters. E.g. copying "Конвертация текста в таблицу" and running print resulting in "??????????? ?????? ? ???????".

What helped is to specify UTF8 encoding explicitly.

using System;
using System.Text;
using System.Windows.Forms;

namespace jpf {
    class paste {
        [STAThread]
        public static int Main(string[] args) {
            Encoding originalEncoding = Console.OutputEncoding;
            Console.OutputEncoding = Encoding.UTF8;

            Console.Write(Clipboard.GetText());
			
            // If we don't revert it, it might affect other programs.
            Console.OutputEncoding = originalEncoding;
            return 0;
        }
    }
}

PS Thanks for providing a note about how easy it is it compile it myself, was meeting this encoding issue for a while now, but was hesitant to figure how can I recompile it, turned out it was so simple.

Update. Admittedly, it can also be fixed by specifying chcp 65001 in the console explicitly.

Probably the best solution all together is just enable UTF-8 in Windows by default:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment