Skip to content

Commit

Permalink
update pbcopy-pbpaste.md, xclip.md, xsel.md and clipboard.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCurrin committed Jun 4, 2024
1 parent 831f384 commit fcbcc48
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 109 deletions.
34 changes: 31 additions & 3 deletions cheatsheets/shell/commands/clipboard/pbcopy-pbpaste.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description: CLI tool for macOS to interact with clipboard data

## Note for Linux users

You can setup `xsel` to behave like this tool - see [xsel][]
You can setup `xsel` to behave like this tool - see the aliases section on the [xsel][] cheatsheet.

[xsel]: {% link cheatsheets/shell/commands/clipboard/xsel.md %}


## Installation
Expand All @@ -17,7 +19,33 @@ This comes installed on macOS.

```sh
$ echo "This is a test" | pbcopy
$ pbpaste > test.txt

```

[xsel]: {% link cheatsheets/shell/commands/clipboard/xsel.md %}
### Read with `pbcopy`

Copy file to the clipboard.

```sh
$ pbcopy < PATH
```

### Output with `pbpaste`

Print the clipboard contents:

```sh
$ pbpaste
```

Pipe to a command:

```sh
$ pbpaste | head
```

Write to a file:

```sh
$ pbpaste > test.txt
```
59 changes: 50 additions & 9 deletions cheatsheets/shell/commands/clipboard/xclip.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: Command-line tool for X11 to interact with clipboard data

`xclip` is a command-line utility available on many Linux systems that enables you to manage the clipboard data within the X Window System (X11) environment. It provides functionality similar to the built-in `pbcopy` and `pbpaste` commands found on macOS.

A feature of this tool is that is supports secondary selection. Many graphical environments have a secondary selection buffer (middle mouse button) in addition to the main clipboard. This tool can be used to work with this secondary buffer as well.


## Installation

Expand All @@ -27,33 +29,72 @@ Here are some common approaches:

## Usage

### Manual

For detailed information on these options and more, refer to the `xclip` man page:

```sh
$ man xclip
```

Use `xclip` - see [man page](https://linux.die.net/man/1/xclip).

> xclip [OPTION] [FILE]...
>
> Description
>
> Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications. Prints current X selection to standard out.
See clipboard management tasks below.

### Copy text to clipboard
### Read

Store value in **X** clipboard selection.

Pipe to `xclip`:

```sh
$ COMMAND | xclip
```

```sh
$ echo 'My content' | xclip
```

Read a file:

```sh
$ echo "This is a test" | xclip
$ xclip -i PATH
$ # OR
$ xclip < PATH
$ # OR
$ xclip PATH
```

This copies the string "This is a test" to the clipboard.
### Output

### Paste clipboard contents to a file
Print clipboard contents:

```sh
$ xclip -o > test.txt
$ xclip
```

This retrieves the clipboard contents and saves them to a file named `test.txt`.
Paste clipboard contents to a file:

```sh
$ xclip -o PATH
$ # OR
$ xclip > PATH
```

### Additional options:
### Selection

- `-selection clipboard`: This specifies the primary clipboard (default).
- `-selection primary`: This targets the primrary selection (if available).
- `-selection secondary`: This targets the secondary clipboard (if available).
- `-in`: Read clipboard content from a file.
- `-out`: Write clipboard content to a file.

e.g.

```sh
$ xclip -sel clip PATH
```
60 changes: 51 additions & 9 deletions cheatsheets/shell/commands/clipboard/xsel.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,80 @@ This content is based on this [video](https://www.youtube.com/watch?v=aMzdeZ8vGX

Xsel is a command-line tool designed for the X Window System (X11) that allows you to interact with the clipboard data. In simpler terms, it's a program you can use in your terminal to manage what gets copied and pasted between applications.

A feature of this tool is that is supports secondary selection. Many graphical environments have a secondary selection buffer (middle mouse button) in addition to the main clipboard. This tool can be used to work with this secondary buffer as well.


## Comparison with other tools
## Install

Note that [pbcopy / pbpaste][] is available for macoS and not available on Linux, but you can configure Linux to use `xsel` like it.
```sh
$ sudo apt install xsel
```

A feature of `xsel` is that is supports secondary selection. Many graphical environments have a secondary selection buffer in addition to the main clipboard. `xsel` can be used to work with this secondary buffer as well.
## Usage

From the help:

[pbcopy / pbpaste]: {% link cheatsheets/shell/commands/clipboard/pbcopy-pbpaste.md %}
```
-p, --primary Operate on the PRIMARY selection (default)
-s, --secondary Operate on the SECONDARY selection
-b, --clipboard Operate on the CLIPBOARD selection
```

### Read

## Install
Copy text to the clipboard:

```sh
$ sudo apt install xsel
$ xsel --input --clipboard
$ xsel --input --clipboard < PATH
$ echo 'Text' | xsel --input --clipboard
```

Copy text to the primary selection - same as above but just `xsel` without arguments.

Copy text to the secondary selection:

```sh
$ xsel --selection secondary
```

### Output

View the contents of the clipboard:

```sh
$ xsel --output --clipboard
$ xsel --output --clipboard | COMMAND
$ xsel --output --clipboard > PATH
```

View the contents of the primary selection:

```sh
$ xsel -o
```

View the contents of the secondary selection:

```sh
$ xsel --selection secondary -o
```


## Configure aliases

Reduce how much you have to type by imitating use of `pbcopy` and `pbpaste` on macOS.

Edit your `~/.bashrc` or `~/.zshrc` with these aliases:

```sh
alias pbcopy='xsel --input --clibboard'
alias pbpaste='xsel --output --clibboard'
alias pbcopy='xsel --input --clipboard'
alias pbpaste='xsel --output --clipboard'
```

Then use as:

```sh
$ echo "This is a test" | pbcopy
$ pbpaste > test.txt
$ pbpaste > PATH
```
92 changes: 4 additions & 88 deletions cheatsheets/shell/files/text-files/clipboard.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,7 @@
---
description: Copy to and paste from clipboard. Especially useful for very large files or when you use SSH with no GUI.
---
# Clipboard

Copy to and paste from clipboard. Especially useful for very large files or when you use SSH with no GUI.


See [StackOverflow answer](https://stackoverflow.com/questions/749544/pipe-to-from-the-clipboard-in-bash-script) for usage and shortcuts.


## Linux

Use `xclip` - see [man page](https://linux.die.net/man/1/xclip).

> xclip [OPTION] [FILE]...
>
> Description
>
> Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications. Prints current X selection to standard out.
### Read

Store value in X selection.

Command output.

```sh
$ COMMAND | xclip
```

Read file.

```sh
$ xclip PATH
```

This lets you paste using the middle mouse button.

If you prefer to traditional pasting:

```sh
$ xclip -sel clip PATH
```

Or according to the manpage:

> -selection
>
> specify which X selection to use, options are "primary" to use XA_PRIMARY (default), "secondary" for XA_SECONDARY or "clipboard" for XA_CLIPBOARD
### Output

Print.

```sh
$ xclip
```

Pipe.

> -o, -out
>
> prints the selection to standard out (generally for piping to a file or program)
```sh
$ xclip -o > PATH
```

## macOS

Use `pbcopy` and `pbpaste`.

### Read

Copy file.

```sh
$ pbcopy < PATH
```

### Output

Print.

```sh
$ pbpaste
```

Pipe.

```sh
$ pbpaste | head
```
See [Clipboard commands]({% link cheatsheets/shell/commands/clipboard/index.md %}) cheatsheet section.

0 comments on commit fcbcc48

Please sign in to comment.