How To Copy Text In Linux : Using Echo Command For Text Output

Selecting text in a Linux terminal can be done by clicking and dragging with your mouse to highlight the desired content. But if you want to master the command line, you need to know how to copy text in Linux using both keyboard shortcuts and command-line tools. This guide covers every method, from simple mouse clicks to advanced clipboard utilities, so you can work faster and smarter.

Whether you are a beginner or a seasoned user, copying text in Linux is a fundamental skill. The terminal and graphical environments each have their own tricks. Let’s break it down step by step.

How To Copy Text In Linux

Linux offers multiple ways to copy text, depending on whether you are in a terminal, a desktop environment, or a text editor. The most common methods involve using the mouse, keyboard shortcuts, or command-line utilities like xclip and pbcopy. Below, we explore each approach in detail.

Copying Text In The Terminal With A Mouse

In most Linux terminals, copying text with the mouse is straightforward. You simply select the text and it is automatically copied to a special buffer called the “primary selection.” To paste, you click the middle mouse button or press Shift+Insert.

  • Highlight text by clicking and dragging the left mouse button.
  • Release the mouse button—the text is now in the primary clipboard.
  • Move your cursor to where you want to paste and click the middle mouse button.

This method works in GNOME Terminal, Konsole, Xterm, and most other terminal emulators. It is fast and does not require any extra commands.

Using Keyboard Shortcuts In The Terminal

If you prefer keyboard shortcuts, most terminal emulators support Ctrl+Shift+C for copy and Ctrl+Shift+V for paste. These shortcuts are different from the standard Ctrl+C (which sends an interrupt signal) and Ctrl+V (which inserts literal characters).

  1. Select the text with your mouse or by holding Shift and using arrow keys.
  2. Press Ctrl+Shift+C to copy the selected text.
  3. Move to your target location and press Ctrl+Shift+V to paste.

This method is consistent across many terminal emulators, including GNOME Terminal, Terminator, and Tilix. Some older terminals may use Ctrl+Insert for copy and Shift+Insert for paste.

Copying Text Between Terminal And GUI Applications

Copying text from a terminal to a graphical application (like a web browser or text editor) requires a bit more care. The primary selection buffer is separate from the system clipboard used by GUI apps. To copy text to the system clipboard, you can use the xclip command.

First, install xclip if it is not already installed:

sudo apt install xclip   # Debian/Ubuntu
sudo dnf install xclip   # Fedora

Then, to copy the output of a command to the clipboard:

echo "Hello, World!" | xclip -selection clipboard

To paste, use Ctrl+V in any GUI application. You can also use xclip -o to output the clipboard contents to the terminal.

Using The pbcopy And pbpaste Commands

On some Linux distributions, you might find pbcopy and pbpaste (often from the xsel package). These commands work similarly to xclip but are simpler to remember.

Install xsel if needed:

sudo apt install xsel

Then copy text to the clipboard:

echo "Copy this text" | xsel --clipboard --input

To paste from the clipboard:

xsel --clipboard --output

These tools are essential for scripting and automating clipboard operations in Linux.

Copying Text In Text Editors (Vim, Nano, Emacs)

If you work in command-line text editors, copying text follows different rules. Each editor has its own keybindings.

Vim

In Vim, copying is called “yanking.” To copy text:

  1. Enter visual mode by pressing v (character-wise), V (line-wise), or Ctrl+v (block-wise).
  2. Move the cursor to select the text.
  3. Press y to yank (copy) the selection.
  4. Move to the desired location and press p to paste below the cursor, or P to paste above.

To copy text to the system clipboard from Vim, use "+y after selecting. This requires Vim compiled with clipboard support.

Nano

In Nano, copying text is less intuitive. You need to mark text first:

  1. Press Alt+A (or Ctrl+6) to set a mark.
  2. Move the cursor to highlight the text.
  3. Press Alt+^ (or Ctrl+Shift+6) to copy the marked text.
  4. Move the cursor and press Ctrl+U to paste.

Emacs

In Emacs, copying is called “killing.” To copy text:

  1. Set the mark with Ctrl+Space.
  2. Move the cursor to select the region.
  3. Press Alt+w to copy (save to kill ring).
  4. Move to the target and press Ctrl+y to paste.

Copying Text From Files Using Command-Line Tools

Sometimes you need to copy text from a file without opening it. Linux provides several commands for this.

Using cat And tee

To copy the contents of a file to the clipboard:

cat file.txt | xclip -selection clipboard

Or using tee to copy and display:

cat file.txt | tee >(xclip -selection clipboard)

Using head And tail

To copy only specific lines:

head -n 10 file.txt | xclip -selection clipboard
tail -n 5 file.txt | xclip -selection clipboard

Using sed Or awk

For more precise copying, use sed or awk:

sed -n '5,10p' file.txt | xclip -selection clipboard
awk 'NR>=5 && NR<=10' file.txt | xclip -selection clipboard

Copying Text With The screen And tmux Utilities

Terminal multiplexers like screen and tmux have their own copy modes. They are useful for copying text from scrollback buffers.

