PHP, Zend Framework and Other Crazy Stuff
Archive for February 3, 2009
Gnome’s Text Editor, Gedit: A TextMate-like Editor for Linux
Feb 3rd
Over the weekend I was helping a friend setup a small Ubuntu server but since he is dead set on remaining on Windows for his desktop needs, I setup a Ubuntu Desktop guest instance in Sun’s excellent VirtualBox to work from for the afternoon. I was using a disk clone from my own PC (where I keep Vista on hold for those days I want to blow up chains of cars stuck on a bridge in Liberty City) and he seemed sort of shocked to see Ubuntu working perfectly well inside Vista at near full resolution. Another Ubuntu convert in the making
.
Being one of those Editplus/Notepad++ types, he was also impressed by Ubuntu’s default editor, the infamous “Text Editor” aka gedit. His surprise at gedit reminded me that to so many people, the humble Gnome editor is often compared to Windows notepad app. A simple editor with minimal features that is pathetic for anything else.
Not true!
The truth is that I use gedit for programming in Ubuntu at least as often as I boot up Eclipse. So there! There are times you want a heavy IDE, and times you need a lightweight editor that starts in a second and takes less than five minutes to open a file. I’ve always liked to avoid IDEs when I can because I find them a chore to use for anything but a few bigger applications where their awesomeness is actually awesome and not just tedious. Mostly they tend to be slow and cumbersome, and this is made worse when you’re running inside a small virtualised OS. Gedit on the other hand is about as unassuming as you could wish for – small, fast, stable and customisable with plugins if you know Python or C (or can download third party plugins
).
Gedit can, with a little work, become an excellent source code editor. It’s main advantage is its plugin system which allows for some really great customisations that allow gedit to appear and act more like Mac OS’s TextMate (which presumably is a good thing). Let there be no mistake, I love gedit. I’ve been tempted into trying alternatives, but gedit seems to suit a need for simplicity in my programming life where I spend all my time using the Gnome desktop.
To get us started here’s a quick video showing a plugged and styled gedit in action.
Basic Functionality
By default, gedit comes with few bells and whistles enabled and its initial state really does make it just another notepad. It will immediately offer source code highlighting however which is a step up at least. Once you have your barebones setup it’s time to look under the hood and get it pimped out.
Your first stop should be the User Preferences (Edit > Preferences). From here you can enable tabs or space indents depending on your likes, enable highlighting of the current line, enable matching bracket highlighting, etc. Basic editing preferences you can’t live without
.
Next, locate the plugins pane (Edit > Preferences > Plugins) which provides a list of installed plugins. The following should be enabled:
File Browser: A file browser plugin allowing to easily access your filesystem (includes remote mounts, creating new files/dirs, monitor dirs for changes, etc)
Indent: Indents or un-indents selected lines
Snippets: Insert often used pieces of text in a fast way.
External tools: Execute external commands and shell scripts
The two ones to watch are File Browser and Snippets. The File Browser is one of those features you’ll take for granted, a bare essential. Snippets is one alternative approach to the lack of full IDE autocompletion allowing you to create countless snippets of reusable code you can insert into files by typing the relevant tag and pressing the tab key. The Snippet format is similar to TextMate’s and supports some cool scripting with Python or bash to tailor the snippet based on other Snippet fields or shell accessible values.
More Official Plugins
Why stop here? Move right along, and install the official gedit-plugins package using (for Ubuntu):
sudo aptitude install gedit-plugins
Now return to your user preferences (you may need to restart gedit so it can pick up newly added/changed files) and enable some additional plugins from this package:
Bracket Completion: Add automaically a closing bracket when you insert one
Code Comment: Comment or uncomment blocks of code
Session Saver: Allow to bookmark working sessions in order to get them back for further use
Terminal: A simple terminal widget accessible from the bottom panel
Third Party Plugins
We’re really getting somewhere now! To add even more functionality you cannot live without, we’ll need a few more unofficial plugins. Well, more than a few… To make things a lot easier, it’s worth grabbing a precompiled collection of the most useful third party plugins, themes, and other assorted updates. The collection we’re after is called “gedit-mate” which is based on a previous collection called “GMate”.
I’ve repacked the current version of this collection with some tweaks and added some PHP specific enhancements. It’s available from http://github.com/padraic/gedit-mate (this is forked from the original Rails version at http://github.com/ivyl/gedit-mate). You can git clone from there, or simply use the Download button on the page to grab a zip or tar archive to work with.
I replaced the original Ruby Rake installer script with something more appropriate, so you just need to navigate to the gedit-mate directory and run:
sudo sh install.sh
Now back to our plugin preferences, enable the following plugins:
Advanced Bookmarks: Advanced bookmarking tool for Gedit
Class Browser: A class browser located in the side pane. Based on ctags, it supports a wide range of languages allowing to quickly navigate files by their classes and methods.
Find In Files: Searches all docs under the root of your File Browser sidepane
Word Completion: Display a completion popup, updating as you type, with suggestions from all words in all documents in all windows
Snap Open: A regex open file dialog that resembles that of Textmate
Open Files: Opens files based on a name query
You can assess a few other plugins to be enabled if you wish, but DO NOT enable the Gemini plugin which conflicts with Bracket Completion and creates some undesireable effects. I found that Gemini had problems with PHP bracket closing.
You now have an editor with some new features and pretty toys to play with. Have a look at the remaining plugins left disabled to see what additional features you might want to enable to
Adding A Splash Of Style
There are any number of personal preferences when it comes to editor styles. My personal choice is a dark background with fairly well bolded text in a lighter (but not overly white) colour which I find is a lot easier on my eyes. At the moment I use the Darkmate theme which matches my needs admirably and looks pretty cool to boot. Our third party plugin pack will have installed a lot more themes to experiment with.
The plugin pack will also have installed the Monaco font that everyone seems to love in TextMate into your user’s .font directory. You can adjust your font style for gedit from the default monospace option to monaco and pick your preferred font size, or pick any other suitable monospace font you prefer.
Zend Framework Snippets
With the Snippets plugin, we have the ability to create strings of code to reuse at will. Gedit cannot support IDE style autocompletion (if you need that, use an IDE!) but Snippets remove quite a bit of that pain when autocompletion is not an absolutely essential requirement (personally I never took to autocompletion much except as a nice aide mémoire – I have a habit of writing over all the suggestions without stopping
).
The plugin pack from above will have installed a php.xml snippets file I wrote which will add updated snippets for common PHP structures (the defaults are not the usual conventions acceptable in PEAR CS) and includes a collection of Zend Framework snippets (the Snippet list prefixes them all with “ZF” so they are at the bottom of the PHP list) to make use of. Feel free to suggest changes/additions to those ZF snippets – maybe one day someone will build and release a TextMate bundle for Zend Framework which I can port more completely into gedit (or vice versa). The Textmate and gedit snippet syntaxes are not completely dissimilar.
Conclusion
Gnome’s default editor is easy to dismiss as a serious programming tool, yet its simplicity and plugin architecture both enable a simple stable lightweight code editor that is capable of holding its own against a range of alternatives and which bears a few resemblances to Mac OS’s much celebrated Textmate.
No, it’s not an IDE, but it’s a very good editor for many purposes when a full blown IDE is overkill. It’s also a viable alternative for those who, like myself, avoid IDEs unless they are obviously needed. Since I use gedit a lot (as in everyday!) for my code editing, the approach above simply makes a good thing even better. Maybe one day they’ll even get around to adding code folding and I can call it a day
.
Still, if gedit does not strike your fancy there are lots of other editors to try out for Linux with varying option bundles. Emacs and vim remain ever popular (after you survive their attrocious interface and realise what gems they are), and in the lightweight category we have options like jedit, scribes or geany. Hopefully this paragraph cuts off the inevitable suggestions about choosing a “real” editor (like any IDE-Bloat-Of-Doom).
