gedit

gedit is the default text editor used in Ubuntu. If you're used to Notepad in Windows — or WordPad, if you need a bit more advanced capabilities, or even TextPad if you've downloaded that — then you'll be pleasantly surprised by the functionality offered in gedit.

Introduction

gedit.png

Whether you're a computer programmer, a business analyst, a home user, or any other type of user, from time to time you're going to need some type of simple text editor. There's a place for the word processor, like OpenOffice.org Writer, but there are also times when you don't need all of that functionality, and just need to do something simple. And, of course, there are times when you need to do something that a word processor is not well-suited for; maybe editing an XML document, or working with raw HTML. (Writer can spit out HTML, too, but sometimes you need to work with the HTML code, not just a word processor that can output HTML.)

In Windows, that role is usually played by Notepad. Because Notepad was purposely built with very little functionality, it's a light-weight, fast-loading application. However, people often need a bit more functionality, which is why TextPad is so popular — especially among programmers. It's still a fairly light-weight application, but it also features things like colour-coding, spell-checking, regular expression capabilities, etc.

For Ubuntu, the text editor installed by default is gedit. (I believe gedit is short for “GNOME edit.”) By default, gedit is a very simple text editor — although it has more features than Notepad — but it also has some plugins which can be enabled, to give it even more functionality. Users familiar with the extra capabilities provided in TextPad will be right at home in gedit.

Features

The following sections outline some of the features provided by gedit.

Tabs

If you're used to Notepad, then you'll know that it can only deal with one text file at a time. If you need to edit two files simultaneously, then you need to open two instances of Notepad. TextPad can edit more than one document, and uses a tabbed interface for the multiple documents, similar to the tabs in Firefox.

gedit uses the same tabbed interface that TextPad uses. This means that it's much easier to edit more than one document at the same time, within the same gedit window. (Of course, if you wish to open multiple instances of gedit, you can do that, too.)

tabs.png

To switch to any of the files/documents you're editing, simply click its tab. When you're done with a particular file/document, you can close it by clicking the X on its tab.

If you have two instances of gedit open, you can drag a tab from one instance to another, to move that document to the other instance of gedit. You can also drag a tab to the desktop, which will open a new instance of gedit, and move the file/document to that new instance.

Syntax Highlighting

highlighting.png

A feature many people find very useful in a text editor is syntax highlighting. This is a feature whereby certain text will be shown in different colours, or bolded or italicized, depending on what type of text it us. For example, if you were editing an HTML document, then the HTML elements and attributes might be shown in different colours, to distinguish the HTML syntax from the actual content of the document. (The screenshot to the right illustrates this.)

There are many different types of documents, and for each type of document, there might be different things you would want to highlight; tags/attributes in HTML or XML, keywords in certain programming languages, settings and values in ini files, etc. gedit supports the following modes of syntax highlighting:

  • Source Code (Programming Languages)
    • Ada
    • Boo
    • C
    • C#
    • C++
    • D
    • Fortran 95
    • Haskell
    • IDL
    • Java
    • Makefile
    • Nemerle
    • Objective Caml
    • Pascal
    • Scheme
    • SQL
    • VB.NET
    • Verilog
    • VHDL
  • Source Code (Scripting Languages)
    • JavaScript
    • Lua
    • Octave
    • Perl
    • PHP
    • Python
    • R
    • Ruby
    • sh
    • TCL
  • Markup Languages
    • HTML
    • LaTeX
    • Texinfo
    • XML
  • Others
    • ChangeLog
    • CSS
    • .desktop
    • Diff
    • gettext translation
    • GtRC
    • .ini
    • MSIL

You can switch the syntax highlighting to the mode you want using the View->Highlight Mode->Category->Sub-Category menu. However, you don't normally have to! gedit is very good at figuring out what type of file you're editing, and choosing the appropriate form of syntax highlighting for you.

