I’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.
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.
does not work.
see this pastebin for analysis
http://pastebin.com/raw/yH4XEzg2
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.
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.
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.
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).
Never tried yad. I’ll definitely take a look. Thanks for the recommendation!
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.
I just published a small change where using xdotool uses
getwindowfocus
instead ofgetactivewindow
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 🙂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.)
Hey Lee.
From the installation step 2, you mentioned “Create a keyboard shortcut that calls `~/bin/texpander.sh`”.
Can you further elaborate this step?
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.
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
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.
Great script. How can I get the current time and date to output?
It doesn’t have any dynamic text capabilities at this point. It just pastes text from snippet files into the document you’re working on.
See my comment below on this topic.
write the following code under the definition of
base_dir=$(realpath “${HOME}/.texpander”) :
date +”%d.%m.%Y %H:%M” > “${base_dir}/timestamp.txt”
then a file “timestamp” will be shown at the list using textexpander
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
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.
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.
Welcome to 2019.
I agree – this would be an awesome feature. Enough for me to step out of macos completely.
Is there any follow up on this.
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.
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
I just type abbreviation name and it selected that abbreviation, so thank you
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?
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.
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.
Some kind of timestamp would be very useful. E.g.:
sig.txt contains
Francesco Porta %Y/%m/%d
i.e. using the syntax of format as in date command.
Or the solution proposed by trae
Thx!
This is great. Thank you!
… and then I thought to myself : “How hard can it be?”
if you have installed xdotool xclip and xsel you can add to the beginning of the script the following lines:
sleep 0.5
xdotool keydown ‘Shift_L’
xdotool key Left
xdotool key Left
xdotool key Left
xdotool key Left
xdotool keyup ‘Shift_L’
xclip -o | xclip -sel clip
and then replace the “name=…” line with:
name=$(xclip -selection c -o)
and change the:
path=”${base_dir}/${name}”
to
path=”${base_dir}/${name}.txt”
and
if [ -f “${base_dir}/${name}” ]
to
if [ -f “${base_dir}/${name}.txt” ]
you have yourself a fullblown textexpander 🙂
the only caveat is that your txt file names have to be exactly the same length, but for me it has always been 4 characters for textexpander.
And then you type your four magic characters, hit the hotkey of your choice and watch the last four characters turn into snippet created beforehand.
P.S. Sorry for my english (it is my fourth language after all) and i know the code is not the most elegant, but i just got acquainted with bash scripting.
That is a very cool option for enhancing this tool. Thanks for sharing 🙂
Great tool.
Thank you
when using texpander.sh in Terminal:
x bin]$ texpander.sh
xdo_get_focused_window_sane failed (code=1)
xdo_focus_window reported an error
cat: /proc//comm: No such file or directory
find: ‘/home/jaap/bin/.texpander’: No such file or directory
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
is texpander looking for .texpander directory in /bin?
Should .texpander with abbreviations be in ~/???
What did I do wrong?
Another vote for a timestamp please.
Follow on to my request for a timestamp feature. I created this simple script to add a timestamp:
#!/bin/sh
date | xclip -sel clip
sleep 0.4
xdotool key ctrl+v
Could this be incorporated somehow?
Keep up the good work.
Wonderful work!
I added the much desired feature of adding the output of scripts by replacing the pasting lines with the following code:
# Put text in primary buffer for Shift+Insert pasting
if [ -x “$path” ]
then
echo -n `”$path”` | xsel -p -i
else
echo -n “$(cat “$path”)” | xsel -p -i
fi
# Put text in clipboard selection for apps like Firefox that
# insist on using the clipboard for all pasting
if [ -x “$path” ]
then
echo -n `”$path”` | xsel -p -i
else
echo -n “$(cat “$path”)” | xsel -b -i
fi
Now I can add the the following script as ‘date.sh’ in the ~/.texpander directory:
#!/bin/sh
date “+%F”
And also the script ‘time.sh’:
#!/bin/sh
date “+%T”
Just have to make them both executable:
chmod 750 ~/.texpander/date.sh
chmod 750 ~/.texpander/time.sh
And voila – after the hotkey and my selection I’ll get the current date / time inserted as desired (in iso format / 24h format).
Additional note: the following article proposes a similar solution, but in addition mentions the possibility to use ‘easybashgui’ as a way to show graphical display elements on all kinds of desktops – this could also be helpful for some other desktop environments:
http://www.linux-magazine.com/Issues/2014/162/Workspace-Text-Expander
Bravo, thank you!
It doesn’t paste anything in Linux Mint 19, xfce.
Popup comes up. Lets me select my shortcut,
then pastes nothing, or the current/previous contents of the clipboard.
Attempted pasting into xfce4-terminal
kate, text-editor, thunderbird new email, web browser.
(Incidentally, if you could write an install script, a good default keystroke would be ctrl-alt-v as it is rarely used, and is somewhat related to paste, (ctrl-v, or ctrl-shift-v)).
Looks like a good tool, but may be not working well for systems that have multiple bus routes to the clipboard like xfce uses its own thing, but will also use dbus. I noticed zenity throws an error when run from the command line :
Couldn’t register with accessibility bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
Never mind… I know why people may be having problems. You need a check at the beginning of the script that checks for:
xsel
xdotool
zenity
Probably something like:
which xsel
xseltest=$?
which xdotool
xdotooltest=$?
which zenity
zenitytest=$?
if (xseltest || xdotooltest || zenitytest ==1 ) {
echo “Please make sure that xsel, xdotool, and zenity are installed.”
exit 1
}
(Sorry if my syntax sucks, been awhile since I did bash programming)
If they don’t exist, throw an error and tell people what they need to install.
Also, the second xdo line adds extra line feeds. so single spaced items come out double spaced, if I get a chance I’ll play with your regex, and see if I can fix it.
Thanks for this feedback! With the current project I’m working on I’ve been using a MacBook Pro so I haven’t had a whole lot of time to update or work on Texpander. But, if you are able to add a fix/enhancement, let me know and issue a pull request on GitHub so I can incorporate your updates for everybody. Thanks again!
hi,
The tool is working great in email or text editor. But i was not able to get it running in pycharm.
Do you or anybody else know why?
Best regards
Markus
Trying to get it to work in Manjaro KDE.
I made my keyboard shortcut, and created the directory in Home.
When I run the shortcut, I can see in the file manager that the .sh file is selected, but when I type an abbreviation, it doesn’t get expanded.
If I run the .sh file in terminal, it brings up the abbreviations I created, and if I select an abbreviation, the output goes into the terminal — not into the application I am using.
Never mind, apparently there was some conflict with the recommended shortcut, and it seems that the conflict is unique to my system.
I set a different keyboard shortcut: Alt+Z and it works perfectly. Thanks for the great tool!