Copying files is fun. Most of the time.
Open the RC file for your shell. I’m using ZSH, so I need to open
~/.zshrc.Create a new function called
copy:function copy() { }Within the function, use
catto return the contents of a file:function copy() { cat $1 }We’re using the
$1variable since we’re going to pass in the file that we want to copy.Pipe the output of
cattoxclip:function copy() { cat $1 | xclip -i }The
-ioption tells xclip to expect content from standard input.Tell xclip that we want to add this content to the secondary clipboard:
function copy() { cat $1 | xclip -i -seletion clipboard }In Ubuntu/Debian based Linux distributions, the main clipboard is accessed using the middle mouse button. Since I never use a mouse, this functionality is useless to me, so I need to address the regular
clipboard.Save and exit the RC file.
Source the file to get the updates:
source ~/.zshrcMake sure to source your actual RC file. The destination might be different to what I’ve got here.
You should now be able to copy the contents of a file using
copy filename:echo "This is a test." >> test.txt copy test.txtPressing
ctrl+vshould now pasteThis is a test.! Don’t forget to add your RC file to version control like Git!