This Linux Command Lets You Copy & Paste Without a Mouse


Ever felt that copy-pasting using the mouse gets tiresome? Especially when you’re more comfortable using the terminal and the keyboard, switching to the mouse can be counterproductive. The xclip command will help you stay on your keyboard when you want to copy and paste content.

What Is xclip?

xclip is a command-line utility used on Linux systems with an X11 implementation to interact with the X selection (also known as the clipboard). It allows you to copy text from files or standard input and make it available for pasting in other X applications. xclip can also print the contents of the X selection to standard output. It has many useful options and use cases to explore.

Even though it sounds like xclip is only meant for systems that use the X window manager. That’s not the case, as you’ll see how I use it on a Wayland system.

In this quick guide, we’ll dive deep into how you can use the xclip command to copy contents from a file, different useful options, and when it’s useful to whip out xclip. For demonstration purposes, I’ll be using Ubuntu 24.04 LTS. If you’re using a different distro, you can still follow along, but you may need to tweak some commands slightly.

When Is xclip Most Useful?

The best use case I can think of is when you want to copy large files. It can be tedious to use the mouse to keep selecting lines after lines. Quite often, long lines get cut off in some text editors, such as nano. In that case, it gets even more difficult to select and copy the file content. In such situations, xclip can be a convenient tool to use. You can copy the file content with a single command and paste it anywhere you want.

How to Get and Use xclip

xclip usually doesn’t come pre-installed on Linux distributions. So, in that case, you’ll have to install it yourself. You can either install it using your distro’s package manager or get the source from its GitHub repository. For Debian, Ubuntu, and their derivatives, install it using:

sudo apt install xclip
Installing xclip on Ubuntu using apt package manager.

If you’re using Red Hat, Fedora, or the likes, then run:

sudo dnf install xclip

For Arch Linux and Manjaro users, use:

sudo pacman -S xclip

To confirm the installation, you can check its version by running:

xclip -version

The most basic way to use the xclip command is to copy the content from the standard input or a file. For example, if you want to copy a file’s content to the clipboard instead of displaying it on the terminal, simply pipe the output to the xclip command. Let me create a simple file to show you how.

First, I’ll create a test file and put some dummy text in it.

touch test.txt
echo 'Demonstrating the xclip command' > test.txt

Now, to read the contents of the file and display the output on the command line, we use the cat command. We’ll do the same here, but pipe the output to the xclip command like this:

cat test.txt | xclip
Copying the content of a file to the clipboard using xclip.

The result isn’t obvious here. That’s because we don’t have any output or anything. So, how do you know that the copy was successful? To display the current clipboard content, simply run:

xclip -o
Displaying the clipboard content in the terminal using the xclip command.

If you don’t want to copy the content of a specific file and simply want to put some text in the clipboard, then the echo command is helpful. We’ll pipe the commands in the same way we did earlier.

echo "I live in the clipboard" | xclip
xclip -o
Copying a text from the echo command using xclip.

You can also copy content from a file without piping the output. To do that, pass the file name as an argument.

xclip test.txt
xclip -o

There’s another way you can copy file content. Here’s the full command:

xclip -sel clip test.txt
Copying file content using xclip and -sel option.

There’s a subtle difference between just passing the file name and using the -sel clip option. The -sel clip option is actually the short form of -selection clipboard. When you use this option, instead of the X window clipboard, you interact with the system clipboard. So what does that mean? If you’re using the first command (passing only the file name), you can paste the copied content using the middle button of your mouse. In the second case, you need to paste the content from the right-click context menu or by pressing Ctrl+Shift+v.

When copying content from a file, sometimes you’d want to ignore the newline character. To do that, use:

xclip -r test.txt
Copying content from a file using xclip without the newline character.

Another interesting option is the looping option. When you use this option, you basically tell xclip to load the contents of the file into the X11 clipboard, and let it be pasted a specific number of times into applications like terminals or text editors that read from the X selection before exiting. By default, the number is zero, so xclip can take infinite requests. Here’s an example:

xclip -loops 5 -verbose test.txt
Using loops with xclip to control the selection requests.

Using xclip, you can also paste the content to a file using redirection.

        touchĀ test2.txt
xclipĀ test.txt
xclipĀ -o > test2.txt
catĀ test2.txt
Using xclip to copy the content from one file to another.

xclip doesn’t only copy the content of text files, it can also copy output from other commands. Say you want to copy the current directory content. To do so, run:

ls -la | xclip
xclip -o
Copying the output of another command using xclip.

Besides all these, xclip also comes with a few more utilities such as xclip-copyfile, xclip-pastefile, and xclip-cutfile. You can use these commands to copy and move files between different directories and even machines. You can learn more about it using the man page or -h option.


The xclip tool is wonderful for quick copy-pasting work. If you prefer using the command line and keyboard for most things, it can help you avoid the mouse for faster workflows. There are many more useful Linux commands like this out for you to try.



Source link

Previous articleThis Mac setup locked me to spatial computing at a fraction of the Vision Pro
Next articleiPhone and iPad users can now emulate Switch games at full speedĀ thanks to AltStore