Texpander – Text Expander For Linux

Text Exapander for Snippets in LinuxI’ve been using Ubuntu for a long time and have been lamenting the sad state of autokey and it’s relationship with Firefox and Thunderbird. Autokey is also a little more than I even need. I just want a quick way to expand a text snippet into a longer chunk of text. For example, I want to type sig and have it expand into my complete email signature – not matter what browser or email client I’m using. So I put together this tiny little script to do just that. Here’s how it works.

There are a variety of different little things I tend to type over and over.  I answer support questions for Cart66, the WordPress Ecommerce Plugin and there are some common questions that tend to come up on a regular basis. So I have saved answers for those common questions. Another example is my contact information. If I am sending a personal email I might put a different email signature with my personal contact information as my email signature. If I am sending a business email, I’ll use a different signature. I also do a lot of coding and there are various code snippets I use repeatedly.

There are browser add-ons that can do text snippet expansions. One of the best ones I’ve found is Auto Text Expander for Google Chrome another one is My Words for Firefox. Of course there’s also Autokey for Ubuntu, but it doesn’t work in Firefox or Thunderbird. I just wanted a simple, centralized tool that worked with everything where I could just type a little abbreviation and have it expand into whatever larger text block I needed regardless of what app I happen to be in. There is Snippy which uses dmenu to do a fairly similar thing to what Texpander does. But I prefer using zenity so I can just type in whatever abbreviation I need rather than using dmenu to select the abbreviation.

So, I wrote this really small bash script which you can assign to a keyboard shortcut. When run, it will prompt you for an abbreviation using zenity. Then paste the contents of the file in your ~/.texpander directory matching the abbreviations you just entered. If you have your email signature in a file called ~/.texpander/sig.txt you would enter the abbreviation sig when zenity prompts you. It looks like this.

texpander-screenshot

To use Texpander:

  • Start writing an email to somebody (or start editing any document)
  • Put your cursor where you want your email signature to be pasted
  • Type ctrl+space (or whatever keyboard shortcut you set up)
  • A zenity window will appear asking for your abbreviation
  • Type in sig and hit Enter (or click “OK”)
  • The contents of ~/.texpander/sig.txt is pasted into your document

The terminal works a little differently from other GUI apps in that you have to type ctrl+shift+v to paste stuff. In texpander.sh there is a check to see if the active window is a terminal. If so, it will paste using ctrl+shift+v if not then it will paste normally as ctrl+v

Update 2/2/2017

I’m really happy to see all the comments and also really appreciate the contributions on github. I have gotten a couple questions about how to create a custom keyboard shortcut to launch Texpander. I’m on Ubuntu 16.04 and all you have to do is…

Launch the keyboard preferences (part of System Preferences). I just fire Dash by pressing the super key and then type keyboard like this.

Then you’ll see the keyboard preferences window and you can create a custom shortcut, like this:

When you click the ( + ) a little window will open where you can name the shortcut and specify the command to run. The command is just the absolute path to the location where you installed texpander.sh

It might not be immediately obvious how to actually set the keys that trigger the shortcut. All you do is click on the window where the key sequence is located.

Installation details and more information about how to use Texpander is available on github.

Download from github

