Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, how do you search for a word boundary character, like the \b in regexp?

Tags:

vim

People also ask

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What is \b word boundary?

A word boundary \b is a test, just like ^ and $ . When the regexp engine (program module that implements searching for regexps) comes across \b , it checks that the position in the string is a word boundary.

How does word boundary work in regex?

Introduction to the Python regex word boundaryBefore the first character in the string if the first character is a word character ( \w ). Between two characters in the string if the first character is a word character ( \w ) and the other is not ( \W – inverse character set of the word character \w ).

How do I find a word in a regular expression?

The regular expression \b[A]\w+ can be used to find all words in the text which start with A. The \b means to begin searching for matches at the beginning of words, the [A] means that these matches start with the letter A, and the \w+ means to match one or more word characters.


/the\>

See :help /ordinary-atom

I assume "regexp" means PCRE. It is worth noting that Vim's regex syntax differs from (and apparently predates) PCRE.

See also:

  • Why does VIM have its own regex syntax?
  • What's the difference between vim regex and normal regex?
  • Within vim's regex engine, why are some metacharacters escaped and some are not?
  • Can I make vim accept \b rather than just \< and \>?

Use \< and \> for word start and word end, respectively.

E.g. In your specific case you would use:

/the\>/

If very magic is turned on, then you shouldn't escape the > character. See what's magic search. SO in your case you'd do:

/\v<the>

it would search for only the word 'the'.


if you are trying to search a word at your cursor. you can just hit *, or # for backward search.