We all probably use copy&paste on a daily basis, but did you know that manipulating your clipboard from the command line or a script is really easy?
(No, this is not about russian hackers and manipulating elections, sorry if you were misled. Here, we’ll talk about your system clipboard, or X selections.)
I had never paid much attention to the fact that you can read from and write to your system’s clipboard from the command line, until I found key mappings in my window manager (awesome-wm) config that do so.
I was customizing my keyboard shortcuts when I came across two entries that would copy between the primary and the clipboard selections.
In linux systems, the primary selection is the one defined by currently selected text, which you usually can paste somewhere else with the middle mouse button. The clipboard selection is the one copy & paste operate on.
I will only use the primary selection in this post for the sake of simplicity and because it is
xclip
’s default behaviour. By simply adding -selection clipboard
to the commands below will
affect the clipboard instead of the primary selection.
xclip
xclip
is a very simple tool that allows manipulation of your X selections. Its basic usage:
xclip -i - reads text into the X selection
xclip -o - prints the selection to stdout
For example with:
watch xclip -o
I can always see what is in my selection - which is pretty dumb.
Or after executing:
xclip -i <<<'foobar'
I have the word foobar
in my selection which I can paste anywhere with the middle mouse button.
Good to know, but why would I need this?
A Real-World Use
While working on a project that involves a double opt-in via an SMS containing a verification code, our development workflow for moving through this step while manually testing the frontend would always be:
- click a button in the frontend that sends the verification
- scroll through a much too verbose (local) server log to find the generated validation code
- copy the code from the log
- switch back to the browser and paste the code into an input field
This is not a lot of work, but if you have to do it dozens of times every day, any simplification of the process makes the developer’s life better. Armed with my new knowledge about manipulating selections I set out to drastically improve the team’s quality of life.
In the python code that generates the validation SMS, if and only if we are in a local development environment, I simply copy the validation code to my X selection with:
os.system(f'echo {validation_code} | xclip -i')
(Note that pythons os.system()
invokes sh
and not bash
, so I have to use an echo+pipe combination
instead of the <<<
redirection I used in the bash example above)
And that simplifies the process to:
- click the button in the frontend that sends the verification
- middle-mouse click in the input field for the code
Which saves us a lot of manual clicking, scrolling and copy/pasting every day.
It’s really just a simple hack but everyone who uses it seems to love it.
Do you have any other fun or practical uses for xclip? For reading or writing to your X selection or clipboard?
Let me know at bd@gs-9.com!