25 thoughts on “Texpander – Text Expander For Linux

  1. stefan says:

    does not work.

    see this pastebin for analysis

    http://pastebin.com/raw/yH4XEzg2

    • Lee says:

      Try adding a .txt extension to your text files. For example, ~/.texpander/something.txt

      Texpander adds the .txt extension to whatever you type in the abbreviation text box. I like having the .txt extension on the text files so you can open them easily in your text editor associated with the .txt file extension.

  2. Keith Peters says:

    Easy enough to set up. Worked right off the bat. I’m used to text expander apps on other platforms that will automatically parse and replace any missing tokens, but there’s a lot of black magic that goes into those. This is really simple, controllable and extendable. I’ll be giving this a nice test drive for a while. Thanks.

  3. Keith Peters says:

    Zenity is pretty cool. I wan’t familiar with it. I was able to easily alter your script to have zenity bring up a list of abbreviations to choose from, rather than having to remember, getting the file names by listing the .texpander dir.

    base_dir=”${HOME}/.texpander/”
    abbrvs=$(ls $base_dir)
    name=$(zenity –list –column=Abbreviations $abbrvs)
    path=$base_dir$name

    I removed the .txt extension part to make the list look better this way. Works great. Thanks again.

    • Joe says:

      If you like zenity, take a look at yad. It’s a fork of zenity which has a number of additional features and is actively being supported and developed. I use it a lot (not in AutoKey).

  4. xircon says:

    Weird problem with cairo-dock – if you detach an item, say a clock and pin on top, the script picks up the process id of cairo-dock and tries to paste to that id.

    Unpin or return to dock, the script works as expected.

    • Lee says:

      I just published a small change where using xdotool uses getwindowfocus instead of getactivewindow to find the pid of the window to paste into. That seems to help me in certain situations. Let me know if that helps with your cairo-dock issue. Thanks for trying it out 🙂

  5. Joe says:

    Nice tool. It will definitely meet some people’s needs.

    A few comments. AutoKey does work in Firefox and Thunderbird. I use it all the time – especially in Thunderbird. I didn’t say it works perfectly – sometimes things change in those apps and you have to adjust your phrases or macros.

    There is also a Python 3 branch of the project
    https://github.com/guoci/autokey-py3
    which has more fixes and a few additional abilities than our original branch
    https://github.com/autokey/autokey
    (We don’t currently have an active developer, but we do support our users.)

  6. Vicky says:

    Hey Lee.

    From the installation step 2, you mentioned “Create a keyboard shortcut that calls ~/bin/texpander.sh“.

    Can you further elaborate this step?

    • Lee says:

      Hi Vicky. You’re not the only one who has asked for details on setting custom keyboard shortcuts. So I just updated this post with some instructions on how to do that.

  7. Aleksandr says:

    Hey,

    Whenever I have something in my clipboard – it just copy-pastes that instead of the abbreviation. So I got to clear my clipboard via terminal for it to start working again.

    The other thing is – it seems to work in stuff like TextEdit but not on my online web email.

    Anyway great idea, I used to use TextExpander on MacOSX, but having some flaws that makes it unusable for me at this point.

    P.S. Using latest Ubuntu, not sure if that might have something to do with it.

    All the best,
    Aleksandr

    • Lee says:

      Hmm… I’m not able to reproduce any of these issues on my own. I’m running Ubuntu 16.04 on a System76 Oryx Pro. I don’t have to clear the clipboard and it works for me in Gmail, my WordPress editor, etc. It’s going to be hard for me to troubleshoot this without being able to reproduce the problem, but I’ll see what I can figure out. Thanks for letting me know about this.

  8. Paul says:

    Great script. How can I get the current time and date to output?

  9. H. Stoellinger says:

    I am trying to run texpander with Thunderbird (Linux/Mint 17.3, KDE). After defining and using the shortcut (strg+space), when I type an abbreviation – e.g. ra – a display box appears, in which the NAME of the file in my ~/.texpander directory, i.e. ra.txt, appears, instead of a list of my abbreviations within that file. Surely I must be missing something, or is there maybe some problem with the KDE-desktop.
    Regards
    H. Stoellinger

    • trae says:

      The names of your files in the .texpander folder are the abbreviations themselves, and the contents of those files are the text to be pasted.

  10. trae says:

    What about adding support for pasting the output of a .sh script? For instance, I could write a script called “date.sh” and throw it in my .texpander folder, and when I use that abbreviation the script is run and its output pasted. In this case the script returns the date and time, a common functionality found in text expansion utilities. This feature might turn your script into Linux’s answer for AHK.

  11. trae says:

    Please add support for folders in the .texpander directory (i.e. /signatures, /answers, etc.). Just make the script iterate through all the subdirectories of the .texpander directory instead of the top level. I have dozens of expansions in AHK on Windows and would love to be able to sort them out a bit. Otherwise, thanks so much for making this great script! AutoKey just doesn’t work for me and I haven’t been able to find an alternative oddly enough. Your script has brought an absolutely invaluable tool into Linux for me. Implementing .sh support like I mentioned above would make it a perfect alternative to AHK for someone like me coming in from Windows.

  12. Moeen says:

    Thank you so much for another great tool. I do everything you said and now when I press Ctrl+Space it show me a window from which I can choose abbreviation but not allowing me to enter abbreviation, any help on this?

    Here is screen shot:
    http://storage9.static.itmages.com/i/17/0802/h_1501662608_7311603_6fd139c2af.png

  13. Andrew Turner says:

    I have just upgraded from Uuntu 17.04 (Unity) to Ubuntu 17.10 (Gnome). Texpander now, more often than not pastes a v instead of the contents of the chosen text file. Any ideas why?

    • leehblue says:

      I’m using the new Pop!_OS by System76 right now which is based on Ubuntu 17.10 with their own tweaks to Gnome. I’m not having that problem, but I suspect it’s probably due to the logic that tries to decide if Texpander should paste with CTRL+V or CTRL+SHIFT+V. Let me know what app you’re trying to paste into or any other info you can think of that might help me duplicate the issue and I’ll try to whip up a fix. I generally paste into Gmail (in Chrome) and Gnome Terminal (using Vim) and both of those are working great for me. Sorry it’s not working out well for you.

  14. Andrew Turner says:

    Lee – Thanks for your prompt reply. I use your app to paste into many programs but mainly Gmail (in Chrome). Now it is very hit and miss – it sometimes pastes the text and sometimes it doesn’t. In LibreOffice & Gedit it pastes nothing at all. The only thing that I have tried to fix the problem is to push the delay up from 1s to 10s – but that did … precisely nothing! Any suggestions would be most appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *