3

My file consists of the following text

one
bone
stone
cuttlebone
minestrone
cornerstone

and I want to select all instances of bone and stone, so I get the following highlighted text:

one
BONE
STONE
cuttleBONE
minestrone
cornerSTONE

NB: capitals indicate highlighted text since I can't format in code.

I have tried the following search patterns, but none of them work

/bone|stone
/[st|b]one
/[\(st\)b]one
/[bst]one
/[b(st)]one

The problem is stone and bone are different lengths, and I don't know how to match to two different length options.

How can I match all instances of stone and bone in my file?

  • You can try fuzzy search plugins as well ;) – SibiCoder Jun 23 '16 at 13:29
8

You need to escape the |:

/bone\|stone

If you want to reuse the one part, you can do:

/\(b\|st\)one

This create a sub expression b\|st and add one to it.

As DJ McMayhem suggested, you can use the \v "verymagic" flag:

/\v(b|st)one 
5

Nobe4's answer is spot-on. Here's an alternative.

vim-abolish. This plugin works fantastic for searching and/or replacing simple variants on a word. For example, let's say you want to turn 'child' to 'adult', while also getting 'children' to 'adults', and all case-variants. You can do that with:

:Subvert/child{,ren}/adult{,s}/g

In your case, you can do a subvert search with:

:Subvert /{st,b}one

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.