xclip

x11 utilitiesLinux/Unix
The xclip command is one of the most frequently used commands in Linux/Unix-like operating systems. xclip Command line interface to X clipboard selections

Quick Reference

Command Name:

xclip

Category:

x11 utilities

Platform:

Linux/Unix

Basic Usage:

xclip [options] [arguments]

Common Use Cases

    Syntax

    xclip [options]

    Options

    Option Description
    -i, -in Read text into X selection from standard input or files (default)
    -o, -out Print the selection to standard output (generally for piping to another program)
    -selection, -sel Specify which X selection to use: primary (default), secondary, or clipboard
    -display, -d X display to connect to (default is $DISPLAY)
    -loops, -l Number of X selection loops to run before exiting
    -t, -target, -targets Specify a target format (MIME type) for the selection data
    -f, -filter Filter mode: standard input to standard output and the selection
    -noutf8 Don't treat text as UTF-8
    -silent, -quiet, -q Suppress warning and error messages
    -verbose, -v Enable verbose output
    -version Print version information and exit
    -help Print help information and exit

    X Selections

    X Window System has multiple selections:

    Selection Description Common Use
    primary The primary selection (default) Middle-click paste
    secondary The secondary selection Rarely used
    clipboard The clipboard selection Ctrl+C/Ctrl+V operations

    Examples

    How to Use These Examples

    The examples below show common ways to use the xclip command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Copy file content to clipboard
    cat file.txt | xclip
    # Copy file content to the X selection (primary selection, middle-click paste)
    cat file.txt | xclip -selection primary
    # Copy file content to clipboard (clipboard selection, Ctrl+V paste)
    cat file.txt | xclip -selection clipboard
    # Copy command output to clipboard ls -la | xclip -selection clipboard
    # Advanced Examples Advanced
    # Copy file content to clipboard and output to terminal
    cat file.txt | tee >(xclip -selection clipboard)
    # Copy SSH public key to clipboard
    cat ~/.ssh/id_rsa.pub | xclip -selection clipboard
    # Copy from one selection to another xclip -o -selection primary | xclip -selection clipboard # Copy HTML content with proper MIME type echo "Bold text" | xclip -t text/html -selection clipboard # Copy image to clipboard xclip -selection clipboard -t image/png -i image.png # Paste clipboard content to a file xclip -o -selection clipboard > clipboard_content.txt # Copy multiple files as filenames to clipboard find . -name "*.jpg" | xclip -selection clipboard # Copy content and notify user
    cat file.txt | xclip -selection clipboard && notify-send "Content copied to clipboard"

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `xclip` command is a versatile utility for the X Window System that provides command-line access to the X clipboard. It serves as a bridge between the terminal and the graphical clipboard system, allowing users to easily copy and paste data between the command line and graphical applications. **Core Functionality:** 1. **X Selection Access**: xclip allows direct manipulation of X selections (primary, secondary, and clipboard) from the command line. 2. **Bidirectional Transfer**: It can both copy data to the clipboard (input mode) and paste data from the clipboard (output mode). 3. **MIME Type Support**: xclip can handle various data formats, not just plain text, by specifying the appropriate MIME type. 4. **Pipeline Integration**: As a command-line tool, xclip integrates seamlessly with Unix pipes, allowing clipboard operations to be part of complex command chains. **Common Use Cases:** 1. **Sharing Command Output**: Easily copy the output of commands to the clipboard for pasting into documents or emails: ```bash ls -la | xclip -selection clipboard ``` 2. **SSH Key Distribution**: Copy SSH public keys to the clipboard for pasting into web interfaces: ```bash cat ~/.ssh/id_rsa.pub | xclip -selection clipboard ``` 3. **Code Sharing**: Copy code or configuration snippets from files to the clipboard: ```bash cat config.json | xclip -selection clipboard ``` 4. **Data Capture**: Save clipboard contents to files: ```bash xclip -o -selection clipboard > clipboard_contents.txt ``` 5. **Cross-Application Workflows**: Move data between terminal applications and GUI applications: ```bash # Extract data from database, format it, and place in clipboard psql -c "SELECT * FROM users" | column -t | xclip -selection clipboard ``` **Technical Details:** 1. **X Selections**: The X Window System maintains three standard selections: - **Primary** (default): Populated when text is selected, pasted with middle-click - **Secondary**: Rarely used alternative selection - **Clipboard**: Used for explicit cut/copy/paste operations (Ctrl+C, Ctrl+V) 2. **Persistence Behavior**: By default, xclip exits immediately after copying data, which can cause the selection to be lost. The `-loops` option keeps xclip running to maintain the selection until another application takes ownership. 3. **MIME Types**: xclip supports various data formats through MIME types, such as: - `text/plain`: Standard text (default) - `text/html`: HTML content - `image/png`: PNG images - `application/json`: JSON data 4. **Filter Mode**: The `-filter` option allows xclip to act like `tee`, passing data to both standard output and the X selection. **Comparison with Related Tools:** 1. **xsel**: Similar to xclip but with slightly different syntax and some unique features. Both tools accomplish the same basic tasks, but some users prefer one over the other for specific use cases. 2. **pbcopy/pbpaste**: macOS equivalents for clipboard operations. xclip provides similar functionality for X11-based systems. 3. **Wayland Alternatives**: On systems using Wayland instead of X11, tools like `wl-copy` and `wl-paste` provide similar functionality. 4. **GUI Clipboard Managers**: Tools like Clipman, CopyQ, or Klipper provide more advanced clipboard management with history, but xclip excels at command-line integration. **Best Practices:** 1. **Clipboard Selection for GUI Applications**: When interacting with most GUI applications, use the clipboard selection: ```bash echo "Text for GUI app" | xclip -selection clipboard ``` 2. **Primary Selection for Terminal Integration**: For quick terminal-to-terminal transfers, the primary selection may be more convenient: ```bash echo "Text for another terminal" | xclip ``` 3. **Persistent Clipboard Content**: For important data that needs to remain in the clipboard, use the loops option: ```bash cat important_data.txt | xclip -selection clipboard -loops 2 ``` 4. **Proper MIME Types**: When copying non-text data or formatted text, specify the appropriate MIME type: ```bash echo "Bold text" | xclip -t text/html -selection clipboard ``` 5. **Silent Operation in Scripts**: Use the quiet option in scripts to suppress unnecessary output: ```bash cat file.txt | xclip -selection clipboard -quiet ``` **Limitations and Considerations:** 1. **X11 Dependency**: xclip only works with the X Window System, not with Wayland (without XWayland) or in pure console environments. 2. **Remote X Sessions**: When working over SSH with X forwarding, clipboard operations with xclip may behave differently than expected or require additional configuration. 3. **Selection Ownership**: The X selection model is ownership-based; when xclip terminates, it gives up ownership of the selection unless the `-loops` option is used. 4. **Large Data Handling**: While xclip can handle reasonably large amounts of data, extremely large clipboard operations may cause performance issues or failures. **Tips and Tricks:** 1. **Creating Clipboard Aliases**: Many users create aliases for common clipboard operations: ```bash alias clip='xclip -selection clipboard' alias paste='xclip -selection clipboard -o' ``` 2. **Multi-format Copying**: For compatibility with various applications, you can copy content in multiple formats simultaneously: ```bash cat file.html | tee >(xclip -t text/html) | xclip -t text/plain ``` 3. **Clipboard to File**: A quick way to save the current clipboard contents to a file: ```bash xclip -selection clipboard -o > clipboard_$(date +%Y%m%d_%H%M%S).txt ``` 4. **File to Clipboard with Notification**: Provide feedback when copying is complete: ```bash cat file.txt | xclip -selection clipboard && notify-send "File copied to clipboard" ``` xclip is an invaluable tool for Linux power users and system administrators who work extensively in the terminal but need to interact with graphical applications. It bridges the command-line and GUI worlds, enabling seamless data flow between these different environments.

    Related Commands

    These commands are frequently used alongside xclip or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the xclip command works in different scenarios.

    $ xclip
    View All Commands