28

I can't for the life of me find an answer to this either on google or here or in the help files.

if "test.c" =~ "\.c"

At first I thought =~ mean ends in, but observe these results:

Command                               Result
echo "test.c" =~ "\.c"                1
echo "test.c" =~ "\.pc"               0
echo "test.pc" =~ "\.c"               1
echo "testc" =~ "\.c"                 1
echo "ctest" =~ "\.c"                 1
echo "ctestp" =~ "\.pc"               0
echo "pctestp" =~ "\.pc"              0
echo ".pctestp" =~ "\.pc"             0

An explanation would be great. A link to a site attempting to decipher VimScript would be even better.

puk
  • 16,318
  • 29
  • 119
  • 199
  • 2
    There is no way `"ctest =~ "\.c"` and `".pctestp" !~ "\.pc"`. – Benoit Mar 02 '12 at 07:33
  • 1
    By the way, it's probably best to avoid `=~`, but use `=~#` or `=~?` instead, which explicitly specify whether you wish case-sensitivity or insensitivity. With a plain `=~`, it depends on the user's current settings. More info: http://vimdoc.sourceforge.net/htmldoc/eval.html#expr4 – Evgeni Sergeev Jul 13 '15 at 15:11

2 Answers2

54

From the Vim documentation, it does a pattern match of the right operand (as a pattern) inside the left.

For strings there are two more items:

    a =~ b      matches with
    a !~ b      does not match with

The left item "a" is used as a string. The right item "b" is used as a pattern, like what's used for searching. Example:

    :if str =~ " "
    :  echo "str contains a space"
    :endif
    :if str !~ '\.$'
    :  echo "str does not end in a full stop"
    :endif

You might try your test cases again. I get, for example, inconsistent with yours:

echo ".pctestp" =~ "\.pc"             1

And double-quotes vs single quotes seem to affect how the backslash is interpreted:

echo "test.pc" =~ "\.c"               1
echo "test.pc" =~ '\.c'               0
816-8055
  • 509
  • 7
  • 15
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Thanks, it was the double quotes that were giving me the strange behavior – puk Mar 02 '12 at 04:03
  • @puk What I've not yet located are the docs that explain the differences in quoting... – Michael Berkowski Mar 02 '12 at 04:08
  • 1
    I find the documentation on vim is all over the place. The `:help` pages are exceedingly helpful for simple things like `d` or `:ls`, but useless if you don't know what you're looking for (ie. `stridx()`). Similarly, the vim wiki is great for keys and commands, but not so much for functions like the aforementioned `stridx()`. – puk Mar 02 '12 at 04:48
  • Your note on double- vs. single-quotes solved my problem. Thanks! – Stephen Talley Aug 15 '17 at 14:45
6

From the docs:

  • http://vimdoc.sourceforge.net/htmldoc/usr_41.html

    For strings there are two more items:

    a =~ b      matches with
    a !~ b      does not match with
    

    The left item "a" is used as a string. The right item "b" is used as a pattern, like what's used for searching. Example:

    :if str =~ " "
    :  echo "str contains a space"
    :endif
    :if str !~ '\.$'
    :  echo "str does not end in a full stop"
    :endif
    
icyrock.com
  • 27,952
  • 4
  • 66
  • 85