Understanding Primary Selection
Linux desktop environments offer a powerful but often overlooked feature called Primary Selection, distinct from the traditional clipboard system. When text is highlighted in most applications, it automatically becomes available in the Primary buffer. Pasting is accomplished via the middle mouse button, eliminating the need for keyboard shortcuts like Ctrl+C and Ctrl+V. This mechanism proves particularly valuable during mouse-intensive tasks where switching between mouse and keyboard would interrupt workflow.
A practical advantage emerges when dealing with websites that restrict code copying through login walls or paywalls. Since Primary Selection captures any highlighted text regardless of JavaScript restrictions, users can bypass these limitations by selecting the desired content and pasting it directly with the middle mouse button, a method often faster than opening browser developer tools.
Piping Terminal Output to Clipboard Buffers
Copying long terminal outputs for documentation or troubleshooting can be cumbersome, requiring careful scrolling and selection. Command-line tools like xclip allow direct redirection of program output to either the Primary or Clipboard selection buffers, streamlining the process significantly.
Installation on Debian-based systems:
sudo apt-get update && sudo apt-get install xclip
Basic usage patterns for redirecting output:
# Send to Primary selection
pwd | xclip -selection primary
# Send to standard clipboard
cat /var/log/syslog | xclip -selection clipboard
Adding shell aliases simplifies these commands. Add the following to ~/.bashrc or ~/.zshrc:
# Convenient shortcuts for clipboard operations
alias toclip='xclip -selection clipboard'
alias toprim='xclip -selection primary'
With aliases configured, the workflow becomes more concise:
# Redirect silently
date | toprim
# Display output while copying
ls -la | tee /dev/stderr | toclip
Retrieving clipboard contents from the terminal is equally straightforward using the -o flag:
# Output Primary selection contents
toprim -o
# Transfer between buffers
toprim -o | toclip
Transferring content from Primary to Clipboard proves useful in several scenarios. Primary selection gets overwritten with any new highlight operation, making it unsuitable for long-term storage. Additionally, certain applications—particularly those running under Wine or cross-platform Electron apps—may not properly support Primary selection, necessitating a transfer to the standard clipboard buffer for compatibility.