# cat /dev/head

a developers log

Simple functions to simplify copy/paste operation X11 terminal

What?

From time to time I need to paste some text from X clipboard into terminal and do some transforming on it - be it sed or grep or gpg decryption. This can be done by pasting into a file and then working on that file or by typing long command line with xclip -o -selection clipboard as a beginnig. There also times when I want to copy some text from my terminal into a browser or to Slack (for example encrypted output from gpg). This can be done thanks to tmux and it's plugin tmux-yank, but it requires selecting text either with keys or with a mouse. Also there's a way with xclip -i -selection clipboard command.

To make my life easier, I've came up with two functions to work with clipboard using xclip command.

Why?

There probably are solutions to this problem over the Internet, however it was much quicker to write those two function in my zshrc, than to search for someting like that over the Internet.

I show them here, maybe someone will find them useful.

Code

xcopy

function xcopy() {
    if [ $# -eq 0 ]; then
        xclip -i -selection clipboard
    else
        cat "$1" | xcopy
    fi
}

First one is xcopy function. if statement is here to enable using this function in conjunction with a pipe. For example:

printf "%s\n%s\n%s\n" "foo" "bar" "baz" | xcopy

It can also copy text from a file given as a first argument.

xpaste

function xpaste() {
    xclip -o -selection clipboard
}

Second is xpaste function. This one is a simple shortcut for long xclip command.


You are free to use those functions anyway you like. Tested and written for Zsh, but should work in any shell (you may need to remove the word function)