You'll also notice that you can turn highlighting off, if this is a feature that doesn't appeal to you.

Line Numbers

Many times, when working with text files, you need to know what line you're working with. A common example is if you are reading a log file of some sort, and have been told that there is an error on line X; you need to somehow find line X in that document.

If you wish, you can have gedit show the line numbers for a document.

linenumbers.png

This feature is turned off, by default, but you can start showing line numbers by going to Edit->Preferences, and checking the Display line numbers checkbox. And, of course, clearing this checkbox will turn the feature back off again.

Word Wrap

Another extremely common feature — one so common that even Notepad supports it — is the ability to “wrap” long lines of text, so that they don't scroll off to the right of the window. Word wrapping can make a document more readable — or it can make it less readable, depending on what type of text you're working with. (e.g. a pure text document might be more readable with word wrapping turned on, whereas a file containing programming source code would usually be more readable with the word wrap turned off, because for many programming languages it's important to see exactly where the newlines are.)

Unfortunately, turning word wrapping on or off is not as easy to do in gedit as it is in any other text editor I've worked with; you have to go to Edit->Preferences, and check or uncheck the Enable text wrapping checkbox. (It would be preferable to have a button in the button bar, to turn this feature on or off. Even Notepad makes it easier than gedit, with the Format->Word Wrap menu item.)

There is another option, in gedit's preferences, where you can prevent gedit from breaking up a word when text wrapping. For example, with this feature turned off, a long sentence might end up looking like this:

this is a very lo
ng piece of text

whereas with the feature on, gedit would intelligently bring the entire word “long” to the next line, like this:

this is a very
long piece of 
text

Search, and Interactive Search

As with most other text editors, you can search for a piece of text within your file/document by pressing Ctrl+f or using the Search->Find menu. This will bring up a Search dialog, where you enter in the text you're looking for. There are some extra options you can use to refine your search:

  • you can do a case-sensitive search, meaning that if you search for “the” gedit would not return “The” as a search result, because the first letter is capitalized
  • you can tell gedit to only match on the entire word; for example, if you search for “the” gedit would not return “thesaurus” as a search result

There is also a Replace function, which works very similar to the Find function, by pressing Ctrl+h or using the Search->Replace menu. This is used to find a certain piece of text, and replace it with a different piece of text.

There is also an “interactive search,” which Notepad users might not be used to. (However, Firefox users would be used to this functionality.) If you press Ctrl+k or use the Search->Interactive Search menu, gedit will bring up a textbox, where you can start entering the text you're searching for. As you enter each character, gedit will refine its search of the document, highlighting every match. For example, if you search for “the” you would first type ‘t’, and gedit would highlight every ‘t’ in the document. You would then type the ‘h’, and gedit would highlight every occurrence of “th” in the document. Finally, when you type the ‘e’ gedit would highlight every occurrence of “the” in the document.

Plugins

In addition to the “core” features mentioned above, gedit comes with some additional plugins, that can be enabled, for additional functionality. Some of these plugins are enabled by default, but some of the others might be just as useful to you.

To turn a plugin on or off, to go Edit->Preferences, and go to the Plugins tab. There will be a list containing each available plugin, and a checkbox which can enable/disable it.

I won't discuss all of the plugins shipped with gedit. Just the ones I can speak intelligently about.

Change Case

This one is disabled, by default.

With the Change Case plugin enabled, gedit will add a new item to the Edit menu, which you can use to change the case (or capitalization) of a piece of text. Simply select a piece of text, and then go to Edit->Change Case->Option, to change how the text is capitalized. You have the following options:

  • ALL UPPER CASE — in other words, every character will be capitalized
  • all lower case — in other words, no character will be capitalized
  • Invert Case — any character which was capitalized will be made lowercase, and any character which is lowercase will be capitalized. For example, if you select the text “this is LaTeX” and choose this option, gedit will turn it into “THIS IS lAtEx”.
  • Title Case — will make sure that the first character of each word is capitalized, and the rest are not. For example, if you select the text “this is LaTeX” and choose this option, gedit will turn it into “This Is Latex”.

Document Statistics

This one is enabled by default.

The Document Statistics plugin is used to give a dialog box with some information — some “statistics,” if you will — about the file/document you are currently editing. To use it, simply use the Tools->Document Statistics menu. It looks like the picture below.

statistics.png

As shown, if there is any text selected, a second set of statistics will also be shown, just for the selected text. In this case, I had not selected any text, so this information was greyed out.

The Side Pane

There are a few plugins which will show up in a “Side Pane” — that is, a special area to the left of the gedit window. If any of these features are enabled, you'll need to show this side pane by using the View->Side Pane menu, or by pressing F9.

Documents

The Documents feature1 simply shows all of the documents you're currently editing. Clicking any document will select it in the editor window, the same as clicking the document's tab would have done.

This plugin is probably not that useful, under normal circumstances, but would become more useful if you are editing many files/documents at once, meaning that not all of the tabs are easily readable on the screen.

File Browser

This one is disabled by default.

The File Browser plugin can be used to navigate around the file system, to find a file you want to open. This can be easier than using the Open dialog.

Tag List

This one is disabled by default.

The Tag List plugin will be familiar to TextPad users. It provides a list of common tags (and other pieces of text) which are used in HTML, XSLT, and LaTeX.

tags.png

Simply double-click any item from the side pane, and it will be inserted into the document, at the current cursor location. In some cases, it might make sense to select a piece of text, and then double-click the item. For example, if I have the text

Click this link to see my homepage

I might want to make the text “this link” into a link. I could select it, and click the Anchor URI option, which would change the text to this:

Click <A HREF=> this link </A> to see my homepage

Although this plugin can be useful, I find the Snippets plugin (described below) to be a bit more useful, and more customizable.

Insert Date/Time

This one is enabled by default.

The Insert Date/Time plugin, as its name suggests, is used to insert the current date and/or time into the document, at the current cursor location. Simply use the Edit->Insert Date and Time menu, will will bring up a dialog, which will allow you to choose the format in which you want the date inserted. (e.g. “Wed 03 Jan 2007 01:57:27 PM EST” vs. “03/01/07” vs. “2007-01-03 13:57:27” There are many, many options.)

Spell Checker

This one is enabled by default.

The Spell Checker plugin will add spell-checking functionality to gedit. With this option on, you can easily find typos or other spelling mistakes you make while editing your documents. The functionality works very much like you're probably used to from word processors like Word or OpenOffice.org Writer, or numerous other applications which involve entering a lot of text.

Unfortunately, gedit's spell checking feature is not HTML-aware — or any other language-aware — so it will often mistake proper markup or keywords for spelling mistakes.

There are two ways you can have gedit check your spelling for you: automatically, or on demand.

You turn on automatic spell checking with the Tools->Autocheck Spelling menu item. When this option is on, any words in your file/document which gedit's dictionary doesn't recognize will be underlined in red. If you right-click any word which is underlined in red, there will be an option in the context menu called Spelling Suggestions; here, gedit will present a list of suggested words which you might have meant to type. Choose any of the selections, to have gedit replace the underlined word with the correctly spelled one. Of course, the word might actually be correct, and just not be in gedit's dictionary. In that case, you can choose the Ignore All option, which will tell gedit to stop underlining occurrences of that word. Or, if the word is one you use often, you can choose Add, to add the word to gedit's dictionary.

You can perform an on-demand spell check by using the Tools->Check Spelling menu, or by pressing F7. (You can do this even if you have the automatic spell checking turned on.) When you do so, gedit will search through the document, looking for any misspelled words. If it finds any, it will pop up a dialog box, with the first misspelled word, and a list of suggested words it can be changed to. Similar to the menu you get when right-clicking a misspelled word for automatic checking, you can change the word, tell gedit to ignore all occurrences of the word, or add the word to gedit's dictionary. There is also the option to replace all occurrences of the word with the new word; for example, if you have been consistently misspelling the word “this” as “thsi” you can hit F7, and gedit will highlight the first occurrence of the misspelled word. If you choose the proper spelling of “this” and then use the Change All button, gedit will replace every occurrence of “thsi” with “this”.

Of course, different words are spelled differently, depending on where you live. (e.g. “humour” in Canada, vs. “humor” in America.) Since I installed the English version of Ubuntu, gedit gives me the choice of a number of English dictionaries. You can choose which dictionary you want to use by going to Tools->Set Language, and choosing the correct one for your locale.

setlanguage.png

I have not found out how to install additional dictionaries, although I assume that it's possible.

Snippets

This one is disabled by default.

The Snippets plugin is similar to the Tag List plugin, in that it is used to insert common “snippets” of text into a document. However, it works a bit differently, and is much more customizable, meaning that you can create your own snippets, or even change the way that gedit's default snippets work. It is probably one of the more powerful plugins available for gedit.

There are three ways you can insert a snippet into a document: enter its tab trigger and hit the Tab key; press its Accelerator key combination; or press Ctrl+Space, to get a list of potential snippets.

For example, suppose you are beginning to edit an HTML document, and want to insert the <body> tag into your document. There is a “body snippet,” which has a “tab trigger” of “body.” This means that you can enter the following:

body

Then, with the cursor right after the ‘y’ in “body,” press Tab. gedit will then replace the word “body” with this:

<body ID="ID " onload=""}>

