#tmux #tmux-plugin #tmux-copycat #rust

bin+lib copyrat

A tmux plugin for copy-pasting within tmux panes

1 unstable release

0.5.5 Feb 24, 2024

#1129 in Command line utilities

25 downloads per month

MIT license

140KB
2.5K SLoC

tmux-copyrat

crate documentation minimum rustc 1.70 edition 2021 tmux 3.x build status

A tmux-plugin for copy-pasting spans of text from the tmux pane's history into a clipboard.

Use case: you're in tmux and your pane history has some dates you want to copy. You press the key binding to highlight dates (see below for configuration). tmux-copyrat kicks-in and highlights all spans of text which correspond to a date. All spans are displayed with a one or two key hint, which you can then press to copy-paste the span into the tmux clipboard or the system clipboard. Check out the demo below.

The name is a tribute to tmux-copyrat, which I used for many years for that same functionality. For this Rust implementation, I got inspired by tmux-thumbs, and I even borrowed some parts of his regex tests.

Version requirement: rustc 1.70+

Demo

Upcoming

Usage

First install and optionally customize the plugin (see both INSTALLATION.md and CONFIGURATION.md pages) and restart tmux.

Press one of the pre-defined tmux key-bindings (see table below) in order to highlight spans of text matching a specific pattern. To yank some text span in the tmux buffer, press the corresponding hint, or press Esc to cancel and exit.

If instead you want to yank the text span into the system clipboard, either press the caps version of the key hint (for instance E instead of e), or first toggle the destination buffer with the space key and press the hint with no caps.

You can also use the n and N (or Up and Down) keys to move focus across the highlighted spans. Press y to yank the focused span into the tmux buffer, or press Y to yank it into the system clipboard.

By default, span highlighting starts from the bottom of the terminal, but you can reverse that behavior with the --reverse option. The --focus-wrap-around option makes navigation go back to the first span. Many more options are described in CONFIGURATION.md.

Matched patterns and default key-bindings

tmux-copyrat can match one or more pre-defined (named) patterns, but you can add your own too (see CONFIGURATION.md).

The default configuration provided in the copyrat.tmux plugin file provides the following key-bindings. Because they all start with prefix + t, the table below only lists the keyboard key that comes after. For instance, for URLs, the key is u, but you should type prefix + t + u.

key binding searches for pattern name
c Hex color codes hexcolor
d Dates or datetimes datetime
D Docker/Podman IDs docker
e Emails email
G String of 4+ digits digits
h SHA-1/-2 short & long sha
m Markdown URLs [..](matched-url) markdown-url
p Abs. and rel. filepaths path
P Hex numbers and pointer addresses pointer-address
strings inside single quotes quoted-single
strings inside double quotes quoted-double
strings inside backticks quoted-backtick
q strings inside single/double/backticks
u URLs url
U UUIDs uuid
v version numbers version
4 IPv4 addresses 4
6 IPv6 addresses 6
space All patterns

Tmux compatibility

tmux-copyrat is known to be compatible with tmux 3.0 onwards.

Testing this kind of integration with tmux is time consuming, so I'll be grateful if you report incompatibilities as you find them.

The copyrat standalone executable

Although the central binary of this crate is tmux-copyrat, the crate also ships with the copyrat executable which provides the same functionality, minus any tmux dependency or integration and instead reads from stdin.

You can use copyrat to search a span of text that you provide to stdin, à la FZF but more focused and less interactive.

For instance here is a bunch of text, with dates and git hashes which you can search with copyrat.

* e006b06 - (12 days ago = 2021-03-04T12:23:34) e006b06 e006b06 swapper: Make quotes
/usr/local/bin/git

lorem
/usr/local/bin
lorem
The error was `Error no such file`

Let's imagine you want a quick way to always search for SHA-1/2, datetimes, strings within backticks, you would define once the following alias

alias pick='copyrat -r --unique-hint -s bold -x sha -x datetime -x quoted-backtick | pbcopy'

and simply

git log | pick

You will see the following in your terminal

copyrat-output.png

You may have noticed that all identical spans share the same hint, this is due to the -unique-hint option (-u). The hints are in bold text, due to the --hint-style bold option (-s). Hints start from the bottom, due to the --reverse option (-r). A custom pattern was provided for matching any "loca", due to the --custom-regex-pattern option (-X). The sha, datetime and content inside backticks were highlighted due to the --named-pattern option (-x).

Run code-coverage

Install the llvm-tools-preview component and grcov

rustup component add llvm-tools-preview
cargo install grcov

Install nightly

rustup toolchain install nightly

The following make invocation will switch to nigthly run the tests using Cargo, and output coverage HTML report in ./coverage/

make coverage

The coverage report is located in ./coverage/index.html

License

This project is licensed under the MIT license

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~5–18MB
~218K SLoC