문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top