8

I've just read this:

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

and I was wondering - can't I make vim recognize \b also, somehow?

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 6
    No, Vim uses its own regular expression engine with its own syntax, where `\b` already has its own meaning: "backspace". – romainl Oct 12 '15 at 12:44
  • 2
    Have a look at [*Perl compatible regular expressions*](http://vim.wikia.com/wiki/Perl_compatible_regular_expressions) at Vim Tips Wiki. – Wiktor Stribiżew Oct 12 '15 at 12:46
  • @stribizhev: Make that an answer, I think. Although it won't work for me, since on Fedora 20 (blech) vim is compiled with `-perl`. – einpoklum Oct 12 '15 at 12:55
  • One thing to note: if you use `\v` to make your regex "very magic", then you can use `<` and `>` instead of `\<` and `\>`, which is slightly more convenient, and in general makes the Vim regexps feel a bit more Perl-like. The [Loupe plugin](https://github.com/wincent/loupe) can be used to make `\v` the default. – Greg Hurrell Oct 13 '15 at 03:48

1 Answers1

8

Since Vim's regex flavor treats \b as a backspace character, and there is no chance re-defining this shorthand construct, you can only make it work with a PCRE regex engine that can be used with Vim the way described at Perl compatible regular expressions Vim Tips Wiki.

This is the description from that page:

  1. Verify with :ver that +perl or +perl/dyn is compiled in.

  2. Install Perl if necessary. On Windows, ActivePerl is standard but any dependency-free perl58.dll will work if you don't need any other perl modules. If you don't want a full install of perl, copy perl58.dll from Strawberry Perl 5.8.x into the folder vim.exe lives and the commands below will work.

  3. Type :perldo s/searchme/replaceme/g

Note: +perl/dyn doesn't seem to be necessary.

Or if Ruby is compiled in, Ruby's regex can be used, too to recognize \b as a word boundary:

Or if you have ruby compiled in (look for +ruby in :ver output)

Equivalent to s/pattern/replacement/g:

:rubydo gsub /pattern/,'replacement'

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563