I have Ubuntu 20.04 and I would like to add a keyboard shortcut, which when I type 'asdf' quickly will copy paste a fixed string into the current cursor position. I tried adding a custom keyboard shortcut, but they can't be arbitrary keys. What are some other ways how I could achieve this? Can I somehow have arbitrary custom shortcut keys? I tried xbindkeys and xdotool but failed to shortcut a simple key:
# test
"xdotool type 'ls'"
F2
so how could I type asdf and have a fixed string copy pasted into the current cursor position and replace the just typed asdf
You are looking for a text snippet possibility, where typing a short text snippet expands to a phrase. A significant productivity booster indeed.
Your idea to do this by scripting is very valid. I run a homemade script myself based on
snippy
.Before moving to scripting, you may want to evaluate a few options. Caveat: all of these options do not work on the Wayland display manager, and for now, no alternatives seem available. If you are running a standard Ubuntu desktop, you need to switch to an X11 session or try scripting yourself using the experimental tools that simulate keyboard activity under Wayland (
ydotool
,wtype
).Espanso: https://espanso.org/docs/get-started/ Highly recommended to try. The setup is text based. It watches what you type, and a trigger, e.g.
:asdf
will be replaced by your custom string. Some Wayland support already made it into the application.Autokey: https://github.com/autokey/autokey Venerable Autokey once failed on me, after which I moved to a script based solution. I did not try it after that. However, it appears to be back and properly maintained again. This is extensively controlled using a GUI. It can do exactly what you want, i.e., replace text as you type an abbreviation you defined such as
asdf
.texpander: https://github.com/leehblue/texpander This is a bash script based solution, relying on
xdotool
. It uses Zenity to present a menu with text fragments you saved, which you can select and paste. You could adapt this script to use a better menuing system likerofi
ordmenu
, or have the script first copy and delete a text fragment you typed, to replace it with the full text.