37

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

3 Answers 3

71

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
5
  • 3
    I just saw also that Ctrl + s, Ctrl + s, = will result in a new ERB tag being opened up for a multiline add, at least with my current configuration. As expected, you can do this for the - and # as well. Mar 23, 2012 at 16:05
  • 28
    In terminal vim the mapping is ctrl+g s followed by the surround key. May 7, 2012 at 0:41
  • 4
    Further to Anthony's comment: <C-s> <C-e> (in INSERT mode) will put in a <% * %><% end %> pair (on separate lines with * denoting cursor position).
    – jjnevis
    Nov 8, 2012 at 14:58
  • 8
    Warning: Ctrl+s can result in this: stackoverflow.com/questions/3419820/… Sep 19, 2013 at 12:30
  • I'm using vim-bootstrap.com and, in my case, in insert mode the trigger was capital S instead. Sep 12, 2014 at 16:37
43

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.

2
  • Yeah, thanks for the intro but I do know vim :-) I was looking for a shortcut. Does anyone else know? I can't imagine that rails.vim doesn't have it - I just can't find any documentation for it anywhere.
    – jriff
    Nov 25, 2010 at 11:02
  • 5
    The visual mode shortcut has changed from s to S
    – meleyal
    May 4, 2012 at 8:43
15

For surroundings in the erb views you might need Tim Pope's ragtag. It can handle different erb specific surroundings. Check out the documentation.

1
  • 1
    I can second this answer, you will need vim-rails, vim-surround and vim-ragtag.
    – Jose Elera
    Apr 22, 2019 at 18:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.