Vim html.erb snippets?? snipMate Need a vim tip
Asked Answered
B

7

13

When I'm in an html.erb file, I get no snipMate snippets.

I would like both HTML and Ruby, or just HTML would be fine, How would I do this?

Would I need to write a set of snippets?

If so, is there a way of pulling in existing snippets without copying them?

Is there a way of telling vim to go into html mode when it sees .html erb?

Boatload answered 11/1, 2011 at 14:38 Comment(0)
R
12

Snippets are stored in directory called snippets somewhere in your ~/.vim folder.

If you look there, there is usually one file per filetype, there is a c.snippets, a ruby.snippets, so it seems what you have to do is to create an erb.snippets there with what you want.

Eventually you could copy the content of ruby.snippets and html.snippets into your new erb.snippets.

Alternatively you can search on github, some people have posted their own erb.snippets configuration. For example, there is a nice collection there : https://github.com/scrooloose/snipmate-snippets

The best thing would to try first to open a snippet file and look at the syntax, it is pretty easy to create your own snippet depending on what you use the most.

Rosewood answered 11/1, 2011 at 14:51 Comment(3)
Awesome thanks.. the erb-rails is lacking in html tags so i'll just copy em in! Thanks.Boatload
how did you make it work? I've added erb.snippets to the snippets folder and added a few snippets but still it says "Pattern not found" while snipmate works perfectly with .c .rb and other native-supported files.Wanettawanfried
This looks like it needs to be refactored though github.com/scrooloose/snipmate-snippets/blob/master/ruby-rails/… . I would rather use respond_with so I'm working on some new snippets.Elative
A
17

You can use an autocmd to set the filetype to html when opening a ".html.erb" file. This could have unwanted side effects for plugins that work for ".erb" files.

autocmd BufNewFile,BufRead *.html.erb set filetype=html

You can also load more than one set of snippets by using a dotted filetype:

autocmd BufNewFile,BufRead *.html.erb set filetype=html.eruby

See :help snippet-syntax in the snipMate help for more info.

Anethole answered 11/1, 2011 at 14:51 Comment(1)
Be careful with this. Syntax highlyting got messed up: dl.dropbox.com/u/750013/SS_2012-11-01_21%3A27%3A41.png.Untread
R
12

Snippets are stored in directory called snippets somewhere in your ~/.vim folder.

If you look there, there is usually one file per filetype, there is a c.snippets, a ruby.snippets, so it seems what you have to do is to create an erb.snippets there with what you want.

Eventually you could copy the content of ruby.snippets and html.snippets into your new erb.snippets.

Alternatively you can search on github, some people have posted their own erb.snippets configuration. For example, there is a nice collection there : https://github.com/scrooloose/snipmate-snippets

The best thing would to try first to open a snippet file and look at the syntax, it is pretty easy to create your own snippet depending on what you use the most.

Rosewood answered 11/1, 2011 at 14:51 Comment(3)
Awesome thanks.. the erb-rails is lacking in html tags so i'll just copy em in! Thanks.Boatload
how did you make it work? I've added erb.snippets to the snippets folder and added a few snippets but still it says "Pattern not found" while snipmate works perfectly with .c .rb and other native-supported files.Wanettawanfried
This looks like it needs to be refactored though github.com/scrooloose/snipmate-snippets/blob/master/ruby-rails/… . I would rather use respond_with so I'm working on some new snippets.Elative
S
8

I am currently on a promoting tour for UltiSnips on StackOverflow. UltiSnips supports extending other file types, your erb.snippets would look like this:

extends html, ruby, rails

snippet temp "A snippet only in Erb"
erb rules ${1}
endsnippet

A conversion script for snipMate snippets is shipped with UltiSnips, so switching is easy.

Sextuplet answered 27/7, 2011 at 5:54 Comment(2)
So, why not just include that extends line in the ultisnips for eruby?Narbonne
I do know nothing about ruby, eruby, erb or any other html or web related coding stuff. So - I need to rely on users providing useful snippet definitions and .snippet files here. Feel free to send a patch.Sextuplet
W
3

I used the autocommand method to the set the filetype, but then I got html syntax errors for things like this:

<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>

The last two angle brackets would be highlighted in red, which drove me bonkers. So, I created a symlink called eruby.snippets that points to html.snippets. That worked like a champ and now I don't have to make changes in two places. I also have an eruby-rails snippet directory for non-html eruby snippets.

This is on a Mac OS X system. Note that an alias won't work. You need to hit the terminal and use the ln command. Not sure about doing this on a Windoze system.

Wearing answered 17/3, 2011 at 18:55 Comment(0)
C
2

You can assign multiple snippets scopes to a single filetype. (I've found that altering the filetype tends to break some syntax highlighting).

You can check that the filetype for erb files is indeed 'eruby' with:

:set filetype?

If you're using the maintained fork of snipmate, it looks like you'll want both the eruby.snippets and eruby-rails.snippets from the snipmate-snippets repository (owned by honza, but I don't have enough reputation to link to it here) (see the INSTALL section of the snipmate README for proper setup).

If you are using the maintained fork, I believe setting g:snipMate.scope_aliases in your .vimrc with the following will work for your example:

let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['eruby'] = 'eruby,eruby-rails'

I've added a pull request to snipmate to have their documentation updated.

Conwell answered 7/1, 2012 at 22:38 Comment(0)
C
1

Jumping on the UltiSnips bandwagon after trying SnipMate for a while. Like SirVer mentioned, having the html, ruby, etc snippets available within an *.erb file was as simple as adding the extend line to the eruby.snippets file.

Cony answered 5/3, 2012 at 1:21 Comment(0)
T
0

With the original snipMate plugin, create a file ~/.vim/ftplugin/erb_snippets.vim and put the following into it:

silent call ExtractSnipsFile(g:snippets_dir . 'html.snippets', &l:filetype)
silent call ExtractSnipsFile(g:snippets_dir . 'ruby.snippets', &l:filetype)
Tricuspid answered 27/10, 2012 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.