How to search using wildcard in VIM

Jimmy P picture Jimmy P · Dec 14, 2016 · Viewed 20.1k times · Source

Using the standard search function (/) in VIM, is there a way to search using a wildcard (match 0 or more characters)?

Example:

I have an array and I want to find anywhere the array's indices are assigned.

array[0] = 1;
array[i] = 1;
array[index]=1;

etc.

I'm looking for something along the lines of

/array*=

if it's possible.

Answer

James picture James · Dec 14, 2016

I think you're misunderstanding how the wildcard works. It does not match 0 or more characters, it matches 0 or more of the preceding atom, which in this case is y. So searching

/array*=

will match any of these:

arra=
array=
arrayyyyyyyy=

If you want to match 0 or more of any character, use the 'dot' atom, which will match any character other than a newline.

/array.*=

If you want something more robust, I would recommend:

/array\s*\[[^\]]\+\]\s*=

which is "array" followed by 0 or more whitespace, followed by anything contained in brackets, followed by 0 or more whitespace, followed by an "equals" sign.