A simple Vim find and replace guide

A simple Vim find and replace guide

I have been using Vim for years and I have been VERY lazy in learning the quirks of the editor. I do know quite some shortcuts and have played some Vimgolf in the past, as well as made some exercises for beginners ? but I do still have a LOT more to learn.

I am a lazy person so I basically just got a bunch of (opinionated) Vim plugins so I don?t have to install my own. The problem with that is that because of all these things done for me, I have yet to take time to learn some of the more ?basic? Vim features: search and replace.

Anyway, I DO know the basic search and replace functions like %s/<search_term>/<replace_term>/g (add c if you want to confirm each replacement). This command basically just replaces all instances of the search_term with the replace_term i.e. given a text file:

Threw you the obvious and you flew with it on your back a nameIn your recollection down among a million sameDifficult not to feela little bit disappointed and passed overwhen I’ve looked right through to see you

if you do:

:%s/you/me/g

It will basically replace all instances of you to me:

Threw me the obvious and me flew with it on mer back a nameIn mer recollection down among a million sameDifficult not to feela little bit disappointed and passed overwhen I’ve looked right through to see me

(Yes, I just used Vim to do the replacement). Add c at the end and it will ask you if you want to change it for each one (with y being yes, of course and n being hell no!)

On to the next one! This is much simpler and can be done by showing a screenshot like so:

Image for postThe highlighted lines might be hard to see!

As you can see, we have the command :s/hello/kek/g which basically replaces all instances of hello to kek ? BUT ONLY FOR THE SELECTED LINES. Easy again. To date, these two have been the most common search-replace functions I have been using.

Recently though, I have learned (not too late!) to search and replace words and re-use the search term as well. Say for some reason your current task was to convert some Ruby to Javascript (true story).

My pretty Rspec will now be converted to JS!

There were a lot of repetition (never mind that! we?re focusing on Vim!) in the code so I basically just needed to say, convert the let into JS using const:

For this example, we can just go with how I converted let(:street) … to const result …

This brings me to the capture command in Vim. All you need to remember is (.*) This is basically a regex that captures everything inside the parenthesis and assigns it to 1 . So here was my snippet for replacing that line:

:%s/let(:street) { (.*) }/const result = streetTypeExtractor(1)/g

Wait, what?!

Okay, let?s slow down. let?s cut it down to components:

  • :%s ? starts the command of find/replace as we have seen above. This only does it for all instances matching the search term, in this case, the whole let(:street) … line
  • let(:street) { (.*) } ? is the search term. I just want to find the text I want to replace (in this case the whole line) PLUS a capture group I wanted to bind to 1: the term ?Gap Road?. Bear in mind I had multiple lines like these in the whole file, all with different street name examples.
  • const result … was the term I wanted to replace it with. the 1 was a placeholder for whatever the (.*) caught and in the above example, it was ‘Gap Road’ (yes, with the single-quotes).
  • g was just a flag to keep searching and replacing that pattern.

TL;DR, it basically converted all of these:

let(:street) { ‘Gap Gap’ }let(:street) { ‘Gap South East’ }let(:street) { ‘Sample Jelly’ }let(:street) { ‘GaP stReeT’ }

into these:

const result = streetTypeExtractor(‘Gap Gap’)const result = streetTypeExtractor(‘Gap South East’)const result = streetTypeExtractor(‘Sample Jelly’)const result = streetTypeExtractor(‘GaP stReeT’)

(Yes, I used the exact command above to do it as well).

And there you go, easy Vim magics!

24