Search code examples
rubysublimetext3erbruby-on-rails-5code-snippets

Wrap with abbreviation for ERB


I love Emmet's Wrap With Abbreviation (docs).

It lets me quickly create a html list from a regular list

start:

foo
bar
baz

command:

ul>li*  

result:

<ul>
  <li>foo</li>
  <li>bar</li>
  <li>baz</li>
</ul>

I really want to be able to do the same with with ERB tags

start:

link_to micropost.user.name, micropost.user
micropost.content
time_ago_in_words(micropost.created_at)

command:

???

end:

<%= link_to micropost.user.name, micropost.user %>
<%= micropost.content %>
<%= time_ago_in_words(micropost.created_at) %>

Is this possible?

Currently I'm using erb snippets and it's awful to have to focus on my erb line by line.

I want to focus on just my ruby all the way, then focus on the erb tags, then focus on the html tags, then focus on the html classes/ids.

I found a great snippet that lets me wrap individual lines, however if If I try it with multiple lines I get:

start:

user.cats
user.dogs

command:

<snippet>
  <content><![CDATA[
<%= ${0:$TM_SELECTED_TEXT} %>
]]></content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <!-- <tabTrigger>hello</tabTrigger> -->
  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <!-- <scope>source.erb</scope> -->
    <description>ERB: Wrap with =</description>
</snippet>

result:

<%= user.cats
user.dogs %>

But I'd like to get

<%= user.cat %>
<%= user.dogs %>

Would love some help :)


Solution

  • One way to get this to do what you want is to split your selection into multiple lines using Selection > Split Into Lines (Ctrl+Shift+L on Windows/Linux). Once you do that when you invoke the snippet it will operate on each selection individually.

    Sample session