fontno
fontno

Reputation: 6862

Vim how to add a hash tag to an eruby (erb) file in order to comment out a line

I'm still learning macvim and I love it but I haven't figured this out. So, say I have a rails or ruby view file and I want to comment out a line or multiple lines like this

post.html.erb

<span class="timestamp">
  <%= time_ago_in_words(post.created_at) %>
  <%= post.created_at %>
</span>

In order to comment out the lines, add a hash tag after the percentage sign. This will comment out the line

<span class="timestamp">
  <%#= time_ago_in_words(post.created_at) %>
  <%#= post.created_at %>
</span>

The nerd commenter plugin will let you visually select the line(s) and <leader> cc will add comments but it wrong. It does this

<%#<%= time_ago_in_words(post.created_at) %>%> 
<%#<%= post.created_at %>%>

it adds another set of <%# %> around the originals and the closing tags will show up in the view.

I want it to look like this

<%#= time_ago_in_words(post.created_at) %>
<%#= post.created_at %>

So the real question is how to map a command that will insert only the hash tag after the percentage sign on visually selected line(s)?

Upvotes: 0

Views: 402

Answers (2)

romainl
romainl

Reputation: 196886

Comment:

xnoremap <leader>c :s/^\s*<%/&#<CR>
nnoremap <leader>c :s/^\s*<%/&#<CR>

Uncomment:

xnoremap <leader>C :s/\(^\s*<%\)#/\1
nnoremap <leader>C :s/\(^\s*<%\)#/\1

Upvotes: 1

damienfrancois
damienfrancois

Reputation: 59320

When you have selected the lines in visual mode, you can enter command mode (:) and enter s/<%=/<%#=/. Binding that command to a custom shortcut would do it ?

Upvotes: 2

Related Questions