Automate Repetitive Typing with Snippits
Posted by Kevin Purdy at 4:00 AM on February 2, 2008
If you're a regular Lifehacker reader and Linux user, you probably have a hole in your productive little heart where a great text substitution app should be. Our own Texter makes repetitive phrases a snap to call up in Windows, TextExpander gives Mac users loads of quick-text options, and you've read all about how you can save time with text substitution (or hit the play button above to see it in action). Say goodbye to unrequited speedy-text love with Snippits, a free, open source utility that can insert text, activate program shortcuts, correct spelling, and even run bits of code, all at the touch of one button. Here's a quick start guide to installing and customising Snippits to start saving time and keystrokes in Linux.
I'll be using Ubuntu 7.10 to walk through installation and setup. Snippits, however, can work from any system that supports the Ruby programming platform, and finding equivalents to the packages listed below shouldn't be too difficult in most Linux distros. I'll also note here that Snippits is still in development, under the steady hand of Ben Kudria, and might produce the occasional bug or oddity. Overall, though, I've found it to be a pretty helpful tool in a working Linux desktop.
Installation
We're going to hit the command line pretty hard here, but it's almost entirely a cut-and-paste affair, so no need to get scared off. The first thing we'll need to do is install Snippits' prerequisite packages. Open a terminal and enter the following:
sudo apt-get install ruby ruby1.8-dev rdoc rubygems libruby-extras xautomation xsel aspell libaspell-dev aspell-enOnce that's completed, enter the next two commands one at a time, answering "Yes" to any prompts it brings up:sudo gem install raspell
sudo gem install snippitsUPDATE: I've removed a suggestion and some code that modifies a system file. Installing Snippits using this guide should already take care of the suggested change, according to the program's creator. Next up, we'll make the directory in which we'll stash our custom text strings. In a terminal or in your file explorer, create the ".snippits" folder in your home directory; the command line code is:
sudo mkdir ~/.snippitsOne more task before we can really get started. Create a new text file in that .snippits directory called "do" (as in sudo gedit ~/.snippits/do) and paste only the following line in it before closing and saving (and trust me, this will make sense soon):{c}{s}{left}{c}{s}{c}x{c}{paste}
Set your shortcut
Snippits doesn't have the ability to trigger text strings from hitting the space bar or Enter key (yet), so you'll have to assign its function to a key you don't use often. I gave over the Pause/Break key to Snippits, since I almost never use it elsewhere, but you'll likely have your own preferred key to assign.
If you're running a GNOME desktop with Compiz, make sure you have the advanced configuration settings tool installed:
sudo apt-get install compizconfig-settings-manager
Head to your System menu, then Preferences->Advanced Desktop Effects Settings. Select the "General Options" button, and then the "Commands" tab. Enter "ks do" on an empty command line (probably 0) and then select the "Actions" tab at the top. Unfold the "Commands" list there, double-click the "Key" field next to the command you selected before, and enter the key you want to activate your text replacements. It's more straightforward in a KDE desktop. From the Settings menu, select Regional & Accessibility->Keyboard Shortcuts->Input Actions, then assign "ks do" to a key.
Basic triggers
Now comes the fun part. There's no GUI for creating Snippits' text triggers, but it's a simple text-based process. To make the simplest kind of trigger, create a new text file in your ".snippits" folder and name it what you want to type to activate it. For example, I'll create a file named webpic—no .txt or any other extension—and put the HTML code for placing an image in it:

