Copy-Paste using the command line

9/3/2021

Today I learned how to copy-paste through the command line. So I thought I'll share it with you 😉

# Install xclip

So to copy-paste through the command line we are going to use xclip (opens new window). xclip ca be installed on ubuntu using the following.

sudo apt install xclip

# Using xclip

The simplest example on using xclip is the following.

# store "foo" in the clipboard
echo "foo" | xclip -in

# output the content of the clipboard
xclip -out

Before you use it you should know the following. xclip has three types of storage. primary, secondary and clipboard.

Per default it stores values you pass to it in primary. But what you typically want to do is to save it to your clipboard.

So what you are going to use typically is the following.

# store "foo" in the clipboard
echo "foo" | xclip -in -selection clipboard

# output the content of the clipboard
xclip -out -selection clipboard

I suggest that you add an alias to your ~/.bashrc or ~/.zshrc file so you have the clipboard set per default.

alias xclip='xclip -selection clipboard'

You could also go further and add aliases for copy and paste. Or if you are a vim user yy and pp or something alike 😉

alias copy='xclip -in -selection clipboard'
alias paste='xclip -out -selection clipboard'

You can also use xclip to copy-paste files.

xclip -in -selection clipboard some_file.txt

xclip -out -selection clipboard > target_file.txt

Notice the > in the output command.

And that's it! 😁