Search code examples
bashautohotkeykeypress

ahk send alternative in bash script


I want to achieve in a bash script, that when I press any key (e.g. F5) and then type any word (e.g. "hello world"), regardless of whether I positioned in the terminal, or text-editor or browser...

While I was using Windows, then I used Autohotkey for that.

How could I achieve that with bash script in Linux?


Solution

  • While as @Socowi mentioned, you will likely not be able to do this with bash alone, you can use AutoHotkey Linux alternatives like AutoKey for this.

    For your specific question, see this guide for setting up and configuring AutoKey on Linux.

    More specifically, scroll down to the section labeled "Inserting repetitive text using a hotkey" for how to configure a text-typing hotkey:

    Inserting repetitive text using a hotkey

    Abbreviations is only one way of inserting text using AutoKey. If you prefer, you can also insert text using a keyboard shortcut, which doesn't require typing an abbreviation.

    To insert some text that you need to type frequently using a keyboard shortcut, click the Set button next to Hotkey, then click Press to Set in the newly opened window, and press the key combination you want to use for this phrase:

    AutoKey set hotkey screenshot

    Make sure you use a hotkey that's not already in use or else it may not work / it will interfere with the already set hotkey. To try this out, set it to something like Ctrl + F1 (which shouldn't be set by the system at least).

    Remember to click the Save button in the AutoKey toolbar after setting the phrase hotkey, or else the changes won't have any effect. Now try it out: focus a text editor, some text field in a web form, etc., and press the hotkey you've set in AutoKey - the Testing AutoKey text expansion on Linux phrase should be automatically inserted as soon as you press the keyboard shortcut.