Save and close that file. Now, whenever I type "webpic" into almost any application (any application that supports basic copy & paste, that is) and hit my shortcut key, it replaces that trigger word with the full HTML string. Nifty! I have to note, however, that Snippits eliminates any text you might have in your system clipboard when it does its thing, but tools like Klipper/Glipper, or GNOME's built-in clipboard-bypassing copy/paste can alleviate that problem.
Tricky triggers
But let's dig deeper. This time, I want my cursor to end up inside the string instead of having dummy text. So I open the trigger file and insert a "{cursor}" where I want it to end up, and that's that. Snippits has a slew of bracketed commands that you can use to basically perform anything your keyboard can do. Getting to the preferences menu in my Firefox window, for example, normally takes heading to a small "Menu" button in the top-right corner (because I'm addicted to the Tiny Menu extension), heading to "Edit" and then "Preferences." Or, I could create a file called "prefs" in my Snippits folder and put this in it:
{alt}m{alt}en
The "{alt}" code is a switch, like holding down the key, so I use it a second time to "release" it, then type the letters I'd normally hit (but can never remember) to get to what I need. Now I simply type "prefs" into the address bar (or anywhere) and hit my Pause shortcut, and there I am. A full list of read-able codes is available at Snippits' ReadMe page. Here are a few more tips on getting the most out of Snippits:
- Word spell-checking: If you hit your Snippits key after typing a word that's not one of your Snippits files, it runs the word through your built-in Aspell dictionary and corrects it if necessary.
- Useful examples: Snippits' author threw a few examples of useful Snippits in with the program, although you'll have to find and move them to get access. In Ubuntu 7.10, I found them in
/var/lib/gems/1.8/gems/snippits-0.5.1-/examples(whew!) and copied them to my .snippits directory. That gives you access to utilities like "time" and "date," which print out exactly what you'd think, as well as others that might inspire you to get crafty with your own Snippits - Key-specific triggers: If you find yourself accidentally firing your substitutions with certain words, you can set specific Snippits to activate with a different key. Simply head back to your system's shortcuts menu and add "ks [Snippits name]" to another key combo. The program's author, for instance, pastes his email address only when he presses a Win key + I, E combination.
Now you're all set up to make the most of your text entry in nearly any Linux application. I'd love to hear if any of our intrepid readers come up with some ultra-useful shortcuts, with code or without, that make life easier, so feel free to share your discoveries in the comments.
Kevin Purdy, associate editor at Lifehacker, can finally, finally say goodbye to manually typing out his Lifehacker post code on all his systems, thanks to Snippits. His weekly feature, Open Sourcery, appears every Saturday on Lifehacker AU.
Tags: clips | command line | feature | keyboard | keyboard shortcuts | linux | open sourcery | ruby | terminal | text | text substitution | time savers | top | ubuntu | video demonstration

Comments (AU Comments · US Comments)
There are currently no AU comments for this post.
Jim
Posted 5:38 AM 2/2/08
This looks AWESOME. Between this and Gnome Do I'll be very happy!
Jim
porter235
Posted 5:38 AM 2/2/08
Brilliant! Thank you, thank you!
porter235
robbhar
Posted 6:39 AM 2/2/08
And....the correct sequence for adding a hotkey in KDE is Settings/Regional & Accessibility/Input Actions then adding a new action
robbhar
robbhar
Posted 6:39 AM 2/2/08
Also the correct path for the snippits web site is:
[ben.kudria.net]
I tried sudo gem install snippits without raspell first and it worked fine and loaded raspell as a requirement.
robbhar
robbhar
Posted 6:39 AM 2/2/08
Keep getting:
robbhar@robbhar-laptop:/var/lib/gems/1.8$ sudo gem install raspell
Bulk updating Gem source index for: [gems.rubyforge.org]
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find raspell (> 0) in any repository
even after getting rid of the cache. Any suggestions?
robbhar
stranack
Posted 6:39 AM 2/2/08
Just what I've been looking for -- perfect. Note to newbies like me, you may need to logout and in again to get it working.
stranack
jonhutch24
Posted 6:39 AM 2/2/08
Ok so how do I get this on windows? Can Texter take parameters like this does? Because that would be extremely useful for me as a programmer.
jonhutch24
Kevin Purdy
Posted 11:38 AM 2/2/08
@robbhar: Thanks for the correction. I'll update soon.
Kevin Purdy
AbramCove
Posted 9:38 AM 4/2/08
> sudo mkdir ~/.snippits
Do we need "sudo" here? The luser should have permissions in his home directory.
AbramCove
ataub2qf
Posted 4:38 AM 5/2/08
Hmm :-( does not work with special chars like umlauts in the file for the text replacement: eg. äüö = umlauts.
btw nice article, thank you :-)
ataub2qf
Kevin Purdy
Posted 11:39 PM 8/2/08
@AbramCove: Good point. I tend to err on the side of sudo, as it were, but you can probably make due without it here.
Kevin Purdy
mlsamuelson
Posted 11:39 PM 8/2/08
I'm a Kubuntu Gutsy user and when running either "gem install" command was seeing
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find raspell (> 0) in any repository
I solved it by doing:
sudo apt-get install build-essentials
This command prompted me to insert my Kubuntu install disc, but I didn't have it handy. The solution here was to:
sudo vi /etc/apt/sources.list
and comment out the line mentioning the CDROM (in my case that was the first line). Then I could run
sudo apt-get install build essentials
sudo gem install snippits
(only the one gem install, as it takes care of the raspell dependency) and all went well from there on out.
mlsamuelson
peschelnet
Posted 11:39 PM 8/2/08
I haven't had the chance to install and test it out yet but, plan too...One question, does it work while using the shell? I presume it would because the author said that it works anywhere copy/paste works.
Just curious...
peschelnet
db0
Posted 10:39 AM 9/2/08
A small update. I can't seem to find the ks script. Where is it placed after installation?
db0
db0
Posted 10:39 AM 9/2/08
Unfortunately it doesn't seem to work for me :-/ I folloewed the steps and other than a small stop at installing raspell (which was installed on the second try) the installation/setup it went well.
However when I press my compiz command button, it doesn't do anything. I can see that the command is activated because the command button's character is not inputed on the text but it is not replaced by my snippits macro either.
Any ideas?
db0
db0
Posted 11:39 PM 11/2/08
Last update. I've managed to find the solution in the Ubuntu Fora. Apparently you may need to amend your path profile in order to make it see the ks scipt ;)
Either run: export PATH="/var/lib/gems/1.8/bin:$PATH" in your terminal or put PATH="/var/lib/gems/1.8/bin:$PATH" at the end of your ~/.bash_profile file
db0
peabodyman
Posted 3:25 AM 12/2/08
[hopefully this does turn out to be a double post, but comments are being weird].
We found a work around to the clipboard problem: [ubuntuforums.org]
it requires xclip
sudo apt-get xclip.
peabodyman
peabodyman
Posted 3:25 AM 12/2/08
We found a bit of a hack around the clipboard problems here: [ubuntuforums.org]
It requires the xclip program
sudo apt-get install xclip
peabodyman
ubuntugamer
Posted 4:39 AM 12/2/08
dunno how useful this will be for people, but 99.9% of people dont use the caps lock key, that would be a great binding to use for converting the text strings
ubuntugamer
RBSplat
Posted 4:39 AM 12/2/08
Everything seems to have worked... except.... when I try to use I get the following error....
error running "ks do"
Failed to execute child process "ks" (No Such File or Directory)
New Linux/Ubuntu user and it's got me stumped.
RBSplat
ubuntugamer
Posted 7:30 AM 12/2/08
i highly advise not to use sudo when doing mkdir ~/.snippets as it will remove your write permissions from that folder, therefore you have to use terminal for every script you want to add
ubuntugamer