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

13 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.

Leave a Reply

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