Save time with Gedit snippets

Apps

Some people think that Gedit is a toy text editor not suitable for more experienced users. And while it might lack Emacs's psychiatrist or Vim's undo branching, it turns out that Gedit has a lot of power under its hood - if you know where to look.

One feature that is guaranteed to save you time is Snippets, a plugin that enables quick insertion of commonly used text, and something we use frequently here on TuxRadar. We're going to show you how to get started using the built-in snippets, and how you can use shell commands and even Python code to make your own...

You see, a lot of our content is drawn from the archives of Linux Format magazine, a magazine that is produced using Mac OS X, Adobe InDesign and a dozen other proprietary tools. No, it's not ideal, but sadly we have little say in the matter. Besides, you don't think the folks behind Cross Stitcher magazine stitch their magazine together, do you?

Anyway, the problem with converting articles from the magazine world to the internet world is the need to replace special symbols with more simple equivalents. Magazines, for example, make extensive use of curly quotes, em- and en-dashes, ellipses (...), non-breaking spaces and more. And while these do have HTML equivalents, they are rarely used, so we strip all these out and replace them with simple ASCII characters.

Once that's done, we need to convert articles to HTML, and that's what takes the real time. Or at least it did - until we started using Snippets. You see, Snippets allows you to insert pre-defined blocks of text either by using a keystroke or tab completion. That's not an uncommon feature in a text editor, admittedly, but Snippets makes it particularly easy to create more advanced placeholders for text replacement, even allowing output from the shell and Python scripts.

Before we get started with some example snippets, you need to make sure you have the Snippets plugin enabled. Fire up Gedit and go to Edit > Preferences. From the Plugins tab, scroll down and make sure Snippets is checked. Once that's done you should see the Configure Plugin button become active - that's where we'll be doing most of our work.

Before you can start using Snippets you need to make sure it's enabled.

Before you can start using Snippets you need to make sure it's enabled.

A basic snippet

Copy and paste this into your Gedit window:

He thrusts his fists
Against the posts
And still insists
He sees the ghosts.

That gives you four simple lines of text. Save that file as test.html - it doesn't really matter where. But what does matter is that Gedit now knows your file is HTML, which means Snippets knows too and will activate its list of HTML snippets. For example, if you select the whole first line and press Shift+Alt+W, it will automatically be wrapped in a <p> tag, like this:

<p>He thrusts his fists</p>

What's more, Gedit will automatically leave the first "p" highlighted so that you can replace it just by typing, and any changes you make will be copied across to the </p> tag so that it matches. To see how this works, go back to the Configure Plugin window for Snippets (you'll also find it under Tools > Manage Snippets) then look inside the HTML category for the snippet called "Wrap Selection in Open/Close Tag".

When you select that snippet, the code behind it will appear in the top-right of the window, and should look like this:

<${1:p}>$GEDIT_SELECTED_TEXT</${1}>

$1 is a placeholder, which means it's something you want to type into after the snippet has been inserted. In this case, there's only one placeholder, $1, but it appears twice. Because the same placeholder number has been used twice - known as a "mirror placeholder" - Gedit will ensure that whatever you type in the first $1 will be synchronised with the second $1, which is how you can change <p> to <h1> and it will be changed in the closing tag too.

The ":p" part of the snippet sets a default value, in this case the HTML paragraph tag, "p". If you leave off the default value, Gedit will just leave a blank spot for you to type.

Finally, the most important part: $GEDIT_SELECTED_TEXT. This is a special value inside snippets that will be automatically replaced with the contents of whatever text you had selected before the snippet was used. You'll be using this a lot!

Working with multiple placeholders

That basic snippet had just one placeholder, which meant that it prompted you to edit only one piece of text. A slightly more complicated snippet is called "Wrap Selection as Link", and has two placeholders. To test it out, select the second of your text lines and press Shift+Alt+L. You should see this:

<a href="http://somesite.com/">Against the posts</a>

After the snippet has run, Gedit will leave the insertion cursor over "http://somesite.com/" with that text highlighted so you can replace it by typing. Type http://www.tuxradar.com in there and then - this is the important bit - press Tab. When you do that, Gedit will move the caret to "Against the posts" and select it just as it had selected "http://somesite.com/". That's because this snippet has two placeholders defined: "$1:http://www.somesite.com/" and "$2:GEDIT_SELECTED_TEXT".

You can have as many of these placeholders as you need, and any number of them can have default values specified - just press Tab to move between them.

Bringing in output from the shell

We're going to create a new snippet from scratch to show off some of the more advanced features of Snippets. Go to Tools > Manage Snippets to bring up the Snippets Manager, then scroll up to the top of the category list to where it says "Global".

