Oct 14, 2007

Ubuntu 7.10, rails, gedit and gtksourceview 2.0

In about 4 days a long awaited version of Ubuntu will be released. I'm not one of those who can just sit and wait for the final release, thus I've been already using Gutsy for a few weeks now.
The most visible changes include compiz turned on by default on capable hardware (that means intel-based cards and some of the ATI cards) and can be easily turned on on hardware that needs proprietary drivers (ie. AMD/NVidia and most ATI cards). Spotlight fans will be pleased to see tracker installed by default and deskbar on their top panels. Network manager got some love too, and now should work for many more users and be able to talk to a wider range of hardware.
The bad news is that deskbar has undergone a total GUI redesign, which turned out to be a big mistake - most people just hated it. Fortunately the maintainer of the app reacted quickly enough to get lots of bug fixes to Ubuntu's repos on time, so that deskbar is usable again (but it's still far from what it used to be). Another slight disappointment is the kernel version - Ubuntu got stuck with 2.6.22 (which is known to cause many problems) as 2.6.23 didn't make it before kernel freeze.
As a big rails fan I'm very excited about new gtksourceview, which supports context-aware syntax highlighting. That means we'll finally get properly colored Ruby syntax within erb blocks in gedit/scribes! But.. it's too good to work out of box, so you'll need some tweaking. I know there are some HowTos out there, but none of them "just worked" for me, so ... here's what I did.

Associate files with gedit

First of all check if you have a file called Override.xml in your ~/.local/share/mime/packages directory. If you do see if it has definition for x-extension-rhtml file inside - if you don't know how to speak XML just see if the "x-extension-rhtml" phrase exists somewhere in the file. If it does, delete the section containing the definition or, if you don't know how to properly edit XML file, just delete the whole Override.xml file. This is what took me about an hour to figure out as it overrides system-wide .rhtml definition and caused my system not to recognize rhtml files properly.

Now we need to teach gnome to recognize .rhtml files and give them their own mime-type. First, download the file with mime-type definitions for rails files. and copy it to /usr/share/mime/packages directory.

Then, as root, you'll need to update mime database. To do so, just use this command:

$ sudo update-mime-database /usr/share/mime

Now your Gnome should be able to recognize .rhtml files as text/rhtml mime-type. Now just right click on an .rhtml file, choose "Open with..." and use gedit/scribes to open the file. Nautilus should remember you want your favorite editor to open these files so from now on you can just double-click on a file to open it.

Context-aware syntax highlighting

Gtksourceview doesn't include definitions for rhtml files by default, so I've had to write them by hand. Here's the lang definition for RHTML files. Just download it and copy to /usr/share/gtksourceview-2.0/language-specs/ directory and restart gedit/scribes. And enjoy.

Code snippets


Everyone just loves code snippets, and now you can define a set of snippets specifically for RHTML files as it has it's own mime-type now! For some of you who are as lazy as I am, I've posted my code snippets for rhtml files. They aren't perfect, but I haven't had much time to work on them. Anyways I hope you'll find it useful. If you'd like to share your own snippets with others email me at robert_at_aenima_dot_pl and I'll post them here.

Other useful plugins

There are some plugins for gedit that can make your life easier. One of them is SnapOpen - it lets you quickly find a file in application's directory tree. Another plugin I use is Rails Hotkeys, which you can get from SVN by typing:

svn co http://svn.simplesideias.com.br/general/gedit/plugins/rails_hotkeys/

in the command line. Some people may find Class Browser plugin useful. It's pretty neat, but still very buggy. And of course I recommend to use the terminal plugin bundled with gedit.

Other resources (added 2008-09-29)

LT has created a nice cheat sheet for snippets for gedit available at http://www.scribd.com/doc/6299312/Gedit-Cheat-Sheet-for-Rails-Development (post in Spanish: http://lt1982.blogspot.com/2008/09/gedit-cheat-sheet-para-ruby-on-rails.html). Thanks!

