info | twitter | github
J-_-L | Writings on Ruby Programming
07.12.11
7 comments

RubyBuntu -6- gedit 3! wtf? set it up for ruby/web development :)

So you’ve installed (or upgraded to) ubuntu 11.10 and everything looks great… Except – uh!, lots of gedit plugins are only compatible with gedit 2! But don’t be sad.. or angry.. This guide points out, how to, nevertheless, create a solid foundation that allows you getting prodcutive with gedit!

Setting up gedit, the slim and powerful, pretty and fast, flexible and stable code editor ;)

This guide assumes an ubuntu 11.10(gnome shell) system, but will also work for similar variants (Btw, what’s the first thing you should adjust in a new ubuntu system? Change the gnome theme to radiance!).

Aside from that, you should already have installed ruby and git (sudo apt-get install git-core). If you still use GNOME 2/gedit 2, please use this previous article instead.

Scaffold

Firstly, ensure that you’ve got the official gedit-plugins package:

sudo apt-get install gedit-plugins

Besides this official package, I usually recommend installing the gedit-gmate package. However, one disadvantage is that plugins get not stored in the default user plugin directory. Moreover, it comes with an extra Ruby language specification for Ruby files in Rails, so editing Ruby files can lead to different colorizations, which is quite confusing. Nevertheless, gmate comes with lots of useful additions that will be used below.

On the other hand, I’ve started to improve the Ruby (and related) gedit configurations and called this rubybuntu-gedit. It’s not finished, yet, but already includes better Ruby specifications. Still, it does not include other web-related language definitions, but gmate has them! In some folder, clone the gmate repo:

git clone https://github.com/gmate/gmate

Copy the files from gmate/lang-specs to /usr/share/gtksourceview-3.0/language-specs:

