0

I would like to be able to find an occurence of a pattern, highlight it, go to the next occurence of the pattern, highlight it, go to the next one, and so on and so forth, and on the end delete all the occurences and enter insert mode. For example, find all occurences of one by one and on the end delete all of them and enter insert mode to replace it with

. The reason I want to see the occurences one by one is that I first want to see where the occurence is in the code in order to decide if I want to delete it or not. For example, I have some HTML and want to see where the tags are in a certain code block in order to decide if I want to delete them or not. I know I can do dw to cut a word or cw to delete a word and enter insert mode. I know I can use regular expressions like :%s/my_patter/replace_pattern/gc but that would delete all the occurences in the whole file, which I don't want. I want to first iterate over each occurence of the pattern, skip it if I want to ommit it before I decide if I want to replace it with something else. This is similar to what you can do in Sublime by selecting a word and pressing ctrl+D and then perform the change on all the occurences with one key stroke.

For example, I have this code and in here I would like to replace all the divs but the second one with p at once:

<div class="form-group">
  <%= f.label :daily_hours, class: "col-sm-2 control-label" %>
  <%= f.text_field :daily_hours, class: "form-control" %>
</div>
<div class="form-group">
  <%= f.label :date, class: "col-sm-2 control-label" %>
  <%= f.date_field :date, class: "form-control" %>
</div>
<div class="form-group">
  <%= f.label :work_performed, class: "col-sm-2 control-label" %>
  <%= f.text_area :work_performed, class: "form-control" %>
</div>
  • 2
    The %s/div/p/gc pattern already allows you to confirm each replacement, how is this not precisely what you're looking for? – Mass Jan 6 '18 at 15:10
  • Maybe he wants to review them one at a time (perhaps marking them in some way) but defer the actual change of the selected ones until the end rather than singly as they are encountered. Subtle difference but that's what I'm reading. Is that what you have in mind @jedi ? – B Layer Jan 6 '18 at 17:17
  • Indeed! @BLayer that's exactly what I am looking for + I want to finally edit all the 'marked' tags at once. – jedi Jan 6 '18 at 17:23
  • Ah, okay. vim does not support this by default, but it is possible with plugins. For example, github.com/terryma/vim-multiple-cursors is popular. But, I won't make an answer because I don't use it myself to verify that it solves your question. – Mass Jan 8 '18 at 1:04
  • 1
    You can also use my NrrwRgn plugin. It allows you to collect all matches, narrow them in a new window and on writing it will put them back into the original. – Christian Brabandt Jan 8 '18 at 7:40
1

There are 2 ways to achieve what you want.

  • using c flag for :s
  • searching for whatever text you want and then you can use cgn to change next occurrence and then . through rest of occurrences. The difference there is that with this solution you can use completion, the disadvantage is that this requires more recent Vim and sometimes can cause problems with recursive changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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