</body>

Furthermore, the Snippets plugin is aware of the different pieces of information you might need to enter within that <body> tag; the first attribute will be highlighted, so that you can type the ID. If you press the Tab key again, the cursor will be moved into the onload attribute's value, so that you can enter that. Pressing Tab once more will move the cursor within the <body> tag, for you to continue editing the document. (Pressing Tab after that will work like normal, and insert a tab character into the document.)

Alternatively, if you can't remember the “tab trigger” for body, you can simply press Ctrl+Space, which will present a list of all of the available snippets.

Another example is the <br> tag. Instead of using a “tab trigger” for this one, there is a keyboard accelerator — in this case, Shift+Ctrl+Space. Anywhere within an HTML document, if you press Shift+Ctrl+Space, gedit will insert the text <br>, and then move your cursor to the line below.

Editing Snippets

As mentioned, if you're not satisfied with the default snippets provided with this plugin, you can add your own, or change the default ones. Simply go to Tools->Manage Snippets, and you will get a window with all of the available snippets.

snippetsmanager.png

You'll notice that there are different snippets available, depending on what type of document you're editing. That is, if you choose the HTML syntax highlighting, there will be HTML snippets, whereas if you choose the C++ syntax highlighting, there will be different snippets available. Within any category, you can select an existing snippet, to edit it (or delete it), or use the New button to create a new one. You can change the trigger or accelorator for any snippet — although you should be aware that certain accelerator key combinations will already be in use! For example, you couldn't create a snippet with an accelerator of Ctrl+c, because that's already used by the Copy command.

