Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does =~ mean in VimScript?

Tags:

operators

vim

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.

like image 332
puk Avatar asked Mar 02 '12 03:03

puk


People also ask

What is let G in Vimrc?

l: local to a function. g: global. :help internal-variables. Follow this answer to receive notifications.

What is VimL?

Vim's built-in scripting language, VimL. This language is also known as Vimscript. Depending on how you look at it, either VimL is an alternate name for Vimscript or Vimscript is an alternate name for VimL.

What is VimL script?

Vim script (aka Vimscript, or VimL) is a full feature scripting language, meaning it can solve almost any text processing problem.

What is vim9?

Vim 9 has only a single big new feature: a new scripting language, Vim9script. The goal is to "drastically" improve the performance of Vim scripts, while also bringing the scripting language more into line with widely used languages such as JavaScript, TypeScript, and Java.


1 Answers

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 
like image 56
Michael Berkowski Avatar answered Sep 28 '22 23:09

Michael Berkowski