sudo cp gmate/lang-specs/*.lang /usr/share/gtksourceview-3.0/language-specs

Next up, install the rubybuntu-gedit gem. It will copy more language files to the above folder, delete the rails and rhtml files and set some gnome mime types (install as sudo when prompted):

gem install rubybuntu-gedit
rubybuntu-gedit install -3

After these steps, gedit can speak languages like Ruby 1.9, Sass, Haml, Cucumber, Gemfile.lock etc. ;)

Settings

Now, start gedit!

Here are some recommendations for the basic settings at the Edit/Preferences dialog:

View Display line numbers on
View Highlight current line on
View Highlight matching bracket on
Editor Tab width 2
Editor Insert spaces instead of tabs on
Editor Enable automatic indentation on

Under the Font & Colors tab, you have to either select the RubyBuntuOne or the RubyBuntuTwo theme to use the enhanced language specifications from above.

The redundant toolbar can be deactivated in the View menu. The default shortcut for toggling the bottom- and sidebars is (<Ctrl>+)<F9>.

Hot hot keys

Before getting into the plugins, let me highlight that good shortcuts for good features are very important. What is the use of a plugin that you do not use, because you always forget how to use it?

ubuntu/gnome offers a nice way to change menu shortcuts: The global Editable menu shortcuts setting. It allows you to change a shortcut of a menu item by moving the mouse over the item and pressing the desired shortcut.

Unfortunately, it has disappeared in ubuntu 10.4 from the appearance settings. But it is not lost, it is just hidden. You can get it back in GNOME 3 with the gsettings command:

gsettings set org.gnome.desktop.interface can-change-accels true

And while we’re on it, let’s also improve the way the home key works in gedit (should jump to the first char, instead of the absolute beginning of a line):

gsettings set org.gnome.gedit.preferences.editor smart-home-end before

Plugins

Installing gedit plugins is quite easy. Just place the downloaded files of a plugin at: ~/.local/share/gedit/plugins/. Pay attention that the .plugin file must lay exactly in the directory. In most cases, it comes with an additional .py file and/or a folder named after the plugin. Plugins can be activated at Edit/Preferences/Plugins.

Below is a list of plugins I use, ordered by subjective importance. Every plugin without a download link is available in the official plugin package or in gmate(gedit3). I’ve alse added a shortcut suggestion for some of them.

Snap open

Download. This is the most important plugin. Press the shortcut and type the filename you want to open – it integrates with the file browser pane and searches through all files in the selected root directory. In about 9 of 10 file switchings, I use this plugin.
File/Snap open Strg+O

Code comment

Comment out whole blocks of code with one keystroke.
Edit/Comment Code Strg+#
Edit/Uncomment Code Strg+Alt+#

Advanced Find/Replace

Download Full text search, unfortunately, not really stable :/. Useful non-gedit alternatives are, for example, the gnome-search-tool or grep: alias grp='grep . --color=auto -r3e '

Snippets

Another wonderful helper. It lets you easily include code templates by typing an alias and pressing <Tab>. Furthermore, it puts the cursor to the right spot (e.g. between the html tags). Some templates even have multiple cursor spots (hit tab again). Alternatively, you can press <Strg>+<Space> to get a list of available aliases (which is also nice for learning them). You can easily customize the snippets at Tools/Manage Snippets…. More on snippets in a future post.

Word completion

This plugin searches trough all open documents (which are quite a lot when using Snap open) and offers known words for completion.

Draw Spaces

Draws little bullets as spaces and little arrows as tabs :).

External Tools

Lets you execute any system command from within gedit! The definite toy for power users :). For example, you can easily execute your document as Ruby code with this snippet:
#!/bin/sh
ruby
Input: Current Document
Output: Display in bottom pane

Change ruby to ruby -c to only check the syntax. There are lots of more External Tools snippets at the gnome wiki and in this blog article. GNOME 3 stores the tools in ~/.config/gedit/tools. Little pitfall: When using Ruby via RVM, you need to load RVM (the code line you also find in your .bashrc) prior executing ruby. Alternatively, start gedit from a RVM-powered terminal.

Smart Highlight

Download. Highlights all occurrences of the currently selected text. This one spots a lot of crazy typos!

Pair Character Completion

Download. Automatically adds common characters like closing brackets – but there are occasions, where it does the wrong thing (e.g. you just need one " but get two ""). Nevertheless, I like it.

Color Picker

Press a shortcut when cursor is on a color value to get a nice color picker. Useful for web/css development. official plugin, but seems to be broken

Open URI Context Menu

Download. Adds options for visiting and downloading links in its context menus.

Embedded Terminal

A gnome-terminal in the bottom pane (Strg+F9). I rather have my terminals in extra windows, but it is ok for a little irb.

Regex Search and Replace

Download. Basic regex search.

I miss my gedit 2 plugins!!!11

This list is definitely shorter than the one for gedit 2, many plugins are not ported, yet. For example, I haven’t found a replacement for these ones:

  • Smart Indent
  • TODO List
  • Rails Extract Partials

In addition, I miss an indent plugin that allows you to (un)indent using <Alt> and the arrow keys. Although gtksourceview (which is a part of gedit) offers indenting using (<Shift>)<Tab>, it’s absolutely not satisfying when trying to indent single lines.

I will update this post with new gedit 3 compatible plugins that I’ll use (or maybe write). New plugins can also be found at:

Rails Footnotes

Last but not least, some extra sugar for Rails-developers: The rails-footnotes gem! It adds useful information about the current Rails request to the bottom of each rendered browser page. It also provides helpful links to associated files – which open in gedit!

footnotes are added at the bottom of every page

But that is still not everything… When you get an error page, you can click on the referenced line and gedit will open the accordant document and jump to the line!

You can click on an error..

...and gedit opens the accordant file on the right line

Total convenience! I gained much productivity using this tool. Because it’s written with textmate in mind, some tweaking is needed. Let’s start with adding this line to the development section of your Gemfile:

gem 'rails-footnotes'

Then run the generator:

rails generate rails_footnotes:install

The next step is to create a textmate handler that the browser uses to open txmt:// links. Create it somewhere on your disk, for example in ~/.txmt_handler:

#!/bin/bash
# simple txmt: to gedit handler (simple because: needs this order and completeness)
# see http://rbjl.net/58
if [[ $1 =~ ^txmt://open\?url=file://(.*?)\&line=([0-9]*)\&column=([0-9]*)$ ]]
then
  exec /usr/bin/gedit ${BASH_REMATCH[1]} +${BASH_REMATCH[2]}:${BASH_REMATCH[3]}
fi

After making this file executable (chmod +x), you need to add the txmt: protocoll handler to your browser. In firefox, just go to about:config and create a boolean key named network.protocol-handler.expose.txmt that you set to false. When you now click on a txmt: link for the first time, firefox will open a popup, in which you can select the script from above.

Creative Commons License

Comments

07.12.11

A.I.

Why you don’t use Ubuntu on Rails PPA and don’t install gedit-gmate package?

07.12.11

J-_-L

Hi ai. Besides the facts in the guide, the main reason is: To have control of which plugins get installed. Furthermore, one can faster react on new plugins in your gmate github repo ;)

15.12.11

stephen murdoch

these instructions worked fine on Lubuntu (11.10), thanks very much

31.03.12

Stefan

01.04.12

Justin Federspiel

Bit late to the party, but thanks for the guide! Looks like I've got a pretty sweet gedit setup going, having followed your direction.

17.04.12

Stefan

Do you have any idea as why syntax highlighting would stop working? I already had gmate installed as sudo.. so i deleted ruby_on_rails.lang and rhtml.lang, installed your gem as sudo.. now every .rb file I open defaults to "plain text". I have checked the mime types, they seem ok.. Also i had no problems on a clean install (following your guide word by word).

Any help is appreciated! Thank you in advance

12.08.12

Lucas

I have ruby installed via rvm and I can't run the ruby interpreter with gedit's external tools. How can I load RVM?