I have code similar to this:

<%= article.body %></td>
<%= article.author %></td>
<%= link_to 'Show', article %></td>
<%= link_to 'Edit', edit_article_path(article) %></td>
<%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>

I want to delete the HTML tags from the end of the lines in Vim. The only way I thought of was a search and replace. I know I can insert and append text to multiple lines, but is there a way to delete text from multiple lines?

link|improve this question
What's wrong with find and replace? – frabjous Jan 30 '11 at 2:56
@frabjous, nothing is wrong with find and replace, I just thought that if you can add text to multiple lines, you would be able to delete text from multiple lines. – mbreedlove Jan 30 '11 at 4:57
You can delete text from multiple lines... with find and replace. That really seems like the natural way to do it; it isn't clear to me what kind of commands could be used that would be more natural or require fewer keystrokes, even theoretically. – frabjous Jan 30 '11 at 15:47
feedback

2 Answers

up vote 3 down vote accepted

Search and replace, after '<', a string of characters not (^) the '<' character, until your reach a '>' that is also at the end of the line:

%s/<[^<]+>$//g

Correction:

%s/<[^<]\+>$//g

link|improve this answer
1  
The plus needs to be escaped and there's no need for the g since it's an anchored pattern. – Dennis Williamson Jan 30 '11 at 5:07
Great job explaining the regex, it's really helpful. However, I'm looking for a way to delete characters off multiple lines. – mbreedlove Jan 30 '11 at 5:08
That's what the % is specifying, that the substitution be done for every line. See :help 10.3, :help :range, :help 04.4 and :help visual.txt for more possibilities. – garyjohn Jan 30 '11 at 11:54
Dennis: thanks for the comment. I've corrected the 'plus' above. I'm a habitual 'g' user, so I tend to stick it in all the time. But you are right -- works fine without the 'g'. – Rolnik Jan 30 '11 at 15:49
feedback

That's one valid method.

From http://vim.wikia.com/wiki/Power_of_g

:[range]g/<pattern>/cmd

An Ex command is one starting with a colon (':'). The command works on the specified range (default whole file), by executing the Ex command cmd for each line matching . Before executing cmd, "." is set to the current line.

This seems an equally facile way to run a command on a group of files.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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