Signup/Sign In
PUBLISHED ON: FEBRUARY 13, 2023

Use Linux Xclip command to copy and paste directly to the terminal

The xclip command is a useful tool for users who work in a terminal environment and need to copy and paste text or data between files and documents. It is particularly useful for users who work with scripts and need to quickly copy and paste large amounts of text.

Syntax of xclip command

The basic syntax for the xclip command is as follows:

xclip [OPTIONS] [FILE]

The [OPTIONS] parameter allows users to specify various options for the xclip command, such as the type of data to be copied or the location of the clipboard. The [FILE] parameter is optional and specifies the file from which data should be copied.

Manual page of xclip command in lInux terminal

xclip command option

-i, -in read text into X selection from standard input or files (default)
-o, -out print the selection to standard out (generally for piping to a file or program)
-f, -filter when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified
-r, -rmlastnl when the last character of the selection is a newline character, remove it. Newline characters that are not the last character in the selection are not affected. If the selection does not end with a
newline character, this option has no effect. This option is useful for copying one-line output of programs like pwd to the clipboard to paste it again into the command prompt without executing the
line immediately due to the newline character pwd appends.
-l, -loops number of X selection requests (pastes into X applications) to wait for before exiting, with a value of 0 (default) causing xclip to wait for an unlimited number of requests until another application
(possibly another invocation of xclip) takes ownership of the selection
-t, -target specify a particular data format using the given target atom. With -o the special target atom name "TARGETS" can be used to get a list of valid target atoms for this selection. For more information
about target atoms refer to ICCCM section 2.6.2
-d, -display X display to use (e.g. "localhost:0"), xclip defaults to the value in $DISPLAY if this option is omitted
-h, -help show quick summary of options
-selection specify which X selection to use, options are "primary" to use XA_PRIMARY (default), "secondary" for XA_SECONDARY or "clipboard" for XA_CLIPBOARD
-version show version information
-silent fork into the background to wait for requests, no informational output, errors only (default)
-quiet show informational messages on the terminal and run in the foreground
-verbose provide a running commentary of what xclip is doing
-noutf8 operate in legacy (i.e. non UTF-8) mode for backwards compatibility (Use this option only when really necessary, as the old behavior was broken)

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.

Examples of xclip command

Here are some common use cases for the xclip command:

Copying Text to the Clipboard

To copy text to the clipboard, use the following syntax:

$ echo "text to copy" | xclip

Pasting Text from the Clipboard

To paste text from the clipboard, use the following syntax:

$ xclip -o

Copying a File to the Clipboard

To copy a file to the clipboard, use the following syntax:

$ xclip -selection clipboard /path/to/file

Pasting a File from the Clipboard

To paste a file from the clipboard, use the following syntax:

$ xclip -selection clipboard -o > /path/to/new/file

Conclusion

In conclusion, the xclip command is a powerful tool for managing the clipboard in a terminal environment. It allows users to easily copy and paste text and files, making it a useful tool for a wide range of tasks. With the various options available, users can customize the xclip command to meet their specific needs.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.