Date Tags Linux

Sometimes you say to yourself, "What the hell am I doing copying this text in the terminal with the mouse? I am supposed to be a programmer!"

Regular people avoid going on a Yak-Shaving mission at these times! I of course threw on my Yak-Shaving uniform and preceded into the seedy depths of the internet to the lands where the great Nix Wizards have been migrating (Reddit). I returned relatively unscathed and now bring to you this gem of CLI goodness!

Install xsel

No one really knows what xsel does. That knowedge has been lost unfortunately...

Kidding, of course. XSel is a "command-line program for getting and setting the contents of the X selection". You can read more about it here:

sudo apt install xsel

The command we are going to use

printf %s "$(history -p !!)" | xsel --clipboard

Let's break this command down a bit:

printf %s - is like echo in bash but it has more features. If you have ever programmed in C, then this command should look familiar to you.

"" - Everything in quotes is the string we want to printf

$ - This is parameter expansion.

history -p - refers to the history command and the -p modifier means to "Print the new command but do not execute it."

!! - this is something called an Event Designator that refers to the last command entered.

| xsel --clipboard - finally, we pipe the result of all of that into xsel with the --clipboard argument so it knows to send it to the clipboard!

So, I just want you to type out that command every time you want to copy the last entered command to the clipboard!

No, if only there were some way for us to create a new command from a series of bash commands...

Create an alias for the command

Feel free to check out my prior post on creating an alias, but its so simple you probably won't need to:

Let's use type to make sure the command I want to use cpx is not already in use.

tim@ubuntu1604:~$ type cpx
bash: type: cpx: not found

Good, now let's use the alias command:

tim@ubuntu1604:~$ alias cpx='printf %s "$(history -p !!)" | xsel --clipboard' 

What if you restart your computer or terminal session?

Restart? You must be on Windows!

Actually, its important to remember that an alias command will only stick around for the remainder of your terminal session. Because of this we need store it in a file to have it loaded each time bash starts. Luckily there is a specific file for this in your home folder.

Add the alias to .bashrc

Go to your home folder cd ~ and in it you will find a file called .bashrc.

Open it up with you favorite text editor and add at the bottom of the file:

alias cpx='printf %s "$(history -p !!)" | xsel --clipboard'

Note, that .bashrc also provides a mechanism where you can create a file called .bash_alias and it will import those into .bashrc making them available in your terminal. So, you can do that or just throw them into your .bashrc.

cpx should work now.

Now you can restart your terminal and cpx will be available to you!


comments powered by Disqus