Score:1

How to add custom shortcut which pastes a fixed string into the current cursor position?

fr flag

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

Score:1
cn flag

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 like rofi or dmenu, or have the script first copy and delete a text fragment you typed, to replace it with the full text.

Hakaishin avatar
fr flag
Thanks a bunch, Espanos did the trick for me, what a simple and intuitive package :) I will leave the question open a bit longer, in case somebody has a Wayland solution, since that will be a problem for me in a few months :D
vanadium avatar
cn flag
It already has some Wayland support with some limitations: https://espanso.org/docs/next/install/linux/#install-on-wayland
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.