Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript comparison


May 24, 2021 Vim


Table of contents


We've learned about conditional statements, but if we can't compare them, if statements aren't very useful. Vim, of course, allows us to compare the size of the values, but not at a glance as they seem.

Execute the following command:

:if 10 > 1
:    echom "foo"
:endif

Obviously, Vim displays foo Now do the following command:

:if 10 > 2001
:    echom "bar"
:endif

Vim doesn't show anything because 10 no 2001 S o far, so good. Run the following command:

:if 10 == 11
:    echom "first"
:elseif 10 == 10
:    echom "second"
:endif

Vim shows second N othing to be surprised about. L et's try comparing strings. Follow these commands:

:if "foo" == "bar"
:    echom "one"
:elseif "foo" == "foo"
:    echom "two"
:endif

Vim two . It's still no surprise, so what exactly did I start by saying (Vim's comparison isn't as straight-talking as it looks)?

Case sensitive

Execute the following command:

:set noignorecase
:if "foo" == "FOO"
:    echom "vim is case insensitive"
:elseif "foo" == "foo"
:    echom "vim is case sensitive"
:endif

Vim elseif clauses, so obviously Vimscript is case sensitive. I t makes sense, but there's nothing to be shocked about. Now do the following command:

:set ignorecase
:if "foo" == "FOO"
:    echom "no, it couldn't be"
:elseif "foo" == "foo"
:    echom "this must be the one"
:endif

Ah! /b10>Stop here. Yes, what you see is true.

== depends on the user's settings.

I swear I didn't fool you. Y ou'll try again. I'm not kidding, I didn't do it.

Defensive programming

What does that mean? T his means that when developing plug-ins for others, you can't == An unpackaged ==

This advice is nmap VS nnoremap N ever guess your user's configuration. V im is both old and difficult. When you write plug-ins, you have to assume that the configuration of the users is very different and changeable.

So how do you adapt to this ridiculous reality? Fortunately, Vim has two additional comparison operators to handle this problem.

Execute the following command:

:set noignorecase
:if "foo" ==? "FOO"
:    echom "first"
:elseif "foo" ==? "foo"
:    echom "second"
:endif

Vim first ==? i s the "Case insensitive regardless of how you set it" comparison operator. Now do the following command:

:set ignorecase
:if "foo" ==# "FOO"
:    echom "one"
:elseif "foo" ==# "foo"
:    echom "two"
:endif

Vim two because ==# sensitive no matter how you set it" comparison operator.

The end of the story tells us one thing: you should always make explicit case-sensitive or insensitive comparisons. U sing the normal form is wrong, and it's going to go wrong after all. A little more can save you from the mess.

When you compare integers, this little difference doesn't make any difference. However, I recommend using case-sensitive comparisons every time, even if you don't necessarily need to, better than forgetting.

When comparing integers, use ==# . . . or ==? A ll right, and it will work correctly in the future once you change to a string-to-string comparison. If you really want to == with , it's not a bad thing, but keep in mind that once you've changed to a comparison between strings, you'll need to modify the comparison operator.

Practice

Try :set ignorecase and :set noignorecase see how the comparisons perform in different states.

Read: :help ignorecase see why some people set this option.

Read :help expr4 see all the allowed comparison operators.