DIY Text Expander

Article from Issue 162/2014

A couple of utilities and a dash of Bash scripting are all you need to roll out a simple yet flexible text expander.

A tool that can expand abbreviations and insert ready-made text snippets can come in handy in many situations. With a bit of Bash scripting and a couple of existing utilities, you can easily build your own text expander and learn a few clever techniques in the process.

The starting point of this project is a simple Bash script published on the Arch Linux forum [1]. This nifty script uses a combination of the XSel [2] and xdotool [3] tools to replace an abbreviation with the related text snippet (Listing 1). The xdotool, which simulates keyboard input and mouse activity, is used to select and cut the abbreviation text (by simulating the Ctrl+Shift+Left Arrow and Ctrl+X keyboard shortcuts).

The cut abbreviation is then set as the X selection with the XSel tool. The script then fetches the appropriate text file and copies its contents as the X selection. Finally, the xdotool pastes the copied contents by simulating the Ctrl+V keyboard shortcut. So, when you type foo and run the script, it replaces the abbreviation with the contents of the ~/.snippy/foo text file.


Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95