In screen

  1. Press Ctrl+A, then [ to enter copy mode.
  2. Move the cursor to the start of the text and press Space.
  3. Move to the end and press Space again to copy.
  4. Press Ctrl+A, then ] to paste.

In tmux

  1. Press Ctrl+B, then [ to enter copy mode.
  2. Use arrow keys or vi keys to navigate.
  3. Press Space to start selection, move to end, then press Enter to copy.
  4. Press Ctrl+B, then ] to paste.

Copying Text From The Terminal To The System Clipboard

If you want to copy terminal output directly to the system clipboard for use in GUI apps, use xclip or xsel with the clipboard selection. For example:

ls -la | xclip -selection clipboard

Now you can paste the directory listing into a text editor or email with Ctrl+V.

Copying Text Without A Mouse

In pure terminal environments (like over SSH), you might not have a mouse. In that case, use the keyboard shortcuts mentioned earlier, or rely on screen/tmux copy modes. You can also use cat with less and copy by selecting text with Shift and arrow keys in some terminals.

Common Pitfalls And Troubleshooting

Sometimes copying text in Linux does not work as expected. Here are a few issues and solutions:

  • Pasting does nothing: Ensure you are using the correct clipboard. The primary selection (middle-click) is separate from the system clipboard.
  • Ctrl+Shift+C does not work: Check your terminal emulator settings. Some terminals allow you to remap these shortcuts.
  • Text is garbled: If you copy from a terminal with special characters, use xclip with the -t option to set the MIME type.
  • Clipboard not shared between terminals: Each terminal may have its own clipboard. Use xclip to unify them.

Automating Text Copying With Scripts

You can create simple scripts to copy text automatically. For example, save the following as copy-to-clipboard.sh:

#!/bin/bash
xclip -selection clipboard "$@"

Make it executable and use it like:

./copy-to-clipboard.sh "Text to copy"

Or pipe output to it:

echo "Hello" | ./copy-to-clipboard.sh

Copying Text In Different Desktop Environments

Linux desktop environments like GNOME, KDE, and Xfce have their own clipboard managers. For example:

  • GNOME: Uses gnome-screenshot and gnome-terminal with standard shortcuts.
  • KDE: Has klipper for clipboard history.
  • Xfce: Uses xfce4-clipman for clipboard management.

These tools can store multiple clipboard entries, making it easier to copy and paste text across applications.

Copying Text From PDFs And Web Pages In Linux

If you need to copy text from a PDF, use pdftotext (from poppler-utils) to extract text:

pdftotext file.pdf - | xclip -selection clipboard

For web pages, you can use lynx or w3m to dump text and then copy it:

lynx -dump https://example.com | xclip -selection clipboard

Copying Text With ssh And Remote Servers

When working on a remote server via SSH, copying text to your local clipboard requires extra steps. One method is to use ssh with X forwarding:

ssh -X user@server

Then use xclip on the remote server, and it will appear on your local clipboard. Alternatively, you can copy text to a file and then use scp to transfer it.

Using Clipboard Managers For Advanced Copying

Clipboard managers like CopyQ, Parcellite, or Diodon allow you to store multiple clipboard entries. They are especially useful if you copy text frequently and need to access previous items.

Install one with:

sudo apt install copyq

Then use it to manage your clipboard history.

Copying Text In The Terminal With tee And Redirection

You can also copy text by redirecting output to a file and then reading it. For example:

command > output.txt
cat output.txt | xclip -selection clipboard

Or use tee to do both at once:

command | tee output.txt | xclip -selection clipboard

Copying Text From Man Pages

To copy text from a man page, you can use man with col to strip formatting:

man ls | col -b | xclip -selection clipboard

This gives you clean text without control characters.

Copying Text With printf And echo

For simple text, use echo or printf:

printf "Copy this line\n" | xclip -selection clipboard

Copying Text From Log Files

Log files are often large. Use grep to filter and copy relevant lines:

grep "error" /var/log/syslog | xclip -selection clipboard

Copying Text With awk And sed For Precision

To copy specific columns or patterns, combine awk or sed with xclip:

awk '{print $2}' file.txt | xclip -selection clipboard
sed -n '/pattern/p' file.txt | xclip -selection clipboard

Copying Text In A Script Without User Interaction

For automation, you can use xdotool to simulate keypresses, but this is less reliable. Instead, stick with xclip for script-friendly copying.

Frequently Asked Questions

How do I copy text in Linux terminal without a mouse?

Use keyboard shortcuts like Ctrl+Shift+C and Ctrl+Shift+V, or use screen/tmux copy modes.

What is the difference between primary selection and clipboard in Linux?

The primary selection is for middle-click paste, while the clipboard is for Ctrl+V paste. They are separate buffers.

Can I copy text from terminal to a GUI application?

Yes, use xclip -selection clipboard to copy to the system clipboard, then paste with Ctrl+V.

How do I copy multiple lines in Vim?

Enter visual mode with V, select lines, then press y to yank. Use "+y to copy to system clipboard.

Why does my copied text not paste in Linux?

You might be using the wrong clipboard buffer. Try middle-click or check if xclip is installed.

Conclusion

Mastering how to copy text in Linux is essential for efficient work. Whether you use the mouse, keyboard shortcuts, or command-line tools, there is a method that