21

I recently started using Sublime Text 2. What an awesome editor. It does a great job of highlighting Ruby code, but it does not highlight my Gemfile.

Is there a way to get it to do that?

I found this Gist but it has no instructions on how to use it.

2
  • you can probably tell the editor to apply ruby syntax highlighting. After all the gemfile is ruby syntax. but then, what do you put into your gemfile. you're not putting your whole rails app in there, are you? Commented Dec 22, 2011 at 17:42
  • 1
    @three, not at all! It's just that whenever I open the Gemfile, there's no syntax highlighting. I just found the option though. It dones't remember. I have to do it every time I open the file. :/ Commented Dec 22, 2011 at 17:49

4 Answers 4

34

There are at least three options:

  1. Switch syntax manually (not preferred, but easy; no explanation required)
  2. Add "Gemfile" to the list of Ruby-syntax files
  3. Use the plugin you link to and create a package for it

1. No explanation, but handy trick

You can bind a keystroke to set syntax without moving to the mouse.

I bound syntax changing to Ctrl-Opt-Space by adding the following to my user keybindings:

[
  { "keys": ["ctrl+alt+space"], 
    "command": "show_overlay", 
    "args": { "overlay": "command_palette", "text": "Set Syntax: " } }
]

2. Add "Gemfile" to list of Ruby-syntax files

  • Linux: ~/.config/sublime-text-2/Packages/Ruby/Ruby.tmLanguage
  • OS X: ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.tmLanguage
  • Windows: %APPDATA%/Sublime Text 2/Packages/Ruby/Ruby.tmLanguage

You can also get there by using the menu option Preferences -> Browse Packages and going into the Ruby package. Once you're in the file it'll be obvious: it's the <array> element with Ruby-looking filenames. Add <string>Gemfile</string> and you're all set.

It's possible the setting could get overwritten on an upgrade; I'm not sure how that works with ST2–it may be cleaner to do it through code as in the snippet.

3. Using the snippet you linked to

More work (and the correction of one syntax error). You can either do it manually, by creating a directory in Packages (see above for location) or create an actual package and allow ST2 to install it.

I created a test package called "Syntax" and copied the snippet into it, restarted ST2, and opening a Gemfile worked as expected. The correction required an additional colon (new gist), nutshell:

elif name[-3] == "erb": # Needed a semi-colon here.
  set_sintax(view, "HTML (Rails)", "Rails")
Sign up to request clarification or add additional context in comments.

4 Comments

On OSX, it's ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.tmLanguage. I dropped it in right below <string>Rakefile</string>. This has always bugged me too. Thanks to you; and to Mohamad for asking!
@Unixmonkey Oh, right; I was going to add that but got distracted. I think the python approach is more interesting, but it was more of a pain to get running (mostly because of that syntax error I didn't notice until I finally opened the console view :/
Thanks, @DaveNewton! If there's a way to make sure this doesn't get borked by updates to the Ruby bundle (i.e., moving it to a user-scoped settings file), I'd love to hear it.
@BenjaminKreeger I'm not sure if it's 100% borkless, but using the snippet in your own shouldn't be overwritten, at least. I'm not super-clear on how/where the packaging stuff works yet, though.
5

If you are here but are using Sublime Text 3 you might not be able able to find the 'list of Ruby-syntax files' in packages.

Most other solutions found online were confusing to me.

I fixed this by manually changing Gemfile to Ruby in the bottom right hand corner file extension menu item when you have opened the file in Sublime Text 3 (which is what I had been doing each time I opened the file up until now).

Once you have selected ruby then go to Preferences -> Settings-More -> Syntax Specific-User

{
  "extensions":
  [
    "Gemfile",
    "Gemfile.lock"
  ]
}

When you navigate to Syntax Specific User it opens a file specific to the language that the file has syntax highlighting for. You may need to change the file back to whatever it is defaulting too (mine was 'Rd (R Documentation).sublime-settings') and removing Gemfile from that Syntax highlighting file.

In Ubuntu these files are stored at

~/.config/sublime-text-3/Packages/User

Comments

2

The DetectSyntax plugin for ST2 provides a more comprehensive solution to highlighting files - It allows file highlighting based on rules. It's smart enough to understand the difference between a Rails file, other files that use .rb as an extension and standard ruby files.

The standard rules include Gemfile, Rakefile, Guardfile and others matched to Ruby for Syntax formatting.

See DetectSyntax on GitHub.

Comments

-1

You can achieve this by copying the HTML.tmLanguage file in the User/ folder, this way it won't be overwritten by an update.

1 Comment

How does that affect syntax highlighting for a Gemfile?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.