The Snippets Manager lets you create and edit your own snippets across a number of different languages.

The Snippets Manager lets you create and edit your own snippets across a number of different languages.

Select that, then click the Create New Snippet button (it's just above the Help button at the bottom of the window).

Call your new snippet "Insert file" and put the following inside it:

$(1:cat $GEDIT_SELECTED_TEXT)

Notice how that uses standard parentheses (ie "(" and ")") rather than braces ("{" and "}") - that's because the parts inside the parentheses will be sent to a shell, executed, then be replaced with whatever the shell command prints out.

What that code example does is run "cat" on a filename, with the filename being specified as $GEDIT_SELECTED_TEXT so all you have to do is type a filename into Gedit, select it, then run the snippet. Underneath the text area for entering snippets are some text boxes marked "Activation". Click inside the Shortcut Key box then press any shortcut you want to assign to this snippet and it will become active immediately.

You can run any shell commands you like, as long as they return their output back using stdout. For example, the "bc" mixed with Gedit Snippets means we can create a simple calculator. In the Snippets Manager, create a new snippet under the Global category called Calculator. Give it this text:

$(1:echo $GEDIT_SELECTED_TEXT | bc)

Give it the shortcut Ctrl+Shift+C, then click Close and type this into your Gedit file:

5 + 10 / 2 * 9

Select it all, then press Ctrl+Shift+C to see your text replaced with the answer to the calculation: 50.

Gedit detects the end of the shell command by looking for a closing parenthesis (")"), so if your command includes one you need to put a backslash before it, like this:

$(1:echo "This is a (test\)" > file.txt)

That will save the text "This is a test" to file.txt. As you can see, there is no need to escape the opening parenthesis ("(").

Mixing in some Python

Here's where Snippets really starts to prove its worth: you can run Python code wherever and whenever you want to. Let's start with a simplified example of a snippet we use here on TuxRadar to kill off curly quotes:

$< return $GEDIT_SELECTED_TEXT.replace('“', '"') >

That is designed to convert this:

“Hello, world!”

...into this:

"Hello, world!"

The "$<" part signals the beginning of your Python code, and it ends with the corresponding ">" at the end of the snippet. In between is a call to the replace() method of a Python string - in this case, $GEDIT_SELECTED_TEXT - which replaces a curly quote with a simple ASCII one.

When you've finished making all your changes to the input, you hand any output back to Gedit using Python's "return" statement.

That initial snippet fixes the opening curly quote, but not the closing curly quote, which is easily fixed simply by making the snippet span more than one line, like this:

$<
output = $GEDIT_SELECTED_TEXT.replace('“', '"')
output = output.replace('”', '"')
return output
>

If you're obsessed with brevity, you could write that as a one-liner using something like this:

$<return $GEDIT_SELECTED_TEXT.replace('“', '"').replace('”', '"') >

As with shell placeholders, you need to be sure to escape the end symbol if needed, which in the case of Python placeholders is >. To give you a working example of this, consider our original text:

He thrusts his fists
Against the posts
And still insists
He sees the ghosts.
If you wanted to put <p> tags around each of those lines, you could select each line individually and use Shift+Alt+W. But with Python we can select the whole paragraph, split it by new lines, then add HTML tags for each line, like this:
$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
	output += "<p\>" + line + "</p\>\n";

return output
>

What that does is split up the selected text by line breaks, then loop over each line and add it along with the <p> start and end tags to the output. However, because <p> and </p> both contain the > symbol, Gedit will think that means the end of your Python snippet. So, the solution is to put a \ before the > so that Gedit ignores it.

Note that we've used "\n" to add line breaks of our own into the output to keep things neat.

Converting tabs to tables

Copy and paste this into your Gedit document:

a	apples
b	bananas
c	cherries
d	dates

That's a list of items, one per line, where the item on the left is separated from the item on the right by a tab character. This is a fairly common way to work with tables in plain-text, but with a bit of Gedit + Snippets + Python magic we can whip that up into a real HTML table.

What we need to do is simple:

  1. Split the selected text by line break
  2. Loop over each line
  3. Split each line by tab
  4. Add HTML table tags

...while remembering that any time we want to print > we need to put a \ before it. Here's how to do that in Python:

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = '<table\>\n';

for line in lines:
	output += '<tr\>';
	
	columns = line.split("\t");
	for item in columns:
		output += '<td\>' + item + '</td\> '
	
	output += '</tr\>\n';

output += '</table\>';
return output
>

If you assign that to a shortcut, then select the four lines from before and run the snippet, you'll get this:

<table>
<tr><td>a</td> <td>apples</td> </tr>
<tr><td>b</td> <td>bananas</td> </tr>
<tr><td>c</td> <td>cherries</td> </tr>
<tr><td>d</td> <td>dates</td> </tr>
</table>

If you wanted to be a bit more fancy and include some zebra stripes for your table rows, just drop in a counter variable and use modulo (%) to check whether it's odd or even, like this:

$<
lines = $GEDIT_SELECTED_TEXT.split("\n")
output = '<table\>\n'
counter = 0

for line in lines:
	if counter % 2 == 0:
		output += '<tr class="even"\>'
	else:
		output += '<tr class="odd"\>'
			
	columns = line.split("\t")
	for item in columns:
		output += '<td\>' + item + '</td\> '
	
	output += '</tr\>\n'
	counter += 1

output += '</table\>'
return output
>

Some last tips

There's a huge amount you can do with Snippets, particularly when you start taking advantage of shell and Python placeholders. But before we let you run free, here are three last pointers:

  1. You can use the $0 placeholder to specify the last position that should be jumped to. By default this is the end of your snippet, but if you use $0 you can choose somewhere else.
  2. Along with $GEDIT_SELECTED_TEXT, you can also use $GEDIT_FILENAME and $GEDIT_BASENAME to use the full filename (eg /home/tuxradar/foo.bar) and the base filename (eg foo.bar) in your snippets.
  3. When you've arranged all your snippets just as you want them, go into the Snippets Manager and click the save icon to export selected snippets as XML so you can copy them other machines.

Have fun - and make sure you take a look through some of the awesome snippets that are included by default!

You should follow us on Identi.ca or Twitter


Your comments

Thank you

Thanks so much for this post, I was wondering how to create a simple feature for converting HTML entities, thought I was going to have to write a plugin, this is much much easier!

One question though: My snippets will not work when placed in the "Global" section. They only work when I place them in the category appropriate to the document type. Any idea why?

Jason

Thanks a lot

That's a very usefull article for me.

Great article.

I find this so useful. Sometimes the smallest things save so much time and effort.

I love gedit!

Initially when I got on ubuntu I thought; where is a good editor and how slow was I overlooking gedit in favor of all the recommended ones from others.

Those people do not understand how great gedit is!

Thank you, cheers.. I owe you a beer

Jeff in Calgary

Thanx, I used this to make a

Thanx, I used this to make a comment snippet, // comment before each selected line. Now figuring out if I can make one to uncommnent also.

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += "//" + line + "\n";

return output
>

adapted your paragraph python snippet

thanks for posting the great python paragraph snippet

I was getting annoyed with all the empty paragraph tags it was creating for blank lines though so I adapted it to remove those by adding this line:
new_output = output.replace("<p\></p\>", "");

Not the most elegant but then again I don't claim to know python! Full code below:

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += "<p\>" + line + "</p\>\n";
new_output = output.replace("<p\></p\>", "");
return new_output
>

snippet for removing in-paragraph line breaks

$<
# Reverse word wrap: Replace single newline characters between lines by spaces.

import re

r = re.compile('(?<!\n)\n(?=.)')
return r.sub(' ',$GEDIT_SELECTED_TEXT)
>

thank

hi thanks for the information, was very useful

Thanks

Thanks.

Thank You!

Thank you very much indeed! This was very helpful. Very detailed and very clear.

Thanks! I love gedit

Thanks for the quick info. Gedit is great for coding!

I think other site

I think other site proprietors should take tuxradar.com as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic! Great!

Thanks for your thoughts.

Thanks for your thoughts. One thing I have noticed is that banks plus financial institutions know the dimensions and spending practices of consumers and also understand that many people max outside their real credit cards around the getaways. They correctly take advantage of this specific fact and then start flooding ones inbox and snail-mail box along with hundreds of 0 APR credit cards offers shortly after the holiday season closes. Knowing that if you're like 98% of all American general public, you'll leap at the possible opportunity to consolidate credit debt and switch balances for 0 annual percentage rates credit cards. Wonderful!

Currently it appears like

Currently it appears like Expression Engine is the preferred phorumging platform available right now. (from what I've read) Is that what you're using on your phorum? Great!

What do you wish to tell it?

Simply wish to say your article is as surprising. The clearness in your post is simply excellent and i could assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the enjoyable work.

Extremely useful

Brilliant, many thanks indeed for this!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

CAPTCHA
We can't accept links (unless you obfuscate them). You also need to negotiate the following CAPTCHA...

Username:   Password:
Create Account | About TuxRadar