In the text box on the right-hand side of the snippet editor, you enter all of the text that you want inserted for this snippet. For example, for the “body snippet,” this text box contains this:

<body id="${1:ID} " onload="$2"}>
     $0
</body>

(Note that there is a bug in this particular snippet; an extra } character.)

There are some strange-looking things in that snippet's text; these are placeholders, which are special pieces of text within a snippet that affect how the snippet works. The following sections list some of the different placeholders you can use within a snippet.

Tab Placeholders

A Tab Placeholder configures how the Tab key works, within your snippet. There are 9 available tab place holders, which are designated by using $1 through $9. $1 would be the first place you want the cursor to appear, $2 the second, etc. For example, suppose you create a snippet like this:

<sometag attr1="$1" attr2="$2">

</sometag>

When you insert this snippet, gedit will insert the following text into your document:

<sometag attr1="|" attr2="">

</sometag>

(except without the | character) The cursor will be placed where I have put the | symbol. You can then enter whatever value you want for that attribute. If you press the Tab key, the cursor will be moved to the next tab cursor; in this case, where I originally put the $2 in my snippet. Pressing Tab additional times will keep moving the cursor to additional tab cursors, until there are no more, and it will then move the cursor to the end of the snippet's text. From that point on, pressing Tab will have no effect on the snippet; it will simply insert a tab character, as normal.

