Goal: insert a date string (e.g. “2008/06/29”) in almost whatever text area/graphical program I’m using currently with a single keyboard shortcut.
Basic principle: copy the date string to the clipboard and emulate the Ctrl-V key combination, which activates the Paste operation in most programs.
- The current program must support Ctrl-V as the Paste shortcut (won’t work on the command line, for example).
- The current clipboard data is erased, replaced by the string inserted.
The bare steps I took
I’m using Ubuntu, but I guess this should work with most distributions, by adapting the installation instructions.
1. I installed the necessary utilities.
sudo apt-get install xsel xbindkeys xbindkeys-config xmacro
2. I created a default xbindkeys configuration file.
xbindkeys --defaults > ~/.xbindkeysrc
3. I wrote this shell script, which I saved on my local disk.
4. I configured xbindkeys using xbindkeys-config to launch the script when the Windows-D shortcut is pressed.
a. I loaded “xbindkeys-config” by typing that on the command line.
b. I created a new shortcut and associated it with the script.
c. I associated it with the Windows-D shortcut by pressing “Get Key” and then pressing Windows-D.
d. I saved and exited.
e. I arranged it so the “xbindkeys” command would run on every logon, which can be done by adding the line “xbindkeys” to a logon script (/home/…/.bash_profile, for example).
The underlying explanations
I often find it useful to insert the date in personal notes I take. So often, in fact, that it’s quite handy to automate the insertion. On Windows, there’s this handy app and scripting language called AutoIT which may be used to automate common tasks. But under Linux, quick googling doesn’t reveal any self-evident choice for an all-encompassing scripting language, so I went looking into more application-specific options.
The first key element to my solution, xsel, is a program that allows one to control the X selection and clipboard from the command line. The second utility, xbindkeys (and its configuration GUI, xbindkeys-config), as you guessed, allows you to associate keyboard shortcuts to commands. Finally, xmacro is a program that allows you to emulate specific keyboard key events, like key presses, and mouse events.
I therefore associated (using xbindkeys) a keyboard shortcut to a script that copies the date string to the clipboard (using xsel) and emulates the Paste keyboard shortcut (using xmacro). That’s a pretty complicated solution, but all in all it didn’t take so much time to set up. I commented the script so you may get a better understanding of the parameters used.
I could have used other shortcuts that Windows-D, but the Windows key isn’t used under Ubuntu, so this was a great occasion to capitalize on keyboard real estate.
As an alternative approach, which doesn’t replace the clipboard data, you can use xmacro to insert every single character in the date string, one at a time. I began by doing this, since it’s much simpler. The problem I ran into is instability: some programs need a delay between keypresses, otherwise they mix up the letters, and the insertion seems slow and sometimes misses letters. That limitation prompted me to try this solution.
If anyone has a suggestion as to how to improve this solution, feel free to post a comment about it.
Further reading and references
- http://whynotwiki.com/GNU/Linux_/_Desktop: useful information and examples on the use of xmacro, xbindkeys