Share text from your terminal with QR codes


Share text from your terminal with QR codes

Often, there are times, that i want to share a text (link, command, search term etc) to my phone, from my main computer PC. For sure, you can send it over with an email, but i don't like this way, cause it's filling up my inbox/trash folder with useless emails, that often forget to delete. Also... sending the email... waiting to arrive at the other account, checking it, copy/pasting is taking too long.

The quick solution for me is to create a QR code, on the fly, inside the terminal (which is always open) and scan it with my smartphone. The QR reader application on the smartphone, has a button to immediately copy the text into the clipboard, so it's making the procedure much quicker.

To do that, we just have to install a small program called qrencode, which is available in the default repos of debian Linux distros. In Ubuntu and similar distros you just intall it with:

sudo apt install qrencode

By just executing the program with a string to encode, it wont do... we have to specify some options, so the QR code is encoded inside the terminal as text. For me, the full command line i use is this:

qrencode -lM -m2 -tutf8

  • -lM: is for error correction
  • -m2: is for emtpy space in the corners
  • -utf8: is for text encoding

Play with the settings and find the one you prefer and/or is better for your needs and terminal size. Last, you should create an alias, so you don't have to remember the command each time, like this:

alias qr="qrencode -lM -m2 -tutf8"

I prefer to use a 80x25 size terminal and the text i copy is often small... but we should use and exploit the full potential of the qrencode command. By using an image (graphics file), we can create bigger QR codes, that contain small paragraphs or even scripts!

This time we will create a function, as it needs more commands to achieve that, and we have to use parameters. The function is as follows:

function qrimg() {
  qrencode "$1" -o /tmp/qr.png
  xclip -selection clipboard -t image/png -i /tmp/qr.png
  start /tmp/qr.png
  sleep 3
  rm /tmp/qr.png
}

We create the function qrimg, which takes our text input and creates an image QR code, which is saved as /tmp/qr.png. Then we use the program xclip to copy the image to the clipboard, in case we want to paste it to another program like GIMP. This is an extra functionality. You can rem it, if you don't want it. After, we use the start command to open the image file, with our preferred image viewer program. We wait for three seconds and delete the image. The sleep command is mandatory or else the image viewer will not be quick enough to open the image, before it's been deleted. You can rem every line you think is no use for you.

You can add the function inside your .bashrc or .bash_aliases file. You could also make another function to open files like qrfimg mytext.txt instead of the above one. If the text you are trying to encode is too big, you will get a bunch of error messages :)

Add a comment

Previous Next