Skip to content

beeender/richclip

Repository files navigation

richclip - Command line clipboard utility for multiple platforms

richclip is created for richclip.nvim which intends to copy rich text format of source code from neovim with highlights to the system clipboard. But it can also be used as a standalone command line utility as an alternative to xclip, xsel, wl-clipboard or other command line clipboard tools.

  • Supports Multiple environments. Xorg and Wayland are supported by the current version. MacOS and Windows are planned.
  • Recognizes the environment automatically, and choose the right clipboard to use.
  • Supports multiple content with different mime-types simultaneously. A typical use case is to have the plain source code with mime-type text/plain and highlighted HTML format code with mime-type text/html copied, so the client can choose the preferred content type to paste.

Installing

Arch Linux

Install richclip from the AUR.

Other Linux Distributions

Download the static linked binary from the release page.

MacOS & Windows

Not supported yet

Usage

Paste

❯ richclip paste --help
Paste the data from clipboard to the output
Usage: richclip paste [OPTIONS]
Options:
  -l, --list-types        List the offered mime-types of the current clipboard only without the contents
  -t, --type <mime-type>  Specify the preferred mime-type to be pasted
  -p, --primary           Use the 'primary' clipboard
  -h, --help              Print help

Copy

❯ richclip copy --help
Receive and copy data to the clipboard
Usage: richclip copy [OPTIONS]

Options:
  -p, --primary             Use the 'primary' clipboard
      --foreground          Run in foreground
      --one-shot            Enable one-shot mode, anything received from stdin will be copied as it is
  -t, --type [<mime-type>]  Specify mime-type(s) to copy and implicitly enable one-shot copy mode
  -h, --help                Print help

Bulk mode copy

By default, richclip receives data in bulk mode. In this mode, multiple formats of data can be copied to the clipboard with multiple mime-types. This allows the paste side to choose the favorite format of data to use.

The data to be copied to the clipboard needs to follow a simple protocol which is described as below.

Item Bytes Content
Magic 4 0x20 0x09 0x02 0x14
Protocol Version 1 0x00
Section Type 1 'M'
Section Length 4 0x00 0x00 0x00 0x0a
Section Data 4 "text/plain"
Section Type 1 'M'
Section Length 4 0x00 0x00 0x00 0x04
Section Data 4 "TEXT"
Section Type 1 'C'
Section Length 4 0x00 0x00 0x00 0x09
Section Data 4 "SOME Data"
Section Type 1 'M'
Section Length 4 0x00 0x00 0x00 0x09
Section Data 4 "text/html"
Section Type 1 'C'
Section Length 4 0x00 0x00 0x00 0x09
Section Data 4 "HTML code"
  • Every section starts with the section type, M (mime-type) or C (content).
  • Before C section, there must be one or more M section to indicate the data type.
  • Section length will be parsed as big-endian uint32 number.

One-shot mode copy

This is the traditional way to copy data like other clipboard utilities. The mime-types can be specified through the command line, and all data received through stdin will be copied as it is.

# Copy "TestData" to the clipboard, with default mime-type
echo TestData | richclip copy --one-shot


# Copy "<body>Haha</body>" to the clipboard, as "text/html" and "HTML"
echo "<body>Haha</body>" | richclip copy --type "text/html" --type "HTML"