Having the ability to drive your development using just a keyboard is very productive. However, when you are using a terminal and have to copy the output of a command to use it somewhere else, it breaks your flow, you need to move your hands away from your keyboard, use the mouse to select the text and then copy it.
When I want to copy passwords to be used elsewhere from my browser, I usually open the developer tools console, inspect element and click on the password input box and then run the following code:
1 | copy($0.value) |
Chrome sets $0
to refer to the currently selected DOM element and $0.value
will give us the value of the password field and sending it to the copy
function copies this text to the OS clipboard.
I have a similar script set up for my terminal, when I want to copy the output
of a command like rake secret
I run the following command:
1 | rake secret | xc # copies a new secret to the clipboard. |
xc
is aliased to the following in my bashrc:
1 | alias xc='tee /dev/tty | xclip -selection clipboard' |
This command prints the output to the terminal (using tee /dev/tty
) and copies
it to the OS clipboard using the xclip
package.
I wanted the same ability in my ruby and elixir REPLs. It was pretty straightforward to do in ruby. Here is the annotated code:
1 | puts 'loading ~/.pryrc ...' |
Below is a similar script for Elixir:
1 | IO.puts("loading ~/.iex.exs") |
1 | iex(2)> :crypto.strong_rand_bytes(16) |> Base.encode16 |> H.copy |
All these utilities (except for the browser’s copy
function) depend on the
xclip
utility which can be installed on ubuntu using sudo apt-get install
xclip
. You can emulate the same behaviour on a Mac using the pbcopy
utility,
you might have to tweak things a little bit, but it should be pretty straightforward.
You can do the same in your favorite programming language too, just find the
right way to spawn an xclip
process and send the text you want to be copied to
its’ stdin. Hope this makes your development a little more pleasant :)