Use xdotool and bash to paste strings via pre-defined hot keys.

Linux HotKey to Type a Pre-defined String (xdotool)

Depending on your distro, you should have a keyboard shortcut program already installed.

Use xdotool and bash to paste strings via pre-defined hot keys.

I wanted to be able to paste the following very oftenly typed command when pressing a certain key combination:

1
cd /home/admin/web/*/public_html/

First, you’ll need xdotool

1
sudo apt-get install xdotool

I was having trouble putting the commands directly into the keyboard shortcut entry, so I put the commands in a mini bash script.
Open a text editor and paste the following:

1
2
3
cdp=$(echo 'cd /home/admin/web/*/public_html/')
sleep 0.3
xdotool type --clearmodifiers "$cdp"

Save the file, I saved mine as /home/user/scripts/xdotool-cdp.sh

Next, add a keyboard shortcut for the following (I used Alt+P):

1
sh /home/user/scripts/xdotool-cdp.sh

Now when you press Alt + P keys, it executes the script.
The script sets a variable cdp to echo cd /home/admin/web/*/public_html/
Then it sleeps for 0.3 seconds.
Then it types cd /home/admin/web/*/public_html/
Make sure to include –clearmodifiers if you’re using keys such as ctrl, alt or shift as the shortcut as it will affect the way xdotool types.

Check it out:

Use xdotool and bash to paste strings via pre-defined hot keys.

Leave a Reply

Your email address will not be published. Required fields are marked *