Score:-5

How to type/insert a long text when pressed shortcut

pk flag

I need to find a unused shortcut in Ubuntu 18.04 and in Chrome and make it possible when I press that combination in Ubuntu it types or inserts a long text in a browser.

The background - I'm gonna take a certification exam which is browser based with a limited time. To save some time I'd like to press a shortcut and it types/inserts 5-10 bash aliases which I can use to save time. I will need to use this multiple times as this won't be a single terminal window.

cocomac avatar
cn flag
You should make sure the exam allows that, as most that I know of do not. If you use it against the rules, you are cheating. By default, I don't think Chrome can do that. _Assuming the exam allows it_, consider a program like [AutoKey](https://github.com/autokey/autokey).
cocomac avatar
cn flag
Does this answer your question? [How can I change what keys on my keyboard do? (How can I create custom keyboard commands/shortcuts?)](https://askubuntu.com/questions/254424/how-can-i-change-what-keys-on-my-keyboard-do-how-can-i-create-custom-keyboard)
vanadium avatar
cn flag
@cocomac this question, although the intens may be less noble, is specifically about how to paste a text snippet using a shortcut. Not to be found in the anwer you marked as duplicate. You may wish to retract your close vote or find another question about text snippets.
Score:0
cn flag

If you are running Xorg, the tools xsel and xdotool allow you to manipulate the clipboard and simulate pressing keystrokes. Following code will retrieve a text file stored on your computer using xsel, and paste it at the location of the cursor using xdotool:

#!/bin/bash
CLIPITEM="/home/user13/snippet.txt"
SHORTCUT=Control+1
# Read clip item into clipboard
xsel -bi < "$CLIPITEM"
# paste snippet
xdotool keyup $SHORTCUT key Shift+Insert

You bind the script to a custom shortcut key.

Adapt the variables CLIPITEM and SHORTCUT to your situation. In the example, Ctrl+1 is used, and that is registered in SHORTCUT. In the script, that shortcut is released before simulating the shortcut key to paste content. Otherwise, it could interfere.

xdotool and xsel only work with the Xorg display driver. If you are on a desktop running Wayland, you will need to use dedicated tools. Currently, ydotool can simulate key presses, and the tools in wl-clipboard can interact with the clipboard.

pk flag
I have created snippet.txt and aliases.sh. Executed aliases.sh it looked iike it inserted whatever I have had in clipboard at a time and after that shortcut did not work. I tend to believe that there's a missing bit here and it seems the whole aliases.sh should be bound to shortcut and this step is not explained.
vanadium avatar
cn flag
You did not mention system information. This will work on Xorg only. For Wayland, look for equivalent tools
pk flag
I'm not an ubuntu expert but I use 18.04. I read and it looks like 18.04 is xorg. I do not get how this will work. xdotool presses Shift + Insert once I press some Control+l. It will insert something from clipboard. Right? But who inserts correct stuff into clipboard? If let say I copy "the junk text" into clipboard. Then I press Control+l what will cause Shoft+Insert, but how my aliases appear in clipboard instead of "the junk text"? Whats the point of going Control+l instead of directly pressing Shift+Insert? Am I missing something?
vanadium avatar
cn flag
When you press the shortcut key, the long text will be inserted in the clipboard and pasted. So this is where you type your long text by just hitting a shortcut key, which is, as far as I understood, what you are asking. The manual way indeed is not that much longer. You open the long text file, copy the text (Ctrl+c), move to the target application and paste (Shift+Insert or Ctrl+V.
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.