Question

What is the difference between using has() and !has() in vimscript?

Was it helpful?

Solution

It's probably not that you're overthinking it, just that you haven't encountered ! in programming languages before. It's pretty straightforward, though -- here's a quick explanation.

If you want to do something based on a condition, you use an if statement, right? For example,

if has('relativenumber')
    echo "Your Vim has the relative number feature!"
endif

If you want to do something if that condition is not true, you put a ! before your condition. (this is called "negating" a logical condition)

if !has('relativenumber')
    echo "Your Vim does NOT have the relative number feature."
endif

You can use that in other case too. Take this, for example:

if x > 3
    echo "x is greater than three"
endif

You have to include parentheses to negate it though. (Order of operations!)

if !(x > 3)
    echo "x is less than or equal to three"
endif

This is equivalent to

if x <= 3
    echo "x is less than or equal to three"
endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top