Getting presets onto X clipboard(s)

Here’s one thing I find really useful – being able to get presets onto the current X clipboard(s) via a hotkey, for stuff like the current timestamp in different formats.

What you’ll need is "xsel" – just install it through aptitude (or your favorite package manager).

Once installed, you can then make a script like this:

# Display a menu like:
# a - 20080715T025257
# b - 2008-07-15 @ 02:52:57
# c - 2008-07-15
# Selection: a

echo "a - $(date "+%Y%m%dT%H%M%S")
b - $(date "+%F @ %H:%M:%S")
c - $(date "+%F")"

read -p "Selection: " selection

case $selection in
		# 20080715T030047
		text=$(date "+%Y%m%dT%H%M%S")
		# 2008-07-15 @ 03:00:47
		text=$(date "+%F @ %H:%M:%S")
		# 2008-07-15
		text=$(date "+%F")
		# 20080715T030047
		text=$(date "+%Y%m%dT%H%M%S")

echo -n "$text"|xsel -p
echo -n "$text"|xsel -b

And assign it to a hotkey via a command like:

xfce4-terminal -x [fullPathToScript]/

Now just press your hotkey, hit [Enter] and you now have the current time in ISO 8601 format on your clipboard!

Oh, and you can also use xsel to do other things – like copying a file’s name / full path from your favorite file-manager’s context menu. For example, in Xubuntu (with Thunar) (you set this in "Edit → Configure custom actions…"):

Name:	 Copy _Full path		# The "_" is for the key accelerator.
Command: xfce4-terminal -x [fullPathToScript]/ %f
-------- And --------
Name:	 Copy file _name
Command: xfce4-terminal -x [fullPathToScript]/ %n
# Don't forget to set the "Appearance Conditions" (tab) for all files!


echo -n $1|xsel -b

You should now be able to copy the file’s full path from the context menu – I find it very useful for getting the full path of a file for file uploads.

Now, it’d be even cooler if we can extend this with the history of the X clipboards – if anyone have an idea how I can do this, please drop me a comment! – Or more precisely, how to collect the history of the X clipboards for use.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: