How to use the command xsel (with examples)
- Linux
- December 25, 2023
The xsel
command is a tool for manipulating the X11 selection and clipboard. It provides a way to interact with the clipboard and perform various operations such as copying data into the clipboard, pasting data from the clipboard, clearing the clipboard, and more.
Use case 1: Using a command’s output as input of the clipboard
Code:
echo 123 | xsel -ib
Motivation:
This use case allows you to quickly copy the output of a command into the clipboard without having to manually select and copy the text. It is equivalent to the “Ctrl + C” shortcut for copying. This can be useful when you want to copy the output of a command and paste it into another application.
Explanation:
echo 123
: This is a command that simply echoes the string “123”.|
: This is the pipe symbol, which is used to redirect the output of one command to the input of another command.xsel -ib
: This command takes the standard input from the pipe (echo 123
) and copies it into the clipboard using the-ib
option. The-i
option tellsxsel
to read from standard input, and the-b
option specifies that the clipboard should be used.
Example output:
The output of the command will be copied into the clipboard. You can then paste it into another application by using the “Ctrl + V” shortcut.
Use case 2: Using the contents of a file as input of the clipboard
Code:
cat path/to/file | xsel -ib
Motivation:
This use case allows you to copy the contents of a file into the clipboard. It can be useful when you want to share the contents of a file with someone by simply pasting it into a chat or email application.
Explanation:
cat path/to/file
: This command reads the contents of the file located atpath/to/file
.|
: This is the pipe symbol, which is used to redirect the output of one command to the input of another command.xsel -ib
: This command takes the standard input from the pipe (cat path/to/file
) and copies it into the clipboard using the-ib
option. The-i
option tellsxsel
to read from standard input, and the-b
option specifies that the clipboard should be used.
Example output:
The contents of the file will be copied into the clipboard. You can then paste it into another application by using the “Ctrl + V” shortcut.
Use case 3: Outputting the clipboard’s contents into the terminal
Code:
xsel -ob
Motivation:
This use case allows you to view the contents of the clipboard in the terminal. It is equivalent to the “Ctrl + V” shortcut for pasting. This can be useful when you want to verify what is currently in the clipboard without actually pasting it into another application.
Explanation:
xsel
: This is thexsel
command itself.-ob
: This option tellsxsel
to output the contents of the clipboard. The-o
option is used to specify output, and the-b
option specifies that the clipboard should be used.
Example output:
The contents of the clipboard will be printed in the terminal.
Use case 4: Outputting the clipboard’s contents into a file
Code:
xsel -ob > path/to/file
Motivation:
This use case allows you to save the contents of the clipboard into a file. It can be useful when you want to store the contents for later use or for sharing with others.
Explanation:
xsel
: This is thexsel
command itself.-ob
: This option tellsxsel
to output the contents of the clipboard. The-o
option is used to specify output, and the-b
option specifies that the clipboard should be used.>
: This is the redirection operator, which is used to redirect the output of a command to a file.path/to/file
: This is the path to the file where the clipboard contents will be saved.
Example output:
The contents of the clipboard will be saved into the specified file.
Use case 5: Clearing the clipboard
Code:
xsel -cb
Motivation:
This use case allows you to clear the contents of the clipboard. It can be useful when you want to remove sensitive or irrelevant information from the clipboard.
Explanation:
xsel
: This is thexsel
command itself.-cb
: This option tellsxsel
to clear the clipboard. The-c
option is used to specify clearing, and the-b
option specifies that the clipboard should be cleared.
Example output:
The clipboard will be cleared, and its contents will be removed.
Use case 6: Outputting the X11 primary selection’s contents into the terminal
Code:
xsel -op
Motivation:
This use case allows you to view the contents of the X11 primary selection in the terminal. The X11 primary selection is the text that is selected using the mouse. This use case is equivalent to performing a middle-click using the mouse.
Explanation:
xsel
: This is thexsel
command itself.-op
: This option tellsxsel
to output the contents of the X11 primary selection. The-o
option is used to specify output, and the-p
option specifies that the primary selection should be used.
Example output:
The contents of the X11 primary selection will be printed in the terminal, which is the selected text from the mouse.