There is an additional trick you can use for tab placeholders; you can default the value of a tab placeholder. In this case, you would use the ${n:default} syntax, where n would be the tab number, and default would be the text you want inserted by default.

For example, suppose we changed our earlier snippet to this:

<sometag attr1="${1:value 1}" attr2="${2: value 2}">

</sometag>

Inserting the snippet would produce the following:

<sometag attr1="value 1" attr2="value 2">

</sometag>

However, because the Snippet plugin is still aware of the tab placeholders, the text “value 1” will be selected; if you start typing, the text will be replaced with whatever you type. If you then press the Tab key, gedit will next highlight the text “value 2” so that you could type a new value for that.

There is also a special tab place holder, which is $0. This is the placeholder where the cursor should end, when the Tab key has been pressed for the last time. For example, in our example above, the last time the user presses the Tab key, the cursor will end up after the </sometag> closing tag; if we change our snippet to this:

<sometag attr1="${1:value 1}" attr2="${2: value 2}">
    $0
</sometag>

then the last place the cursor is placed will be within the tag, which makes more sense, in this case.

Selected Text

In many cases, you don't just want to insert a bunch of text; you might want to surround the selected text. For example, you could create a snippet for creating an anchor, like the following:

<a href="${1:URL}"></a>

However, to be really useful, it would be great if you could select a piece of text, and wrap the <a> and </a> tags around that text. You can do that with the $GEDIT_SELECTED_TEXT placeholder. We could change our snippet to this:

<a href="${1:URL}">$GEDIT_SELECTED_TEXT</a>

It would work like this:

Suppose I have the following text, in the editor:

Click here to see my homepage.

I then highlight the text “Click here” and insert the snippet. My document would then look like this:

<a href="URL">Click here</a> to see my homepage.

and the text “URL” would be highlighted, so that I could type in the address.

If there is no text selected in the document, then nothing would appear where the $GEDIT_SELECTED_TEXT placeholder is in the snippet. In other words, if we were to insert the snippet above, with no text selected, it would simply insert

<a href="URL"></a>

File Name

Similar to the $GEDIT_SELECTED_TEXT placeholder, there are two placeholders for inserting the filename into a snippet: $GEDIT_FILENAME and $GEDIT_BASENAME. $GEDIT_FILENAME will insert the name of the file, including its full path on the filesystem; for example, a file named blah.html sitting on my desktop might produce /home/serna/Desktop/blah.html.

$GEDIT_BASENAME will insert just the name of the file, without the path; so for this example, it would just insert blah.html.

Backing Up Changes

If you have created your own snippets, or modified existing snippets, you'll probably want to be able to make a backup of those changes. This way, you can copy your changes to another computer, where you also have Ubuntu installed, or have your changes ready, if you ever reinstall Ubuntu on your current computer.

Backing up your changes is easy. Simply go to the ~/.gnome2/gedit/snippets directory2, where there will be some XML files. (Notice the period at the beginning of the .gnome2 directory, which means that it's hidden.) Copy these files to your backup location. On any new computer, where you want to keep your changes, simply put the files back into this location.

page_revision: 33, last_edited: 1211661508|%e %b %Y, %H:%M %Z (%O ago)
Unless stated otherwise Content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.