red888
red888

Reputation: 31540

vim: match word NOT starting with a $

In vim I want to search for a word "var" that does not begin with a $.

I tried this, but it just matches the "var" in "$var" I want to only match "var" if it doesn't begin with a $:

/\$\@!\(\var\)

I think I need to do something like if $ not immediately to the left of word "var" because the word boundary doesn't include the $ right?

Upvotes: 1

Views: 188

Answers (2)

benjifisher
benjifisher

Reputation: 5112

The two solutions in @Kent's answer are not equivalent. Try them on this sample text:

var
$var
foo var
foo $var

I suggest using @Kent's second solution, although you do not need to escape the $:

/$\@<!var

@Kent already gave you a link to answer half of your follow-up question (in the comments). For the other half, read

:help /[]

Upvotes: 2

Kent
Kent

Reputation: 195039

this does what you want

/[^$]\zsvar

if you love negative look-behind:

/\$\@<!var

:h \zs
:h \@<!

will give you detailed explaination.

Upvotes: 3

Related Questions