23 comments:

  1. Thank you, thank you, thank you!!!!!

    I have been looking everywhere to get gedit to highlight my rhtml files after upgrading to Gutsy. I finally googled the phrase "gtksourceview-2.0 rhtml" and your post was the only result. Jackpot!

    I just recently discovered how awesome gedit is for Rails development. I can't thank you enough, my new Gutsy install is finally complete.

    ReplyDelete
  2. sweet, thanks for posting =)

    ReplyDelete
  3. thanks, you rock! been at it for about 8 hours now and you have cemented in the last few pieces of the puzzle.

    btw, lots of snippets here:

    http://blog.nationcode.com/2007/5/30/emular-textmate-en-linux

    to those of you not in the know, only use his snippets. the other stuff mostly won't work on gutsy

    ReplyDelete
  4. thanks a lot for the info. i just wrote about it here
    http://crepuscular-homunculus.blogspot.com/2007/10/gedit-for-ruby-and-everything-else-on.html

    ReplyDelete
  5. Hi :)

    I'm glad you all found this useful.

    @polypus: Thanks for linking and for the tip about Rails 2.0 globs. I've updated my mime-type XML file with the new extensions.

    ReplyDelete
  6. Thanks a lot. My dayjob is ASP.NET dev, and I am just starting to do some Rails under Ubuntu at home. Both the framework and the development platform are quite a refreshment after years of Windows and Visual Studio.

    ReplyDelete
  7. The link to rails.xml is broken

    ReplyDelete
  8. Sorry, the server where I keep files had a bit of downtime, all is working fine now.

    ReplyDelete
  9. Very good post!!! Now everything is working right with my Ubuntu 7.10 and Gedit 2.20!!!

    Thanks!!!

    ReplyDelete
  10. The RHTML snippets are really useful. Thanks for sharing !!

    --
    sur
    http://expressica.com

    ReplyDelete
  11. I can't seem to get erb tags highlighted...they were highlighted before I updated to Gutsy from Feisty...any ideas ?
    I've tried:

    style name="rhtml:erb-tag" foreground="green" />
    style name="rhtml:erb-closing-tag" foreground="green" />

    in my style xml file but no go.

    ReplyDelete
  12. Excellent! Thanks for the tips on getting RHTML highlighted properly - I've recently started using Ubuntu for development at work, and the lack of syntax highlighting was a nightmare.

    ReplyDelete
  13. Oh my... I just discovered your awesome setup for Gedit, as I'm just getting started with RoR. Fantabulous. Linked to you from crepuscular homunculus.

    ReplyDelete
  14. i need highlighting for html.erb, rs.rjs files...how do i go about this? rhtml just aint enough

    ReplyDelete
  15. @Teena: this will make .rjs and .erb files work as expecteded, too. I already updated XML files to support the additional extensions quite a long time ago.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Hey,
    I followed these steps, and those of some other websites - and it works as expected. However, there is one small "problem". In rhtml (or erb), code inside of <% %> is marked as Ruby code, which means it gets the Ruby syntax highlighting -- which is great. What it doesn't get is the Ruby snippets! How do you fix this? Is this a bug in the snippet plugin? Or is there some extra definitions you have to do? It would be nice not to have to redo all the Ruby snippets for rhtml, especially for .. each .. in, and each .. do which are used a lot in rhtml.

    ReplyDelete
  18. Thanks for the tip about .local/share/mime. gEdit was not showing any YML files in the file browser, and it was due to some rogue files in that directory, both in applications and in packages. I never would have figured this out without your post!

    ReplyDelete
  19. I have a set of snippets for gedit on GitHub. Read more on that on my Blog or check out the demonstration on Vimeo

    ReplyDelete
  20. UPDATE for people that stumble upon this as I did.

    your rails definition file does not work with ubuntu 10.04.
    you'll need to explicitly declare "*.html.erb" instead of just "*.erb"

    so replace this line:


    with this:


    and you'll be all set.

    ReplyDelete
  21. Also found a link to: http://github.com/mig/gedit-rails

    It's all packaged up nicely, and seems to work straight away.

    Thanks

    ReplyDelete
  22. http://github.com/mig/gedit-rails
    Is great. Thanks for posting that. By the way, the server for the rails.lang and rails.xml files is down, so if someone has a mirror that would be awesome!

    ReplyDelete