If I define the variable foo
like this:
:let foo = '~/Desktop'
And I want to check the length of the string it contains, then I can type:
:echo strlen(foo)
The output is 9, which is right.
If I had typed :echo strlen('foo')
, the output would have been 3, because Vim would have calculated the length of the string 'foo'
instead of the evaluation of the variable foo
.
Next, I want to expand the filepath contained in foo
.
The following command doesn't echo any message:
:echo glob('foo')
But the following does output something (/home/username/Desktop
):
:echo glob(foo)
So, these 2 examples seem to show that if I want to pass the evaluation of a variable name as an argument to a function, I must not put quotes around it.
Now, if I want to check whether the variable foo
exists and has been defined, and I type:
:if exists(foo) | echo "success!" | endif
There's no output. However, the following works:
:if exists('foo') | echo "success!" | endif
This time, it was different, as I needed to put quotes around foo
for it to be evaluated by exists()
.
Why do I need to use quotes with exists()
but neither with strlen()
nor with glob()
?
When do you need to put quotes around a variable name which is passed as an argument to a function?