Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert ERB-tags with Vim?

How do I insert <% %> in Vim? I have rails.vim.

like image 734
jriff Avatar asked Nov 25 '10 08:11

jriff


2 Answers

It is also possible to insert erb tags in INSERT mode (using rails.vim and surround.vim plugins):

  1. Press Ctrl + s and = afterwards to insert <%= %> block
  2. Press Ctrl + s and - afterwards to insert <% -%> block
  3. Press Ctrl + s and # afterwards to insert <%# %> block
like image 87
Rolands Bondars Avatar answered Oct 23 '22 12:10

Rolands Bondars


I've been using the surround.vim plugin for a long time, but not the rails.vim plugin. Turns out that the rails.vim plugin is adding functionality to surround.vim, or is piggy-backing on it. Either way, I got it working this-a way:

  1. Install the rails.vim plugin if it isn't already, and open an ERB document. Vim should recognize it as a eruby filetype.
  2. If it doesn't use :set filetype=eruby Return to make Vim see things your way.
  3. Visually select some text you want to wrap with the ERB tags using v or V.
  4. Press S= or S-. (That's capital S)
  5. The selected text should be wrapped in a <%= %> or <%- %> block.

I just confirmed this works in column-selection mode too.

This is documented in the rails.vim help: from the command-mode search for :h rails-surround. And if you don't have the help available for rails.vim, it's because the instructions weren't followed for installing it:

:helptags ~/.vim/doc

The rails.vim plugin requires the surround.vim plugin also, so that has to be previously installed. I use that plugin's functionality at least once a minute when I'm editing. It rocks.

like image 21
the Tin Man Avatar answered Oct 23 '22 11:10

the Tin Man